| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author Boudewijn Schoon <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright Zicht Online <http://zicht.nl> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace Zicht\Itertools\lib; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Zicht\Itertools\lib\Interfaces\FiniteIterableInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Zicht\Itertools\lib\Traits\FiniteIterableTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | // todo: place the two classed in their own file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Class GroupedIterator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package Zicht\Itertools\lib | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class GroupedIterator extends \IteratorIterator implements FiniteIterableInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     use FiniteIterableTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var mixed */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $groupKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * GroupedIterator constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param mixed $groupKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 15 |  |     public function __construct($groupKey) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 15 |  |         $this->groupKey = $groupKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 15 |  |         parent::__construct(new \ArrayIterator()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 15 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 13 |  |     public function getGroupKey() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 13 |  |         return $this->groupKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * Adds an element to the iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param mixed $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 15 |  |     public function append($key, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 15 |  |         $this->getInnerIterator()->append([$key, $value]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 15 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @{inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 13 |  |     public function current() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 13 |  |         return $this->getInnerIterator()->current()[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @{inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 13 |  |     public function key() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 13 |  |         return $this->getInnerIterator()->current()[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @{inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 11 |  |     public function count() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 11 |  |         return iterator_count($this->getInnerIterator()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  * Class GroupbyIterator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  * @package Zicht\Itertools\lib | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | class GroupbyIterator extends \IteratorIterator implements FiniteIterableInterface | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     use FiniteIterableTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * GroupbyIterator constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      * @param \Closure $func | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      * @param \Iterator $iterable | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 15 |  |     public function __construct(\Closure $func, \Iterator $iterable) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         // todo: this implementation pre-computes everything... this is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         // not the way an iterator should work.  Please re-write. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 15 |  |         $groupedIterator = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 15 |  |         $previousGroupKey = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 15 |  |         $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 15 |  |         foreach ($iterable as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 15 |  |             $groupKey = call_user_func($func, $value, $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 15 |  |             if ($previousGroupKey !== $groupKey || $groupedIterator === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 15 |  |                 $previousGroupKey = $groupKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 15 |  |                 $groupedIterator = new GroupedIterator($groupKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 15 |  |                 $data [] = $groupedIterator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 15 |  |             $groupedIterator->append($key, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 15 |  |         parent::__construct(new \ArrayIterator($data)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 | 15 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |      * @{inheritDoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 13 |  |     public function key() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 13 |  |         return $this->current()->getGroupKey(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |      * @{inheritDoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 11 |  |     public function count() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 11 |  |         return iterator_count($this->getInnerIterator()); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |      * @{inheritDoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 1 |  |     public function toArray() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 1 |  |         $array = iterator_to_array($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 1 |  |         array_walk( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             $array, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 1 |  |             function (&$value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                 /** @var GroupedIterator $value */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 1 |  |                 $value = $value->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 1 |  |         return $array; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 148 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 149 |  |  |  | 
            
                        
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: