Completed
Push — master ( cf44b9...669da9 )
by James
12:32 queued 06:44
created
src/Axolotl/Collection.php 2 patches
Indentation   +223 added lines, -223 removed lines patch added patch discarded remove patch
@@ -14,227 +14,227 @@
 block discarded – undo
14 14
  * @subpackage Axolotl
15 15
  */
16 16
 class Collection implements Countable, Iterator, Serializes {
17
-	/**
18
-	 * Collection elements.
19
-	 *
20
-	 * @var array
21
-	 */
22
-	protected $elements = array();
23
-
24
-	/**
25
-	 * Collection configuration.
26
-	 *
27
-	 * @var array
28
-	 */
29
-	protected $config = array();
30
-
31
-	/**
32
-	 * Models registered to the collection.
33
-	 *
34
-	 * @var string
35
-	 */
36
-	protected $model;
37
-
38
-	/**
39
-	 * Where Collection is in loop.
40
-	 *
41
-	 * @var int
42
-	 */
43
-	protected $position = 0;
44
-
45
-	/**
46
-	 * Collection constructor.
47
-	 *
48
-	 * @param array $elements
49
-	 * @param array $config
50
-	 */
51
-	public function __construct( array $elements = array(), array $config = array() ) {
52
-		$this->parse_config( $this->config = $config );
53
-
54
-		foreach ( $elements as $element ) {
55
-			$this->add( $element );
56
-		}
57
-	}
58
-
59
-	/**
60
-	 * Adds a new element to the Collection.
61
-	 *
62
-	 * @param mixed $element
63
-	 *
64
-	 * @throws RuntimeException
65
-	 *
66
-	 * @return $this
67
-	 */
68
-	public function add( $element ) {
69
-		if ( $this->model && is_array( $element ) ) {
70
-			$element = new $this->model( $element );
71
-		}
72
-
73
-		if ( $this->model && ! ( $element instanceof $this->model ) ) {
74
-			throw new RuntimeException;
75
-		}
76
-
77
-		$this->elements[] = $element;
78
-
79
-		return $this;
80
-	}
81
-
82
-	/**
83
-	 * Removes an element by index or value.
84
-	 *
85
-	 * @param mixed $index
86
-	 *
87
-	 * @return $this
88
-	 */
89
-	public function remove( $index ) {
90
-		if ( ! is_string( $index ) || ! is_numeric( $index ) || ! isset( $this->elements[ $index ] ) ) {
91
-			foreach ( $this->elements as $key => $element ) {
92
-				if ( $element === $index ) {
93
-					$index = $key;
94
-					break;
95
-				}
96
-			}
97
-		}
98
-
99
-		if ( isset( $this->elements[ $index ] ) ) {
100
-			unset( $this->elements[ $index ] );
101
-			$this->elements = array_values( $this->elements );
102
-		}
103
-
104
-		return $this;
105
-	}
106
-
107
-	/**
108
-	 * Fetches the element at the provided index.
109
-	 *
110
-	 * @param int $index
111
-	 *
112
-	 * @return mixed|null
113
-	 */
114
-	public function at( $index ) {
115
-		return isset( $this->elements[ $index ] ) ? $this->elements[ $index ] : null;
116
-	}
117
-
118
-	/**
119
-	 * Maps over the Collection's elements,
120
-	 *
121
-	 * @param callable $callback
122
-	 *
123
-	 * @return Collection
124
-	 */
125
-	public function map( callable $callback ) {
126
-		return new Collection( array_map( $callback, $this->elements ) );
127
-	}
128
-
129
-	/**
130
-	 * Filters the Collection's elements.
131
-	 *
132
-	 * @param callable $callback
133
-	 *
134
-	 * @return Collection
135
-	 */
136
-	public function filter( callable $callback ) {
137
-		return new Collection( array_filter( $this->elements, $callback ) );
138
-	}
139
-
140
-	/**
141
-	 * Fetches a single element in the Collection by callback.
142
-	 *
143
-	 * @param callable $callback
144
-	 *
145
-	 * @return mixed|null
146
-	 */
147
-	public function find( callable $callback ) {
148
-		foreach ( $this->elements as $element ) {
149
-			if ( call_user_func( $callback, $element ) ) {
150
-				return $element;
151
-			}
152
-		}
153
-
154
-		return null;
155
-	}
156
-
157
-	/**
158
-	 * {@inheritDoc}
159
-	 *
160
-	 * @return array
161
-	 */
162
-	public function serialize() {
163
-		return array_map(function( $element ) {
164
-			if ( $element instanceof Serializes ) {
165
-				return $element->serialize();
166
-			}
167
-
168
-			return $element;
169
-		}, $this->elements);
170
-	}
171
-
172
-	/**
173
-	 * Return the current element.
174
-	 *
175
-	 * @return mixed
176
-	 */
177
-	public function current() {
178
-		return $this->at( $this->position );
179
-	}
180
-
181
-	/**
182
-	 * Move forward to next element.
183
-	 */
184
-	public function next() {
185
-		$this->position ++;
186
-	}
187
-
188
-	/**
189
-	 * Return the key of the current element.
190
-	 *
191
-	 * @return mixed
192
-	 */
193
-	public function key() {
194
-		return $this->position;
195
-	}
196
-
197
-	/**
198
-	 * Checks if current position is valid.
199
-	 *
200
-	 * @return bool
201
-	 */
202
-	public function valid() {
203
-		return isset( $this->elements[ $this->position ] );
204
-	}
205
-
206
-	/**
207
-	 * Rewind the Iterator to the first element.
208
-	 */
209
-	public function rewind() {
210
-		$this->position = 0;
211
-	}
212
-
213
-	/**
214
-	 * Count elements of an object.
215
-	 *
216
-	 * @return int
217
-	 */
218
-	public function count() {
219
-		return count( $this->elements );
220
-	}
221
-
222
-	/**
223
-	 * Parses the Collection config to set its properties.
224
-	 *
225
-	 * @param array $config
226
-	 *
227
-	 * @throws LogicException
228
-	 */
229
-	protected function parse_config( array $config ) {
230
-		if ( isset( $config['model'] ) ) {
231
-			$model = $config['model'];
232
-
233
-			if ( ! is_subclass_of( $model, 'Intraxia\Jaxion\Axolotl\Model' ) ) {
234
-				throw new LogicException;
235
-			}
236
-
237
-			$this->model = $model;
238
-		}
239
-	}
17
+    /**
18
+     * Collection elements.
19
+     *
20
+     * @var array
21
+     */
22
+    protected $elements = array();
23
+
24
+    /**
25
+     * Collection configuration.
26
+     *
27
+     * @var array
28
+     */
29
+    protected $config = array();
30
+
31
+    /**
32
+     * Models registered to the collection.
33
+     *
34
+     * @var string
35
+     */
36
+    protected $model;
37
+
38
+    /**
39
+     * Where Collection is in loop.
40
+     *
41
+     * @var int
42
+     */
43
+    protected $position = 0;
44
+
45
+    /**
46
+     * Collection constructor.
47
+     *
48
+     * @param array $elements
49
+     * @param array $config
50
+     */
51
+    public function __construct( array $elements = array(), array $config = array() ) {
52
+        $this->parse_config( $this->config = $config );
53
+
54
+        foreach ( $elements as $element ) {
55
+            $this->add( $element );
56
+        }
57
+    }
58
+
59
+    /**
60
+     * Adds a new element to the Collection.
61
+     *
62
+     * @param mixed $element
63
+     *
64
+     * @throws RuntimeException
65
+     *
66
+     * @return $this
67
+     */
68
+    public function add( $element ) {
69
+        if ( $this->model && is_array( $element ) ) {
70
+            $element = new $this->model( $element );
71
+        }
72
+
73
+        if ( $this->model && ! ( $element instanceof $this->model ) ) {
74
+            throw new RuntimeException;
75
+        }
76
+
77
+        $this->elements[] = $element;
78
+
79
+        return $this;
80
+    }
81
+
82
+    /**
83
+     * Removes an element by index or value.
84
+     *
85
+     * @param mixed $index
86
+     *
87
+     * @return $this
88
+     */
89
+    public function remove( $index ) {
90
+        if ( ! is_string( $index ) || ! is_numeric( $index ) || ! isset( $this->elements[ $index ] ) ) {
91
+            foreach ( $this->elements as $key => $element ) {
92
+                if ( $element === $index ) {
93
+                    $index = $key;
94
+                    break;
95
+                }
96
+            }
97
+        }
98
+
99
+        if ( isset( $this->elements[ $index ] ) ) {
100
+            unset( $this->elements[ $index ] );
101
+            $this->elements = array_values( $this->elements );
102
+        }
103
+
104
+        return $this;
105
+    }
106
+
107
+    /**
108
+     * Fetches the element at the provided index.
109
+     *
110
+     * @param int $index
111
+     *
112
+     * @return mixed|null
113
+     */
114
+    public function at( $index ) {
115
+        return isset( $this->elements[ $index ] ) ? $this->elements[ $index ] : null;
116
+    }
117
+
118
+    /**
119
+     * Maps over the Collection's elements,
120
+     *
121
+     * @param callable $callback
122
+     *
123
+     * @return Collection
124
+     */
125
+    public function map( callable $callback ) {
126
+        return new Collection( array_map( $callback, $this->elements ) );
127
+    }
128
+
129
+    /**
130
+     * Filters the Collection's elements.
131
+     *
132
+     * @param callable $callback
133
+     *
134
+     * @return Collection
135
+     */
136
+    public function filter( callable $callback ) {
137
+        return new Collection( array_filter( $this->elements, $callback ) );
138
+    }
139
+
140
+    /**
141
+     * Fetches a single element in the Collection by callback.
142
+     *
143
+     * @param callable $callback
144
+     *
145
+     * @return mixed|null
146
+     */
147
+    public function find( callable $callback ) {
148
+        foreach ( $this->elements as $element ) {
149
+            if ( call_user_func( $callback, $element ) ) {
150
+                return $element;
151
+            }
152
+        }
153
+
154
+        return null;
155
+    }
156
+
157
+    /**
158
+     * {@inheritDoc}
159
+     *
160
+     * @return array
161
+     */
162
+    public function serialize() {
163
+        return array_map(function( $element ) {
164
+            if ( $element instanceof Serializes ) {
165
+                return $element->serialize();
166
+            }
167
+
168
+            return $element;
169
+        }, $this->elements);
170
+    }
171
+
172
+    /**
173
+     * Return the current element.
174
+     *
175
+     * @return mixed
176
+     */
177
+    public function current() {
178
+        return $this->at( $this->position );
179
+    }
180
+
181
+    /**
182
+     * Move forward to next element.
183
+     */
184
+    public function next() {
185
+        $this->position ++;
186
+    }
187
+
188
+    /**
189
+     * Return the key of the current element.
190
+     *
191
+     * @return mixed
192
+     */
193
+    public function key() {
194
+        return $this->position;
195
+    }
196
+
197
+    /**
198
+     * Checks if current position is valid.
199
+     *
200
+     * @return bool
201
+     */
202
+    public function valid() {
203
+        return isset( $this->elements[ $this->position ] );
204
+    }
205
+
206
+    /**
207
+     * Rewind the Iterator to the first element.
208
+     */
209
+    public function rewind() {
210
+        $this->position = 0;
211
+    }
212
+
213
+    /**
214
+     * Count elements of an object.
215
+     *
216
+     * @return int
217
+     */
218
+    public function count() {
219
+        return count( $this->elements );
220
+    }
221
+
222
+    /**
223
+     * Parses the Collection config to set its properties.
224
+     *
225
+     * @param array $config
226
+     *
227
+     * @throws LogicException
228
+     */
229
+    protected function parse_config( array $config ) {
230
+        if ( isset( $config['model'] ) ) {
231
+            $model = $config['model'];
232
+
233
+            if ( ! is_subclass_of( $model, 'Intraxia\Jaxion\Axolotl\Model' ) ) {
234
+                throw new LogicException;
235
+            }
236
+
237
+            $this->model = $model;
238
+        }
239
+    }
240 240
 }
