@@ -14,210 +14,210 @@ |
||
| 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 |
|
| 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 | - protected function map( callable $callback ) { |
|
| 126 | - return new Collection( array_map( $callback, $this->elements ), $this->config ); |
|
| 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 | - * {@inheritDoc} |
|
| 142 | - * |
|
| 143 | - * @return array |
|
| 144 | - */ |
|
| 145 | - public function serialize() { |
|
| 146 | - return array_map(function( $element ) { |
|
| 147 | - if ( $element instanceof Serializes ) { |
|
| 148 | - return $element->serialize(); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - return $element; |
|
| 152 | - }, $this->elements); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Return the current element. |
|
| 157 | - * |
|
| 158 | - * @return mixed |
|
| 159 | - */ |
|
| 160 | - public function current() { |
|
| 161 | - return $this->at( $this->position ); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Move forward to next element. |
|
| 166 | - */ |
|
| 167 | - public function next() { |
|
| 168 | - $this->position ++; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Return the key of the current element. |
|
| 173 | - * |
|
| 174 | - * @return mixed |
|
| 175 | - */ |
|
| 176 | - public function key() { |
|
| 177 | - return $this->position; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Checks if current position is valid. |
|
| 182 | - * |
|
| 183 | - * @return bool |
|
| 184 | - */ |
|
| 185 | - public function valid() { |
|
| 186 | - return isset( $this->elements[ $this->position ] ); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Rewind the Iterator to the first element. |
|
| 191 | - */ |
|
| 192 | - public function rewind() { |
|
| 193 | - $this->position = 0; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * Count elements of an object. |
|
| 198 | - * |
|
| 199 | - * @return int |
|
| 200 | - */ |
|
| 201 | - public function count() { |
|
| 202 | - return count( $this->elements ); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Parses the Collection config to set its properties. |
|
| 207 | - * |
|
| 208 | - * @param array $config |
|
| 209 | - * |
|
| 210 | - * @throws LogicException |
|
| 211 | - */ |
|
| 212 | - protected function parse_config( array $config ) { |
|
| 213 | - if ( isset( $config['model'] ) ) { |
|
| 214 | - $model = $config['model']; |
|
| 215 | - |
|
| 216 | - if ( ! is_subclass_of( $model, 'Intraxia\Jaxion\Axolotl\Model' ) ) { |
|
| 217 | - throw new LogicException; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - $this->model = $model; |
|
| 221 | - } |
|
| 222 | - } |
|
| 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 |
|
| 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 | + protected function map( callable $callback ) { |
|
| 126 | + return new Collection( array_map( $callback, $this->elements ), $this->config ); |
|
| 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 | + * {@inheritDoc} |
|
| 142 | + * |
|
| 143 | + * @return array |
|
| 144 | + */ |
|
| 145 | + public function serialize() { |
|
| 146 | + return array_map(function( $element ) { |
|
| 147 | + if ( $element instanceof Serializes ) { |
|
| 148 | + return $element->serialize(); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + return $element; |
|
| 152 | + }, $this->elements); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Return the current element. |
|
| 157 | + * |
|
| 158 | + * @return mixed |
|
| 159 | + */ |
|
| 160 | + public function current() { |
|
| 161 | + return $this->at( $this->position ); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Move forward to next element. |
|
| 166 | + */ |
|
| 167 | + public function next() { |
|
| 168 | + $this->position ++; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Return the key of the current element. |
|
| 173 | + * |
|
| 174 | + * @return mixed |
|
| 175 | + */ |
|
| 176 | + public function key() { |
|
| 177 | + return $this->position; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Checks if current position is valid. |
|
| 182 | + * |
|
| 183 | + * @return bool |
|
| 184 | + */ |
|
| 185 | + public function valid() { |
|
| 186 | + return isset( $this->elements[ $this->position ] ); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Rewind the Iterator to the first element. |
|
| 191 | + */ |
|
| 192 | + public function rewind() { |
|
| 193 | + $this->position = 0; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * Count elements of an object. |
|
| 198 | + * |
|
| 199 | + * @return int |
|
| 200 | + */ |
|
| 201 | + public function count() { |
|
| 202 | + return count( $this->elements ); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Parses the Collection config to set its properties. |
|
| 207 | + * |
|
| 208 | + * @param array $config |
|
| 209 | + * |
|
| 210 | + * @throws LogicException |
|
| 211 | + */ |
|
| 212 | + protected function parse_config( array $config ) { |
|
| 213 | + if ( isset( $config['model'] ) ) { |
|
| 214 | + $model = $config['model']; |
|
| 215 | + |
|
| 216 | + if ( ! is_subclass_of( $model, 'Intraxia\Jaxion\Axolotl\Model' ) ) { |
|
| 217 | + throw new LogicException; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + $this->model = $model; |
|
| 221 | + } |
|
| 222 | + } |
|
| 223 | 223 | } |