| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace WebTheory\Collection\Access; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use WebTheory\Collection\Contracts\ArrayDriverInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use WebTheory\Collection\Contracts\CollectionQueryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use WebTheory\Collection\Contracts\ObjectComparatorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use WebTheory\Collection\Contracts\PropertyResolverInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use WebTheory\Collection\Query\BasicQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use WebTheory\Collection\Resolution\Abstracts\ResolvesPropertyValueTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class IdentifiableItemList extends StandardList implements ArrayDriverInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     use ResolvesPropertyValueTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 187 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         string $property, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         PropertyResolverInterface $propertyResolver, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         ObjectComparatorInterface $objectComparator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 187 |  |         $this->property = $property; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 187 |  |         $this->propertyResolver = $propertyResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 187 |  |         $this->objectComparator = $objectComparator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function fetch(array $array, $item) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         return $this->getFirstMatchingItem($array, $item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 2 |  |     public function maybeRemoveItem(array &$array, $item): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 2 |  |         return ($item = $this->getFirstMatchingItem($array, $item)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 2 |  |             ? $this->remove($array, $item) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 2 |  |             : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 2 |  |     protected function arrayContains(array $array, $item): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 2 |  |         return $this->getQuery(...$this->query($item))->match($array); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 2 |  |     protected function getFirstMatchingItem(array $array, $item): ?object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 2 |  |         return $this->getQuery(...$this->query($item))->first($array); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 4 |  |     protected function query($item): array | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 4 |  |         return [$this->property, '=', $item]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 4 |  |     protected function getQuery(string $property, string $operator, $value): CollectionQueryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 4 |  |         return new BasicQuery( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 4 |  |             $property, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 4 |  |             $operator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 4 |  |             $value, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 4 |  |             $this->propertyResolver, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 4 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  |