Please login to merge, or discard this patch.
Spacing   +33 added lines, -33 removed lines patch added patch discarded remove patch
@@ -48,11 +48,11 @@  discard block
 block discarded – undo
48 48
 	 * @param array $elements
49 49
 	 * @param array $config
50 50
 	 */
51
-	public function __construct( array $elements = array(), array $config = array() ) {
52
-		$this->parse_config( $this->config = $config );
51
+	public function __construct(array $elements = array(), array $config = array()) {
52
+		$this->parse_config($this->config = $config);
53 53
 
54
-		foreach ( $elements as $element ) {
55
-			$this->add( $element );
54
+		foreach ($elements as $element) {
55
+			$this->add($element);
56 56
 		}
57 57
 	}
58 58
 
@@ -65,12 +65,12 @@  discard block
 block discarded – undo
65 65
 	 *
66 66
 	 * @return $this
67 67
 	 */
68
-	public function add( $element ) {
69
-		if ( $this->model && is_array( $element ) ) {
70
-			$element = new $this->model( $element );
68
+	public function add($element) {
69
+		if ($this->model && is_array($element)) {
70
+			$element = new $this->model($element);
71 71
 		}
72 72
 
73
-		if ( $this->model && ! ( $element instanceof $this->model ) ) {
73
+		if ($this->model && !($element instanceof $this->model)) {
74 74
 			throw new RuntimeException;
75 75
 		}
76 76
 
@@ -86,19 +86,19 @@  discard block
 block discarded – undo
86 86
 	 *
87 87
 	 * @return $this
88 88
 	 */
89
-	public function remove( $index ) {
90
-		if ( ! is_string( $index ) || ! is_numeric( $index ) || ! isset( $this->elements[ $index ] ) ) {
91
-			foreach ( $this->elements as $key => $element ) {
92
-				if ( $element === $index ) {
89
+	public function remove($index) {
90
+		if (!is_string($index) || !is_numeric($index) || !isset($this->elements[$index])) {
91
+			foreach ($this->elements as $key => $element) {
92
+				if ($element === $index) {
93 93
 					$index = $key;
94 94
 					break;
95 95
 				}
96 96
 			}
97 97
 		}
98 98
 
99
-		if ( isset( $this->elements[ $index ] ) ) {
100
-			unset( $this->elements[ $index ] );
101
-			$this->elements = array_values( $this->elements );
99
+		if (isset($this->elements[$index])) {
100
+			unset($this->elements[$index]);
101
+			$this->elements = array_values($this->elements);
102 102
 		}
103 103
 
104 104
 		return $this;
@@ -111,8 +111,8 @@  discard block
 block discarded – undo
111 111
 	 *
112 112
 	 * @return mixed|null
113 113
 	 */
114
-	public function at( $index ) {
115
-		return isset( $this->elements[ $index ] ) ? $this->elements[ $index ] : null;
114
+	public function at($index) {
115
+		return isset($this->elements[$index]) ? $this->elements[$index] : null;
116 116
 	}
117 117
 
118 118
 	/**
@@ -122,8 +122,8 @@  discard block
 block discarded – undo
122 122
 	 *
123 123
 	 * @return Collection
124 124
 	 */
125
-	public function map( callable $callback ) {
126
-		return new Collection( array_map( $callback, $this->elements ) );
125
+	public function map(callable $callback) {
126
+		return new Collection(array_map($callback, $this->elements));
127 127
 	}
128 128
 
129 129
 	/**
@@ -133,8 +133,8 @@  discard block
 block discarded – undo
133 133
 	 *
134 134
 	 * @return Collection
135 135
 	 */
136
-	public function filter( callable $callback ) {
137
-		return new Collection( array_filter( $this->elements, $callback ) );
136
+	public function filter(callable $callback) {
137
+		return new Collection(array_filter($this->elements, $callback));
138 138
 	}
139 139
 
140 140
 	/**
@@ -144,9 +144,9 @@  discard block
 block discarded – undo
144 144
 	 *
145 145
 	 * @return mixed|null
146 146
 	 */
147
-	public function find( callable $callback ) {
148
-		foreach ( $this->elements as $element ) {
149
-			if ( call_user_func( $callback, $element ) ) {
147
+	public function find(callable $callback) {
148
+		foreach ($this->elements as $element) {
149
+			if (call_user_func($callback, $element)) {
150 150
 				return $element;
151 151
 			}
152 152
 		}
@@ -160,8 +160,8 @@  discard block
 block discarded – undo
160 160
 	 * @return array
161 161
 	 */
162 162
 	public function serialize() {
163
-		return array_map(function( $element ) {
164
-			if ( $element instanceof Serializes ) {
163
+		return array_map(function($element) {
164
+			if ($element instanceof Serializes) {
165 165
 				return $element->serialize();
166 166
 			}
167 167
 
@@ -175,14 +175,14 @@  discard block
 block discarded – undo
175 175
 	 * @return mixed
176 176
 	 */
177 177
 	public function current() {
178
-		return $this->at( $this->position );
178
+		return $this->at($this->position);
179 179
 	}
180 180
 
181 181
 	/**
182 182
 	 * Move forward to next element.
183 183
 	 */
184 184
 	public function next() {
185
-		$this->position ++;
185
+		$this->position++;
186 186
 	}
187 187
 
188 188
 	/**
@@ -200,7 +200,7 @@  discard block
 block discarded – undo
200 200
 	 * @return bool
201 201
 	 */
202 202
 	public function valid() {
203
-		return isset( $this->elements[ $this->position ] );
203
+		return isset($this->elements[$this->position]);
204 204
 	}
205 205
 
206 206
 	/**
@@ -216,7 +216,7 @@  discard block
 block discarded – undo
216 216
 	 * @return int
217 217
 	 */
218 218
 	public function count() {
219
-		return count( $this->elements );
219
+		return count($this->elements);
220 220
 	}
221 221
 
222 222
 	/**
@@ -226,11 +226,11 @@  discard block
 block discarded – undo
226 226
 	 *
227 227
 	 * @throws LogicException
228 228
 	 */
229
-	protected function parse_config( array $config ) {
230
-		if ( isset( $config['model'] ) ) {
229
+	protected function parse_config(array $config) {
230
+		if (isset($config['model'])) {
231 231
 			$model = $config['model'];
232 232
 
233
-			if ( ! is_subclass_of( $model, 'Intraxia\Jaxion\Axolotl\Model' ) ) {
233
+			if (!is_subclass_of($model, 'Intraxia\Jaxion\Axolotl\Model')) {
234 234
 				throw new LogicException;
235 235
 			}
236 236
 
Please login to merge, or discard this patch.