1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17
18 namespace OpenCloud\DNS\Resource;
19
20 use OpenCloud\Common\Exceptions;
21 use OpenCloud\Common\Http\Message\Formatter;
22 use OpenCloud\Common\Lang;
23 use OpenCloud\Common\Resource\PersistentResource;
24
25 abstract class AbstractResource extends PersistentResource
26 {
27 public function create($params = array())
28 {
29 $body = Formatter::decode(parent::create($params));
30
31 return new AsyncResponse($this->getService(), $body);
32 }
33
34 public function update($params = array())
35 {
36 $response = parent::update($params);
37 $body = Formatter::decode($response);
38
39 return new AsyncResponse($this->getService(), $body);
40 }
41
42 public function delete()
43 {
44 $body = Formatter::decode(parent::delete());
45
46 return new AsyncResponse($this->getService(), $body);
47 }
48
49 protected function createJson()
50 {
51 if (!$this->getCreateKeys()) {
52 throw new Exceptions\CreateError(
53 Lang::translate('Missing [createKeys]')
54 );
55 }
56
57 return (object) array(
58 self::jsonCollectionName() => array(
59 $this->getJson($this->getCreateKeys())
60 )
61 );
62 }
63
64 protected function updateJson($params = array())
65 {
66 if (!$this->getUpdateKeys()) {
67 throw new Exceptions\UpdateError(
68 Lang::translate('Missing [updateKeys]')
69 );
70 }
71
72 return $this->getJson($this->getUpdateKeys());
73 }
74
75 76 77 78 79 80
81 private function getJson($keys)
82 {
83 $object = new \stdClass;
84 foreach ($keys as $item) {
85 if (!empty($this->$item)) {
86 $object->$item = $this->$item;
87 }
88 }
89
90 return $object;
91 }
92
93 94 95 96 97
98 public function getCreateKeys()
99 {
100 return (!empty($this->createKeys)) ? $this->createKeys : false;
101 }
102
103 104 105 106 107
108 public function getUpdateKeys()
109 {
110 return (!empty($this->updateKeys)) ? $this->updateKeys : false;
111 }
112 }
113