1: <?php
2: /**
3: * PHP OpenCloud library.
4: *
5: * @copyright 2013 Rackspace Hosting, Inc. See LICENSE for information.
6: * @license https://www.apache.org/licenses/LICENSE-2.0
7: * @author Glen Campbell <glen.campbell@rackspace.com>
8: * @author Jamie Hannaford <jamie.hannaford@rackspace.com>
9: */
10:
11: namespace OpenCloud\Autoscale\Resource;
12:
13: use OpenCloud\Common\PersistentObject;
14:
15: /**
16: * Contains generic, abstracted functionality for Autoscale resources.
17: */
18: abstract class AbstractResource extends PersistentObject
19: {
20: /**
21: * These are used to set the object used for JSON encode.
22: *
23: * @var array
24: */
25: public $createKeys = array();
26:
27: /**
28: * These resources are associated with this one. When this resource object
29: * is populated, if a key is found matching one of these array keys, it is
30: * set as an instantiated resource object (rather than an arbitrary string
31: * or stdClass object).
32: *
33: * @var array
34: */
35: public $associatedResources = array();
36:
37: /**
38: * Same as an associated resource, but it's instantiated as a Collection.
39: *
40: * @var array
41: */
42: public $associatedCollections = array();
43:
44: /**
45: * Creates the object which will be JSON encoded for request.
46: *
47: * @return \stdClass
48: */
49: public function createJson()
50: {
51: $object = new \stdClass;
52:
53: foreach ($this->createKeys as $key) {
54: if ($value = $this->getProperty($key)) {
55: $object->$key = $value;
56: }
57: }
58:
59: if (!empty($this->metadata)) {
60: $object->metadata = new \stdClass;
61: foreach ($this->getMetadata()->toArray() as $key => $value) {
62: $object->metadata->$key = $value;
63: }
64: }
65:
66: return $object;
67: }
68:
69: /**
70: * Creates the object which will be JSON encoded for request.
71: *
72: * @return array
73: */
74: protected function updateJson($params = array())
75: {
76: $existing = array();
77: foreach ($this->createKeys as $key) {
78: $existing[$key] = $this->$key;
79: }
80:
81: return $existing + $params;
82: }
83:
84: public function primaryKeyField()
85: {
86: return 'id';
87: }
88:
89: }