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 Jamie Hannaford <jamie.hannaford@rackspace.com>
8: */
9:
10: namespace OpenCloud\Common\Collection;
11:
12: use Countable;
13: use ArrayAccess;
14:
15: /**
16: * A generic, abstract collection class that allows collections to exhibit array functionality.
17: *
18: * @package OpenCloud\Common\Collection
19: * @since 1.8.0
20: */
21: abstract class ArrayCollection implements ArrayAccess, Countable
22: {
23: /**
24: * @var array The elements being held by this iterator.
25: */
26: protected $elements;
27:
28: /**
29: * @param array $data
30: */
31: public function __construct(array $data = array())
32: {
33: $this->setElements($data);
34: }
35:
36: /**
37: * @return int
38: */
39: public function count()
40: {
41: return count($this->elements);
42: }
43:
44: /**
45: * @param array $data
46: * @return $this
47: */
48: public function setElements(array $data = array())
49: {
50: $this->elements = $data;
51: return $this;
52: }
53:
54: /**
55: * Sets a value to a particular offset.
56: *
57: * @param mixed $offset
58: * @param mixed $value
59: */
60: public function offsetSet($offset, $value)
61: {
62: $this->elements[$offset] = $value;
63: }
64:
65: /**
66: * Appends a value to the container.
67: *
68: * @param $value
69: */
70: public function append($value)
71: {
72: $this->elements[] = $value;
73: }
74:
75: /**
76: * Checks to see whether a particular offset key exists.
77: *
78: * @param mixed $offset
79: * @return bool
80: */
81: public function offsetExists($offset)
82: {
83: return isset($this->elements[$offset]);
84: }
85:
86: /**
87: * Checks to see whether a particular value exists.
88: *
89: * @param $value
90: * @return bool
91: */
92: public function valueExists($value)
93: {
94: return array_search($value, $this->elements) !== false;
95: }
96:
97: /**
98: * Unset a particular key.
99: *
100: * @param mixed $offset
101: */
102: public function offsetUnset($offset)
103: {
104: unset($this->elements[$offset]);
105: }
106:
107: /**
108: * Get the value for a particular offset key.
109: *
110: * @param mixed $offset
111: * @return mixed|null
112: */
113: public function offsetGet($offset)
114: {
115: return $this->offsetExists($offset) ? $this->elements[$offset] : null;
116: }
117:
118: }