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\Common\Collection;
19
20 use Countable;
21 use OpenCloud\Common\ArrayAccess;
22
23 /**
24 * A generic, abstract collection class that allows collections to exhibit array functionality.
25 *
26 * @package OpenCloud\Common\Collection
27 */
28 abstract class ArrayCollection extends ArrayAccess implements Countable
29 {
30 /**
31 * @var array The elements being held by this iterator.
32 */
33 protected $elements;
34
35 /**
36 * @param array $data
37 */
38 public function __construct(array $data = array())
39 {
40 $this->setElements($data);
41 }
42
43 /**
44 * @return int
45 */
46 public function count()
47 {
48 return count($this->elements);
49 }
50
51 /**
52 * @param array $data
53 * @return $this
54 */
55 public function setElements(array $data = array())
56 {
57 $this->elements = $data;
58
59 return $this;
60 }
61
62 /**
63 * Appends a value to the container.
64 *
65 * @param $value
66 */
67 public function append($value)
68 {
69 $this->elements[] = $value;
70 }
71
72 /**
73 * Checks to see whether a particular value exists.
74 *
75 * @param $value
76 * @return bool
77 */
78 public function valueExists($value)
79 {
80 return array_search($value, $this->elements) !== false;
81 }
82 }
83