Completed
Push — develop ( 273208...5b590b )
by Daniel
09:31
created

base_collection::get_entities()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\model;
11
12
abstract class base_collection implements \Iterator, \Countable, \ArrayAccess
13
{
14
	protected $entities = array();
15
16
	protected $entity_class;
17
18
	/**
19
	 * Get the entities stored in the collection
20
	 */
21 10
	public function get_entities()
22
	{
23 10
		return $this->entities;
24
	}
25
26
	/**
27
	 * Clear the collection
28
	 */
29 82
	public function clear()
30
	{
31 82
		$this->entities = array();
32 82
	}
33
34
	/**
35
	 * Reset the collection (implementation required by Iterator Interface)
36
	 */
37 51
	public function rewind()
38
	{
39 51
		reset($this->entities);
40 51
	}
41
42
	/**
43
	 * Get the current entity in the collection (implementation required by Iterator Interface)
44
	 *
45
	 * @return mixed
46
	 */
47 54
	public function current()
48
	{
49 54
		return current($this->entities);
50
	}
51
52
	/**
53
	 * Get the next entity in the collection (implementation required by Iterator Interface)
54
	 *
55
	 * @return mixed
56
	 */
57 50
	public function next()
58
	{
59 50
		return next($this->entities);
60
	}
61
62
	/**
63
	 * Get the key of the current entity in the collection (implementation required by Iterator Interface)
64
	 *
65
	 * @return mixed
66
	 */
67 4
	public function key()
68
	{
69 4
		return key($this->entities);
70
	}
71
72
	/**
73
	 * Check if there’re more entities in the collection (implementation required by Iterator Interface)
74
	 *
75
	 * @return bool
76
	 */
77 52
	public function valid()
78
	{
79 52
		return ($this->current() !== false);
80
	}
81
82
	/**
83
	 * Count the number of entities in the collection (implementation required by Countable Interface)
84
	 *
85
	 * @return mixed
86
	 */
87 22
	public function count()
88
	{
89 22
		return count($this->entities);
90
	}
91
92
	/**
93
	 * Add an entity to the collection (implementation required by ArrayAccess interface)
94
	 *
95
	 * @param mixed $key
96
	 * @param mixed $entity
97
	 * @return bool
98
	 * @throws \blitze\sitemaker\exception\invalid_argument
99
	 */
100 79
	public function offsetSet($key, $entity)
101
	{
102 79
		if ($entity instanceof $this->entity_class)
103 79
		{
104 75
			if (!isset($key))
105 75
			{
106
				$this->entities[] = $entity;
107
			}
108
			else
109
			{
110 75
				$this->entities[$key] = $entity;
111
			}
112 75
			return true;
113
		}
114
115 4
		throw new \blitze\sitemaker\exception\invalid_argument(array('entity', 'INVALID_ENTITY'));
116
	}
117
118
	/**
119
	 * Remove an entity from the collection (implementation required by ArrayAccess interface)
120
	 *
121
	 * @param mixed $key
122
	 * @return bool
123
	 */
124 4
	public function offsetUnset($key)
125
	{
126 4
		if ($key instanceof $this->entity_class)
127 4
		{
128 4
			$this->entities = array_filter(
129 4
				$this->entities,
130 4
				function ($v) use ($key)
131
				{
132 4
					return $v !== $key;
133
				}
134 4
			);
135 4
			return true;
136
		}
137
138 4
		if (isset($this->entities[$key]))
139 4
		{
140 4
			unset($this->entities[$key]);
141 4
			return true;
142
		}
143
		return false;
144
	}
145
146
	/**
147
	 * Get the specified entity in the collection (implementation required by ArrayAccess interface)
148
	 *
149
	 * @param mixed $key
150
	 * @return null
151
	 */
152 5
	public function offsetGet($key)
153
	{
154 5
		return isset($this->entities[$key]) ? $this->entities[$key] : null;
155
	}
156
157
	/**
158
	 * Check if the specified entity exists in the collection (implementation required by ArrayAccess interface)
159
	 *
160
	 * @param mixed $key
161
	 * @return bool
162
	 */
163 6
	public function offsetExists($key)
164
	{
165 6
		return isset($this->entities[$key]);
166
	}
167
}
168