1: <?php
2: /**
3: * PHP OpenCloud library.
4: *
5: * @copyright Copyright 2013 Rackspace US, Inc. See COPYING for licensing information.
6: * @license https://www.apache.org/licenses/LICENSE-2.0 Apache 2.0
7: * @version 1.6.0
8: * @author Glen Campbell <glen.campbell@rackspace.com>
9: * @author Jamie Hannaford <jamie.hannaford@rackspace.com>
10: */
11:
12: namespace OpenCloud\LoadBalancer\Resources;
13:
14: use OpenCloud\Common\PersistentObject;
15: use OpenCloud\Common\Lang;
16:
17: /**
18: * SubResource is an abstract class that handles subresources of a
19: * LoadBalancer object; for example, the
20: * `/loadbalancers/{id}/errorpage`. Since most of the subresources are
21: * handled in a similar manner, this consolidates the functions.
22: *
23: * There are really four pieces of data that define a subresource:
24: * * `$url_resource` - the actual name of the url component
25: * * `$json_name` - the name of the JSON object holding the data
26: * * `$json_collection_name` - if the collection is not simply
27: * `$json_name . 's'`, this defines the collectio name.
28: * * `$json_collection_element` - if the object in a collection is not
29: * anonymous, this defines the name of the element holding the object.
30: * Of these, only the `$json_name` and `$url_resource` are required.
31: */
32: abstract class SubResource extends PersistentObject
33: {
34:
35: /**
36: * This method needs attention.
37: *
38: * @codeCoverageIgnore
39: */
40: public function initialRefresh()
41: {
42: if (isset($this->id)) {
43: $this->refresh();
44: } else {
45: $entity = (method_exists($this->getParent(), 'url')) ? $this->getParent() : $this->getService();
46: $this->refresh(null, $entity->url($this->resourceName()));
47: }
48: }
49:
50: /**
51: * returns the URL of the SubResource
52: *
53: * @api
54: * @param string $subresource the subresource of the parent
55: * @param array $qstr an array of key/value pairs to be converted to
56: * query string parameters for the subresource
57: * @return string
58: */
59: public function url($subresource = null, $qstr = array())
60: {
61: return $this->getParent()->url($this->ResourceName());
62: }
63:
64: /**
65: * returns the JSON document's object for creating the subresource
66: *
67: * The value `$_create_keys` should be an array of names of data items
68: * that can be used in the creation of the object.
69: *
70: * @return \stdClass;
71: */
72: protected function CreateJson()
73: {
74: $object = new \stdClass;
75:
76: foreach ($this->createKeys as $item) {
77: $object->$item = $this->$item;
78: }
79:
80: if ($top = $this->jsonName()) {
81: $object = (object) array($top => $object);
82: }
83:
84: return $object;
85: }
86:
87: /**
88: * returns the JSON for the update (same as create)
89: *
90: * For these subresources, the update JSON is the same as the Create JSON
91: * @return \stdClass
92: */
93: protected function updateJson($params = array())
94: {
95: return $this->createJson();
96: }
97:
98: /**
99: * returns a (default) name of the object
100: *
101: * The name is constructed by the object class and the object's ID.
102: *
103: * @api
104: * @return string
105: */
106: public function name()
107: {
108: return method_exists($this->getParent(), 'id')
109: ? sprintf('%s-%s', get_class($this), $this->getParent()->id())
110: : parent::name();
111: }
112: }
113: