1 <?php
2 /**
3 * Copyright 2012-2014 Rackspace US, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 namespace OpenCloud\Image\Resource;
19
20 use OpenCloud\Common\Resource\BaseResource;
21
22 /**
23 * Class that represents abstracted functionality for JSON schema objects. Because the nature of these objects is so
24 * dynamic (i.e. their structure is determined by an API-generated schema document), they implement the \ArrayAccess
25 * SPL interface. This allows them to be accessed as arrays - which is very useful for undefined properties.
26 *
27 * @package OpenCloud\Images\Resource
28 * @codeCoverageIgnore
29 */
30 abstract class AbstractSchemaResource extends BaseResource implements \ArrayAccess
31 {
32 /** @var string The ID of this resource */
33 protected $id;
34
35 /** @var array The internal elements of this model */
36 protected $data = array();
37
38 /**
39 * @param array $data
40 */
41 public function setData(array $data)
42 {
43 $this->data = $data;
44 }
45
46 /**
47 * @return array
48 */
49 public function getData()
50 {
51 return $this->data;
52 }
53
54 /**
55 * @param $id
56 */
57 public function setId($id)
58 {
59 $this->id = (string) $id;
60 }
61
62 /**
63 * @return string
64 */
65 public function getId()
66 {
67 return $this->id;
68 }
69
70 /**
71 * Sets a value to a particular offset.
72 *
73 * @param mixed $offset
74 * @param mixed $value
75 */
76 public function offsetSet($offset, $value)
77 {
78 if ($offset === null) {
79 $this->data[] = $value;
80 } else {
81 $this->data[$offset] = $value;
82 }
83 }
84
85 /**
86 * Checks to see whether a particular offset key exists.
87 *
88 * @param mixed $offset
89 * @return bool
90 */
91 public function offsetExists($offset)
92 {
93 return array_key_exists($offset, $this->data);
94 }
95
96 /**
97 * Unset a particular key.
98 *
99 * @param mixed $offset
100 */
101 public function offsetUnset($offset)
102 {
103 unset($this->data[$offset]);
104 }
105
106 /**
107 * Get the value for a particular offset key.
108 *
109 * @param mixed $offset
110 * @return mixed|null
111 */
112 public function offsetGet($offset)
113 {
114 return $this->offsetExists($offset) ? $this->data[$offset] : null;
115 }
116 }
117