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\DNS\Resource;
12:
13: use OpenCloud\Common\PersistentObject;
14: use OpenCloud\Common\Lang;
15: use OpenCloud\Common\Exceptions;
16: use OpenCloud\Common\Http\Message\Formatter;
17:
18: /**
19: * The DnsObject class is an extension of the PersistentObject class that
20: * permits the asynchronous responses used by Cloud DNS
21: */
22: abstract class Object extends PersistentObject
23: {
24:
25: /**
26: * Create() returns an asynchronous response
27: *
28: * @param array $params array of key/value pairs
29: * @return AsyncResponse
30: */
31: public function create($params = array())
32: {
33: $body = Formatter::decode(parent::create($params));
34: return new AsyncResponse($this->getService(), $body);
35: }
36:
37: /**
38: * Update() returns an asynchronous response
39: *
40: * @param array $params array of key/value pairs
41: * @return AsyncResponse
42: */
43: public function update($params = array())
44: {
45: $response = parent::update($params);
46: $body = Formatter::decode($response);
47: return new AsyncResponse($this->getService(), $body);
48: }
49:
50: /**
51: * Delete() returns an asynchronous response
52: *
53: * @param array $params array of key/value pairs
54: * @return AsyncResponse
55: */
56: public function delete()
57: {
58: $body = Formatter::decode(parent::delete());
59: return new AsyncResponse($this->getService(), $body);
60: }
61:
62: /**
63: * creates the JSON for create
64: *
65: * @return stdClass
66: */
67: protected function createJson()
68: {
69: if (!$this->getCreateKeys()) {
70: throw new Exceptions\CreateError(
71: Lang::translate('Missing [createKeys]')
72: );
73: }
74:
75: return (object) array(
76: self::jsonCollectionName() => array(
77: $this->getJson($this->getCreateKeys())
78: )
79: );
80: }
81:
82: /**
83: * creates the JSON for update
84: *
85: * @return stdClass
86: */
87: protected function updateJson($params = array())
88: {
89: if (!$this->getUpdateKeys()) {
90: throw new Exceptions\UpdateError(
91: Lang::translate('Missing [updateKeys]')
92: );
93: }
94: return $this->getJson($this->getUpdateKeys());
95: }
96:
97: /**
98: * returns JSON based on $keys
99: *
100: * @param array $keys list of items to include
101: * @return stdClass
102: */
103: private function getJson($keys)
104: {
105: $object = new \stdClass;
106: foreach($keys as $item) {
107: if (!empty($this->$item)) {
108: $object->$item = $this->$item;
109: }
110: }
111: return $object;
112: }
113:
114: /**
115: * Retrieve the keys which are required when the object is created.
116: *
117: * @return array|false
118: */
119: public function getCreateKeys()
120: {
121: return (!empty($this->createKeys)) ? $this->createKeys : false;
122: }
123:
124: /**
125: * Retrieve the keys which are required when the object is updated.
126: *
127: * @return array|false
128: */
129: public function getUpdateKeys()
130: {
131: return (!empty($this->updateKeys)) ? $this->updateKeys : false;
132: }
133:
134: }
135: