|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ahc\Underscore; |
| 4 | + |
| 5 | +trait UnderscoreAliases |
| 6 | +{ |
| 7 | + /** |
| 8 | + * Alias of first(). |
| 9 | + * |
| 10 | + * @param int $n |
| 11 | + * |
| 12 | + * @return array|mixed With n = 1 (default), it gives one item, which may not be array. |
| 13 | + */ |
| 14 | + public function head($n = 1) |
| 15 | + { |
| 16 | + return $this->first($n); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Alias of first(). |
| 21 | + * |
| 22 | + * @param int $n |
| 23 | + * |
| 24 | + * @return array|mixed With n = 1 (default), it gives one item, which may not be array. |
| 25 | + */ |
| 26 | + public function take($n = 1) |
| 27 | + { |
| 28 | + return $this->first($n); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Alias of last(). |
| 33 | + * |
| 34 | + * @param int $n |
| 35 | + * |
| 36 | + * @return array|mixed With n = 1 (default), it gives one item, which may not be array. |
| 37 | + */ |
| 38 | + public function tail($n = 1) |
| 39 | + { |
| 40 | + return $this->last($n); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Alias of last(). |
| 45 | + * |
| 46 | + * @param int $n |
| 47 | + * |
| 48 | + * @return array|mixed With n = 1 (default), it gives one item, which may not be array. |
| 49 | + */ |
| 50 | + public function drop($n = 1) |
| 51 | + { |
| 52 | + return $this->last($n); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Alias of unique(). |
| 57 | + * |
| 58 | + * @param callable|string $fn The callback. String is resolved to value of that index. |
| 59 | + * |
| 60 | + * @return self |
| 61 | + */ |
| 62 | + public function uniq($fn = null) |
| 63 | + { |
| 64 | + return $this->unique($fn); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Alias of difference(). |
| 69 | + * |
| 70 | + * @param array|mixed $data Array or array like or array convertible. |
| 71 | + * |
| 72 | + * @return self |
| 73 | + */ |
| 74 | + public function without($data) |
| 75 | + { |
| 76 | + return $this->difference($data); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Alias of map(). |
| 81 | + * |
| 82 | + * @param callable $fn The callback. |
| 83 | + * |
| 84 | + * @return self |
| 85 | + */ |
| 86 | + public function collect(callable $fn) |
| 87 | + { |
| 88 | + return $this->map($fn); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Alias of reduce(). |
| 93 | + * |
| 94 | + * @param callable $fn The callback. |
| 95 | + * @param mixed $memo The initial value carried over to each iteration and returned finally. |
| 96 | + * |
| 97 | + * @return mixed |
| 98 | + */ |
| 99 | + public function foldl(callable $fn, $memo) |
| 100 | + { |
| 101 | + return $this->reduce($fn, $memo); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Alias of reduce(). |
| 106 | + * |
| 107 | + * @param callable $fn The callback. |
| 108 | + * @param mixed $memo The initial value carried over to each iteration and returned finally. |
| 109 | + * |
| 110 | + * @return mixed |
| 111 | + */ |
| 112 | + public function inject(callable $fn, $memo) |
| 113 | + { |
| 114 | + return $this->reduce($fn, $memo); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Alias of reduceRight(). |
| 119 | + * |
| 120 | + * @param callable $fn The callback. |
| 121 | + * @param mixed $memo The initial value carried over to each iteration and returned finally. |
| 122 | + * |
| 123 | + * @return mixed |
| 124 | + */ |
| 125 | + public function foldr(callable $fn, $memo) |
| 126 | + { |
| 127 | + return $this->reduceRight($fn, $memo); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Alias of find(). |
| 132 | + * |
| 133 | + * @param callable $fn The truth test callback. |
| 134 | + * @param bool $useValue Whether to return value or the index on match. |
| 135 | + * |
| 136 | + * @return mixed|null |
| 137 | + */ |
| 138 | + public function detect(callable $fn) |
| 139 | + { |
| 140 | + return $this->find($fn); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Alias of filter(). |
| 145 | + * |
| 146 | + * @param callable|string|null $fn The truth test callback. |
| 147 | + * |
| 148 | + * @return self |
| 149 | + */ |
| 150 | + public function select(callable $fn = null) |
| 151 | + { |
| 152 | + return $this->filter($fn); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Alias of every(). |
| 157 | + * |
| 158 | + * @param callable $fn The truth test callback. |
| 159 | + * |
| 160 | + * @return bool |
| 161 | + */ |
| 162 | + public function all(callable $fn) |
| 163 | + { |
| 164 | + return $this->every($fn); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Alias of some(). |
| 169 | + * |
| 170 | + * @param callable $fn The truth test callback. |
| 171 | + * |
| 172 | + * @return bool |
| 173 | + */ |
| 174 | + public function any(callable $fn) |
| 175 | + { |
| 176 | + return $this->some($fn); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Alias of contains(). |
| 181 | + */ |
| 182 | + public function includes($item) |
| 183 | + { |
| 184 | + return $this->contains($item); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Alias of count(). |
| 189 | + * |
| 190 | + * @return int |
| 191 | + */ |
| 192 | + public function size() |
| 193 | + { |
| 194 | + return $this->count(); |
| 195 | + } |
| 196 | +} |
0 commit comments