| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace WebTheory\Collection\Access\Abstracts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use WebTheory\Collection\Contracts\ArrayDriverInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use WebTheory\Collection\Contracts\ObjectComparatorInterface; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | abstract class AbstractArrayDriver implements ArrayDriverInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |     protected ObjectComparatorInterface $objectComparator; | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 | 187 |  |     public function __construct(ObjectComparatorInterface $objectComparator) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 | 187 |  |         $this->objectComparator = $objectComparator; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 30 |  |     public function fetch(array $array, $item) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 | 30 |  |         return $array[$item]; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 | 187 |  |     public function collect(array &$array, array $items): void | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 187 |  |         foreach ($items as $offset => $item) { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 187 |  |             $this->insert($array, $item, $offset); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 6 |  |     public function remove(array &$array, $item): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 6 |  |         return is_object($item) | 
            
                                                                        
                            
            
                                    
            
            
                | 32 | 4 |  |             ? $this->deleteObjectIfLocated($array, $item) | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 6 |  |             : $this->maybeRemoveItem($array, $item); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 8 |  |     public function contains(array $array, $item): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 8 |  |         return is_object($item) | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 2 |  |             ? $this->arrayContainsObject($array, $item) | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 8 |  |             : $this->arrayContains($array, $item); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 2 |  |     protected function maybeRemoveItem(array &$array, $item): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 2 |  |         if (isset($array[$item])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 2 |  |             unset($array[$item]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 2 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |         return false; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 4 |  |     protected function arrayContains(array $array, $item): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 | 4 |  |         return isset($array[$item]); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 187 |  |     protected function arrayContainsObject(array $array, object $object): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 187 |  |         foreach ($array as $item) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 187 |  |             if ($this->objectComparator->matches($item, $object)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 7 |  |                 return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 187 |  |         return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 4 |  |     protected function deleteObjectIfLocated(array &$array, object $object): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 4 |  |         $position = array_search($object, $array, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 4 |  |         if ($position === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 4 |  |         unset($array[$position]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 | 4 |  |         return true; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 82 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 83 |  |  |  |