base_collection::clear()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 104
	public function clear()
30
	{
31 104
		$this->entities = array();
32 104
	}
33
34
	/**
35
	 * Reset the collection (implementation required by Iterator Interface)
36
	 */
37 73
	public function rewind()
38
	{
39 73
		reset($this->entities);
40 73
	}
41
42
	/**
43
	 * Get the current entity in the collection (implementation required by Iterator Interface)
44
	 *
45
	 * @return mixed
46
	 */
47 76
	public function current()
48
	{
49 76
		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 72
	public function next()
58
	{
59 72
		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 74
	public function valid()
78
	{
79 74
		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 void
98
	 * @throws \blitze\sitemaker\exception\invalid_argument
99
	 */
100 101
	public function offsetSet($key, $entity)
101
	{
102 101
		if (!$entity instanceof $this->entity_class)
103 101
		{
104 97
			throw new \blitze\sitemaker\exception\invalid_argument(array('entity', 'INVALID_ENTITY'));
105 97
		}
106
107
		if (!isset($key))
108
		{
109
			$this->entities[] = $entity;
110 97
		}
111
		else
112 97
		{
113
			$this->entities[$key] = $entity;
114
		}
115 4
	}
116
117
	/**
118
	 * Remove an entity from the collection (implementation required by ArrayAccess interface)
119
	 *
120
	 * @param mixed $key
121
	 * @return void
122
	 */
123
	public function offsetUnset($key)
124 4
	{
125
		if ($key instanceof $this->entity_class)
126 4
		{
127 4
			$this->entities = array_filter(
128 4
				$this->entities,
129 4
				function ($v) use ($key)
130 4
				{
131
					return $v !== $key;
132 4
				}
133
			);
134 4
		}
135 4
		else if (isset($this->entities[$key]))
136
		{
137
			unset($this->entities[$key]);
138 4
		}
139 4
	}
140 4
141 4
	/**
142 4
	 * Get the specified entity in the collection (implementation required by ArrayAccess interface)
143 4
	 *
144 4
	 * @param mixed $key
145
	 * @return null
146
	 */
147
	public function offsetGet($key)
148
	{
149
		return isset($this->entities[$key]) ? $this->entities[$key] : null;
150
	}
151
152
	/**
153 5
	 * Check if the specified entity exists in the collection (implementation required by ArrayAccess interface)
154
	 *
155 5
	 * @param mixed $key
156
	 * @return bool
157
	 */
158
	public function offsetExists($key)
159
	{
160
		return isset($this->entities[$key]);
161
	}
162
}
163