Completed
Push — master ( 122574...f8fd12 )
by James
03:56
created

Collection::next()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Intraxia\Jaxion\Axolotl;
3
4
use Countable;
5
use Intraxia\Jaxion\Contract\Axolotl\Serializes;
6
use Iterator;
7
use LogicException;
8
use RuntimeException;
9
10
/**
11
 * Class Collection
12
 *
13
 * @package Intraxia\Jaxion
14
 * @subpackage Axolotl
15
 */
16
class Collection implements Countable, Iterator, Serializes {
17
	/**
18
	 * Collection elements.
19
	 *
20
	 * @var array
21
	 */
22
	protected $elements = array();
23
24
	/**
25
	 * Models registered to the collection.
26
	 *
27
	 * @var string
28
	 */
29
	protected $model;
30
31
	/**
32
	 * Where Collection is in loop.
33
	 *
34
	 * @var int
35
	 */
36
	protected $position = 0;
37
38
	/**
39
	 * Collection constructor.
40
	 *
41
	 * @param array $elements
42
	 * @param array $config
43
	 */
44 60
	public function __construct( array $elements = array(), array $config = array() ) {
45 60
		$this->parse_config( $config );
46
47 57
		foreach ( $elements as $element ) {
48 24
			$this->add( $element );
49 57
		}
50 57
	}
51
52
	/**
53
	 * Adds a new element to the Collection.
54
	 *
55
	 * @param mixed $element
56
	 *
57
	 * @throws RuntimeException
58
	 */
59 27
	public function add( $element ) {
60 27
		if ( $this->model && is_array( $element ) ) {
61 9
			$element = new $this->model( $element );
62 9
		}
63
64 27
		if ( $this->model && ! ( $element instanceof $this->model ) ) {
65 3
			throw new RuntimeException;
66
		}
67
68 24
		$this->elements[] = $element;
69 24
	}
70
71
	/**
72
	 * Fetches the element at the provided index.
73
	 *
74
	 * @param int $index
75
	 *
76
	 * @return mixed
77
	 */
78 12
	public function at( $index ) {
79 12
		return isset( $this->elements[ $index ] ) ? $this->elements[ $index ] : null;
80
	}
81
82
	/**
83
	 * Return the current element.
84
	 *
85
	 * @return mixed
86
	 */
87 3
	public function current() {
88 3
		return $this->at( $this->position );
89
	}
90
91
	/**
92
	 * Move forward to next element.
93
	 */
94 3
	public function next() {
95 3
		$this->position ++;
96 3
	}
97
98
	/**
99
	 * Return the key of the current element.
100
	 *
101
	 * @return mixed
102
	 */
103 3
	public function key() {
104 3
		return $this->position;
105
	}
106
107
	/**
108
	 * Checks if current position is valid.
109
	 *
110
	 * @return bool
111
	 */
112 3
	public function valid() {
113 3
		return isset( $this->elements[ $this->position ] );
114
	}
115
116
	/**
117
	 * Rewind the Iterator to the first element.
118
	 */
119 3
	public function rewind() {
120 3
		$this->position = 0;
121 3
	}
122
123
	/**
124
	 * Count elements of an object.
125
	 *
126
	 * @return int
127
	 */
128 6
	public function count() {
129 6
		return count( $this->elements );
130
	}
131
132
	/**
133
	 * Parses the Collection config to set its properties.
134
	 *
135
	 * @param array $config
136
	 *
137
	 * @throws LogicException
138
	 */
139 60
	protected function parse_config( array $config ) {
140 60
		if ( isset( $config['model'] ) ) {
141 15
			$model = $config['model'];
142
143 15
			if ( ! is_subclass_of( $model, 'Intraxia\Jaxion\Axolotl\Model' ) ) {
144 3
				throw new LogicException;
145
			}
146
147 12
			$this->model = $model;
148 12
		}
149 57
	}
150
151
	/**
152
	 * {@inheritDoc}
153
	 *
154
	 * @return array
155
	 */
156
	public function serialize() {
157 9
		return array_map(function( $element ) {
158 6
			if ( $element instanceof Serializes ) {
159 3
				return $element->serialize();
160
			}
161
162 3
			return $element;
163 9
		}, $this->elements);
164
	}
165
}
166