| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Part of the Fusion.Collection package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @license MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Fusion\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Fusion\Collection\Contracts\AbstractCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Fusion\Collection\Contracts\CollectionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Fusion\Collection\Contracts\CollectionValidationInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * An implementation of a collection. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * A collection holds values internally with a numeric index. The values can consist of any value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * that can be stored in a PHP array that isn't `null`. The internal index grows and shrink with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * the collection as items are removed or added. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * Collections are traversable and can be looped or accessed directly using array index notation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @since 1.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | class Collection extends AbstractCollection implements CollectionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * Instantiates a collection object with an optional array of starter items. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * If the starting items contain any `null` values, an exception will be thrown. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param \Fusion\Collection\Contracts\CollectionValidationInterface $validator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param array $items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @throws \Fusion\Collection\Exceptions\CollectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 63 |  |     public function __construct(CollectionValidationInterface $validator, array $items = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 63 |  |         $this->validator = $validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 63 |  |         foreach ($items as $item) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 14 |  |             $this->add($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 62 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** {@inheritdoc} */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 48 |  |     public function add($value): CollectionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 48 |  |         $this->validator->validateNonNullValue($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 48 |  |         array_push($this->collection, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 48 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** {@inheritdoc} */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 3 |  |     public function replace(int $key, $value): CollectionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 3 |  |         $this->offsetSet($key, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** {@inheritdoc} */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 6 |  |     public function find(int $key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 6 |  |         $this->validator->validateOffsetExists($key, $this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 5 |  |         return $this->offsetGet($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * Retrieves a value at the given offset. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * This method will throw a `CollectionException` if the offset is not an integer or if the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * offset does not exist. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @see \Fusion\Collection\Contracts\AbstractCollection::offsetGet() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @link http://php.net/manual/en/arrayaccess.offsetget.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param mixed $offset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @throws \Fusion\Collection\Exceptions\CollectionException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 14 |  |     public function offsetGet($offset) | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 14 |  |         $this->validator->validateIntValue($offset); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 13 |  |         $this->validator->validateOffsetExists($offset, $this); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 10 |  |         return parent::offsetGet($offset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * Sets a value at the given offset. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * This method will throw a `CollectionException` if the offset does not exist or if the offset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * is not an integer. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @see \Fusion\Collection\Contracts\AbstractCollection::offsetSet() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @link http://php.net/manual/en/arrayaccess.offsetset.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param mixed $offset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @throws \Fusion\Collection\Exceptions\CollectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 8 |  |     public function offsetSet($offset, $value): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 8 |  |         $this->validator->validateIntValue($offset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 7 |  |         $this->validator->validateOffsetExists($offset, $this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 6 |  |         parent::offsetSet($offset, $value); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 116 |  |  | } |