chillerlan /
php-traits
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Trait Enumerable |
||
| 4 | * |
||
| 5 | * @filesource Enumerable.php |
||
| 6 | * @created 28.06.2017 |
||
| 7 | * @package chillerlan\Traits |
||
| 8 | * @author Smiley <[email protected]> |
||
| 9 | * @copyright 2017 Smiley |
||
| 10 | * @license MIT |
||
| 11 | */ |
||
| 12 | |||
| 13 | namespace chillerlan\Traits; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @link http://api.prototypejs.org/language/Enumerable/ |
||
| 17 | */ |
||
| 18 | trait Enumerable{ |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $array = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | protected $offset = 0; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @link http://api.prototypejs.org/language/Enumerable/prototype/toArray/ |
||
| 32 | * |
||
| 33 | * @return array |
||
| 34 | * |
||
| 35 | * @codeCoverageIgnore |
||
| 36 | */ |
||
| 37 | public function __toArray():array { |
||
| 38 | return $this->array; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @link http://api.prototypejs.org/language/Enumerable/prototype/each/ |
||
| 43 | * |
||
| 44 | * @param callable $callback |
||
| 45 | * |
||
| 46 | * @return $this |
||
| 47 | */ |
||
| 48 | public function __each($callback){ |
||
| 49 | $this->__map($callback); |
||
| 50 | |||
| 51 | return $this; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @link http://api.prototypejs.org/language/Enumerable/prototype/collect/ |
||
| 56 | * @link http://api.prototypejs.org/language/Enumerable/prototype/map/ |
||
| 57 | * |
||
| 58 | * @param callable $callback |
||
| 59 | * |
||
| 60 | * @return array |
||
| 61 | * @throws \chillerlan\Traits\TraitException |
||
| 62 | */ |
||
| 63 | public function __map($callback):array { |
||
| 64 | |||
| 65 | if(!\is_callable($callback)){ |
||
| 66 | throw new TraitException('invalid callback'); |
||
| 67 | } |
||
| 68 | |||
| 69 | $return = []; |
||
| 70 | |||
| 71 | foreach($this->array as $index => $element){ |
||
| 72 | $return[$index] = \call_user_func_array($callback, [$element, $index]); |
||
| 73 | } |
||
| 74 | |||
| 75 | return $return; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @link http://api.prototypejs.org/language/Array/prototype/reverse/ |
||
| 80 | * |
||
| 81 | * @return \chillerlan\Traits\EnumerableInterface |
||
| 82 | */ |
||
| 83 | public function __reverse():EnumerableInterface{ |
||
| 84 | $this->array = \array_reverse($this->array); |
||
| 85 | $this->offset = 0; |
||
| 86 | |||
| 87 | return $this; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return mixed |
||
| 92 | */ |
||
| 93 | public function __first(){ |
||
| 94 | return $this->array[0] ?? null; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return mixed |
||
| 99 | */ |
||
| 100 | public function __last(){ |
||
| 101 | return $this->array[\count($this->array) - 1] ?? null; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return \chillerlan\Traits\EnumerableInterface |
||
| 106 | */ |
||
| 107 | public function __clear():EnumerableInterface{ |
||
| 108 | $this->array = []; |
||
| 109 | |||
| 110 | return $this; |
||
|
0 ignored issues
–
show
|
|||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @link http://api.prototypejs.org/language/Array/prototype/inspect/ |
||
| 115 | * |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function __inspect():string { |
||
| 119 | return \print_r($this->array, true); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @link http://api.prototypejs.org/language/Enumerable/prototype/findAll/ |
||
| 124 | * |
||
| 125 | * @param callable $callback |
||
| 126 | * |
||
| 127 | * @return array |
||
| 128 | * @throws \chillerlan\Traits\TraitException |
||
| 129 | */ |
||
| 130 | public function __findAll($callback):array{ |
||
| 131 | |||
| 132 | if(!\is_callable($callback)){ |
||
| 133 | throw new TraitException('invalid callback'); |
||
| 134 | } |
||
| 135 | |||
| 136 | $return = []; |
||
| 137 | |||
| 138 | foreach($this->array as $index => $element){ |
||
| 139 | |||
| 140 | if(\call_user_func_array($callback, [$element, $index]) === true){ |
||
| 141 | $return[] = $element; |
||
| 142 | } |
||
| 143 | |||
| 144 | } |
||
| 145 | |||
| 146 | return $return; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @link http://api.prototypejs.org/language/Enumerable/prototype/reject/ |
||
| 151 | * |
||
| 152 | * @param callable $callback |
||
| 153 | * |
||
| 154 | * @return array |
||
| 155 | * @throws \chillerlan\Traits\TraitException |
||
| 156 | */ |
||
| 157 | public function __reject($callback):array{ |
||
| 158 | |||
| 159 | if(!\is_callable($callback)){ |
||
| 160 | throw new TraitException('invalid callback'); |
||
| 161 | } |
||
| 162 | |||
| 163 | $return = []; |
||
| 164 | |||
| 165 | foreach($this->array as $index => $element){ |
||
| 166 | |||
| 167 | if(\call_user_func_array($callback, [$element, $index]) !== true){ |
||
| 168 | $return[] = $element; |
||
| 169 | } |
||
| 170 | |||
| 171 | } |
||
| 172 | |||
| 173 | return $return; |
||
| 174 | } |
||
| 175 | |||
| 176 | } |
||
| 177 |