| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author Boudewijn Schoon <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright Zicht Online <http://zicht.nl> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace Zicht\Itertools\conversions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Doctrine\Common\Collections\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Zicht\Itertools\lib\StringIterator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Transforms anything into an Iterator or throws an InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * > mixedToIterator([1, 2, 3]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * 1 2 3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * > mixedToIterator('foo') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * f o o | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @param array|string|\Iterator $iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @return \Iterator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | function mixed_to_iterator($iterable) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     // NULL is often used to indicate that nothing is there, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     // for robustness we will deal with NULL as it is an empty array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 349 |  |     if (is_null($iterable)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 4 |  |         $iterable = new \ArrayIterator([]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     // an array is *not* an instance of Traversable (as it is not an | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     // object and hence can not 'implement Traversable') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 349 |  |     if (is_array($iterable)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 279 |  |         $iterable = new \ArrayIterator($iterable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 279 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     // a string is considered iterable in Python | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 349 |  |     if (is_string($iterable)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 5 |  |         $iterable = new StringIterator($iterable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     // a doctrine Collection (i.e. Array or Persistent) is also an iterator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     if ($iterable instanceof Collection) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 349 |  |         $iterable = $iterable->getIterator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     if ($iterable instanceof \Traversable and !($iterable instanceof \Iterator)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $iterable = new \IteratorIterator($iterable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 349 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     // by now it should be an Iterator, otherwise throw an exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     if (!($iterable instanceof \Iterator)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         throw new \InvalidArgumentException('Argument $ITERABLE must be a Traversable'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 349 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 49 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     return $iterable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 300 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  * Try to transforms something into a Closure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  * When $CLOSURE is null the returned Closure behaves like an identity function, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  * i.e. it will return the value that it is given. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  * @param null|\Closure $closure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  * @return \Closure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | function mixed_to_closure($closure) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     if (is_null($closure)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return function ($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 207 |  |             return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 58 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 85 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     if (!($closure instanceof \Closure)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 127 |  |         // A \Closure is always callable, but a callable is not always a \Closure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         // Checking within this if statement is a slight optimization, preventing an unnecessary function wrap | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if (is_callable($closure)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $closure = function () use ($closure) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 12 |  |                 return call_user_func_array($closure, func_get_args()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             throw new \InvalidArgumentException('Argument $CLOSURE must be a Closure'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 12 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     return $closure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 115 |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  * Try to transforms something into a Closure that gets a value from $STRATEGY. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  * When $STRATEGY is null the returned Closure behaves like an identity function, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  * i.e. it will return the value that it is given. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  * When $STRATEGY is a string the returned Closure tries to find a properties, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  * methods, or array indexes named by the string.  Multiple property, method, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  * or index names can be separated by a dot. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  * - 'getId' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  * - 'getData.key' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  * When $STRATEGY is callable it is converted into a Closure (see mixedToClosure). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  * @param null|string|\Closure $strategy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  * @return \Closure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | function mixed_to_value_getter($strategy) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     if (is_string($strategy)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $keyParts = explode('.', $strategy); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $strategy = function ($value) use ($keyParts) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 207 |  |             foreach ($keyParts as $keyPart) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 29 |  |                 if (is_object($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                     // property_exists does not distinguish between public, protected, or private properties, hence we need to use reflection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 29 |  |                     $reflection = new \ReflectionObject($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 29 |  |                     if ($reflection->hasProperty($keyPart)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                         $property = $reflection->getProperty($keyPart); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 13 |  |                         if ($property->isPublic()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 13 |  |                             $value = $property->getValue($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 7 |  |                             continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 7 |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 7 |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 7 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 if (is_callable([$value, $keyPart])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 6 |  |                     $value = call_user_func([$value, $keyPart]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 22 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 6 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 6 |  |                 if (is_array($value) && array_key_exists($keyPart, $value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                     $value = $value[$keyPart]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 16 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 16 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 16 |  |                 if (is_object($value) && method_exists($value, '__get')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                     $value = $value->$keyPart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 4 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                 // no match found | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                 $value = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 4 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 4 |  |             return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 29 |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 29 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 29 |  |     return mixed_to_closure($strategy); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 29 |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 207 |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  * Transforms anything into an Iterator or throws an InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  * @param array|string|\Iterator $iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  * @return \Iterator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  * @deprecated Use mixed_to_iterator() instead, will be removed in version 3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | function mixedToIterator($iterable) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     return mixed_to_iterator($iterable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  * Try to transforms something into a Closure. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  * @param null|\Closure $closure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  * @return \Closure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  * @deprecated Use mixed_to_closure() instead, will be removed in version 3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | function mixedToClosure($closure) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     return mixed_to_closure($closure); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  * Try to transforms something into a Closure that gets a value from $STRATEGY. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  * @param null|string|\Closure $strategy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  * @return \Closure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  * @deprecated Use mixed_to_closure() instead, will be removed in version 3.0 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 187 |  |  |  */ | 
            
                                                        
            
                                    
            
            
                | 188 |  |  | function mixedToValueGetter($strategy) | 
            
                                                        
            
                                    
            
            
                | 189 |  |  | { | 
            
                                                        
            
                                    
            
            
                | 190 |  |  |     return mixed_to_value_getter($strategy); | 
            
                                                        
            
                                    
            
            
                | 191 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 192 |  |  |  | 
            
                        
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.