1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace OpenCloud\CloudMonitoring\Resource;
12:
13: use OpenCloud\Common\Exceptions;
14: use OpenCloud\Common\PersistentObject;
15: use OpenCloud\Common\Http\Message\Formatter;
16:
17: abstract class AbstractResource extends PersistentObject
18: {
19: public function createJson()
20: {
21: foreach (static::$requiredKeys as $requiredKey) {
22: if (!$this->getProperty($requiredKey)) {
23: throw new Exceptions\CreateError(sprintf(
24: "%s is required to create a new %s", $requiredKey, get_class()
25: ));
26: }
27: }
28:
29: $object = new \stdClass;
30:
31: foreach (static::$emptyObject as $key) {
32: if ($property = $this->getProperty($key)) {
33: $object->$key = $property;
34: }
35: }
36:
37: return $object;
38: }
39:
40: protected function updateJson($params = array())
41: {
42: $object = (object) $params;
43:
44: foreach (static::$requiredKeys as $requiredKey) {
45: if (!$this->getProperty($requiredKey)) {
46: throw new Exceptions\UpdateError(sprintf(
47: "%s is required to update a %s", $requiredKey, get_class($this)
48: ));
49: }
50: }
51:
52: return $object;
53: }
54:
55: 56: 57: 58: 59: 60:
61: public function listAll()
62: {
63: return $this->getService()->collection(get_class($this), $this->url());
64: }
65:
66: 67: 68: 69: 70: 71: 72: 73:
74: public function testParams($params = array(), $debug = false)
75: {
76: $json = json_encode((object) $params);
77:
78:
79: $response = $this->getService()
80: ->getClient()
81: ->post($this->testUrl($debug), array(), $json)
82: ->send();
83:
84: return Formatter::decode($response);
85: }
86:
87: 88: 89: 90: 91: 92: 93:
94: public function test($debug = false)
95: {
96: $json = json_encode($this->updateJson());
97: $this->checkJsonError();
98:
99: $response = $this->getClient()
100: ->post($this->testExistingUrl($debug), array(), $json)
101: ->send();
102:
103: return Formatter::decode($response);
104: }
105:
106: }