o2system /
spl
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * This file is part of the O2System Framework package. |
||||
| 4 | * |
||||
| 5 | * For the full copyright and license information, please view the LICENSE |
||||
| 6 | * file that was distributed with this source code. |
||||
| 7 | * |
||||
| 8 | * @author Steeve Andrian Salim |
||||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
| 10 | */ |
||||
| 11 | |||||
| 12 | // ------------------------------------------------------------------------ |
||||
| 13 | |||||
| 14 | namespace O2System\Spl\DataStructures\Traits; |
||||
| 15 | |||||
| 16 | // ------------------------------------------------------------------------ |
||||
| 17 | |||||
| 18 | use O2System\Spl\DataStructures\SplArrayObject; |
||||
| 19 | |||||
| 20 | /** |
||||
| 21 | * Trait ArrayFunctionsTrait |
||||
| 22 | * |
||||
| 23 | * Add re-usable SplArray Classes manipulations. |
||||
| 24 | * |
||||
| 25 | * @package O2System\Spl\Traits |
||||
| 26 | */ |
||||
| 27 | trait ArrayFunctionsTrait |
||||
| 28 | {
|
||||
| 29 | /** |
||||
| 30 | * ArrayFunctionsTrait::getCombine |
||||
| 31 | * |
||||
| 32 | * Creates an SplArrayObject by giving keys of the array copy. |
||||
| 33 | * |
||||
| 34 | * @see http://php.net/manual/en/function.array-combine.php |
||||
| 35 | * |
||||
| 36 | * @param array $keys Array of keys to be used. Illegal values for key will be converted to string. |
||||
| 37 | * |
||||
| 38 | * @return SplArrayObject |
||||
| 39 | */ |
||||
| 40 | public function getCombine(array $keys) |
||||
| 41 | {
|
||||
| 42 | $arrayCombine = array_combine($keys, $this->getArrayCopy()); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 43 | |||||
| 44 | return new SplArrayObject($arrayCombine); |
||||
|
0 ignored issues
–
show
It seems like
$arrayCombine can also be of type false; however, parameter $array of O2System\Spl\DataStructu...ayObject::__construct() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 45 | } |
||||
| 46 | |||||
| 47 | // ------------------------------------------------------------------------ |
||||
| 48 | |||||
| 49 | /** |
||||
| 50 | * ArrayFunctionsTrait::getKeys |
||||
| 51 | * |
||||
| 52 | * Return all the keys or a subset of the array copy. |
||||
| 53 | * |
||||
| 54 | * @see http://php.net/manual/en/function.array-keys.php |
||||
| 55 | * |
||||
| 56 | * @param mixed $searchValue If specified, then only keys containing these values are returned. |
||||
| 57 | * @param bool $strict Determines if strict comparison (===) should be used during the search. |
||||
| 58 | * |
||||
| 59 | * @return array Returns an array of all the keys of the array copy. |
||||
| 60 | */ |
||||
| 61 | public function getKeys($searchValue = null, $strict = false) |
||||
| 62 | {
|
||||
| 63 | if (isset($searchValue)) {
|
||||
| 64 | return array_keys($this->getArrayCopy(), $searchValue, $strict); |
||||
| 65 | } |
||||
| 66 | |||||
| 67 | return array_keys($this->getArrayCopy()); |
||||
| 68 | } |
||||
| 69 | |||||
| 70 | // ------------------------------------------------------------------------ |
||||
| 71 | |||||
| 72 | /** |
||||
| 73 | * ArrayFunctionsTrait::getValues |
||||
| 74 | * |
||||
| 75 | * Return all the values of the array copy |
||||
| 76 | * |
||||
| 77 | * @see http://php.net/manual/en/function.array-values.php |
||||
| 78 | * |
||||
| 79 | * @return array Returns an indexed array of values. |
||||
| 80 | */ |
||||
| 81 | public function getValues() |
||||
| 82 | {
|
||||
| 83 | return array_values($this->getArrayCopy()); |
||||
| 84 | } |
||||
| 85 | |||||
| 86 | // ------------------------------------------------------------------------ |
||||
| 87 | |||||
| 88 | /** |
||||
| 89 | * ArrayFunctionsTrait::getSlice |
||||
| 90 | * |
||||
| 91 | * Return sliced array of storage array. |
||||
| 92 | * |
||||
| 93 | * @see http://php.net/manual/en/function.array-slice.php |
||||
| 94 | * |
||||
| 95 | * @param int $offset If offset is non-negative, the sequence will start at that offset in the array. If |
||||
| 96 | * offset is negative, the sequence will start that far from the end of the array. |
||||
| 97 | * |
||||
| 98 | * @param int $length If length is given and is positive, then the sequence will have up to that many |
||||
| 99 | * elements in it. If the array is shorter than the length, then only the available array |
||||
| 100 | * elements will be present. If length is given and is negative then the sequence will |
||||
| 101 | * stop that many elements from the end of the array. If it is omitted, then the sequence |
||||
| 102 | * will have everything from offset up until the end of the array. |
||||
| 103 | * |
||||
| 104 | * @param bool $preserveKeys Note that array_slice() will reorder and reset the numeric array indices by default. |
||||
| 105 | * You can change this behaviour by setting preserve_keys to TRUE. |
||||
| 106 | * |
||||
| 107 | * @return array Returns the slice. If the offset is larger than the size of the array then returns an empty array. |
||||
| 108 | */ |
||||
| 109 | public function getSlice($offset = 0, $length = null, $preserveKeys = false) |
||||
| 110 | {
|
||||
| 111 | return array_slice($this->getArrayCopy(), $offset, $length, $preserveKeys); |
||||
| 112 | } |
||||
| 113 | |||||
| 114 | // ------------------------------------------------------------------------ |
||||
| 115 | |||||
| 116 | /** |
||||
| 117 | * ArrayFunctionsTrait::getSlices |
||||
| 118 | * |
||||
| 119 | * Extract the slices of the array copy. |
||||
| 120 | * |
||||
| 121 | * @see http://php.net/manual/en/function.array-slice.php |
||||
| 122 | * |
||||
| 123 | * @param array $lengths Array of lengths |
||||
| 124 | * |
||||
| 125 | * @param bool $preserveKeys Note that array_slice() will reorder and reset the numeric array indices by default. |
||||
| 126 | * You can change this behaviour by setting preserve_keys to TRUE. |
||||
| 127 | * |
||||
| 128 | * @return array Returns the slices. If the offset is larger than the size of the array then returns an empty array. |
||||
| 129 | */ |
||||
| 130 | public function getSlices(array $lengths, $preserveKeys = false) |
||||
| 131 | {
|
||||
| 132 | $arraySlices = []; |
||||
| 133 | |||||
| 134 | foreach ($lengths as $key => $length) {
|
||||
| 135 | $arraySlices[ $key ] = array_slice($this->getArrayCopy(), 0, $length, $preserveKeys); |
||||
| 136 | } |
||||
| 137 | |||||
| 138 | return $arraySlices; |
||||
| 139 | } |
||||
| 140 | |||||
| 141 | // ------------------------------------------------------------------------ |
||||
| 142 | |||||
| 143 | /** |
||||
| 144 | * ArrayFunctionsTrait::getChunk |
||||
| 145 | * |
||||
| 146 | * Return chunk array of the array copy. |
||||
| 147 | * |
||||
| 148 | * @see http://php.net/manual/en/function.array-chunk.php |
||||
| 149 | * |
||||
| 150 | * @param int $size The size of each chunk |
||||
| 151 | * @param bool $preserveKeys When set to TRUE keys will be preserved. Default is FALSE which will reindex the chunk |
||||
| 152 | * numerically |
||||
| 153 | * |
||||
| 154 | * @return array Returns a multidimensional numerically indexed array, starting with zero, with each dimension |
||||
| 155 | * containing size elements. |
||||
| 156 | */ |
||||
| 157 | public function getChunk($size, $preserveKeys = false) |
||||
| 158 | {
|
||||
| 159 | return array_chunk($this->getArrayCopy(), $size, $preserveKeys); |
||||
| 160 | } |
||||
| 161 | |||||
| 162 | // ------------------------------------------------------------------------ |
||||
| 163 | |||||
| 164 | /** |
||||
| 165 | * ArrayFunctionsTrait::getChunks |
||||
| 166 | * |
||||
| 167 | * Return chunks array of the array copy. |
||||
| 168 | * |
||||
| 169 | * @see http://php.net/manual/en/function.array-chunk.php |
||||
| 170 | * |
||||
| 171 | * @param array $sizes Array list of sizes of each chunk |
||||
| 172 | * @param bool $preserveKeys When set to TRUE keys will be preserved. Default is FALSE which will reindex the |
||||
| 173 | * chunk numerically |
||||
| 174 | * |
||||
| 175 | * @return array Returns a multidimensional numerically indexed array, starting with zero, with each dimension |
||||
| 176 | * containing size elements. |
||||
| 177 | */ |
||||
| 178 | public function getChunks(array $sizes, $preserveKeys = false) |
||||
| 179 | {
|
||||
| 180 | $arrayChunks = []; |
||||
| 181 | |||||
| 182 | $offset = 0; |
||||
| 183 | foreach ($sizes as $key => $limit) {
|
||||
| 184 | $arrayChunks[ $key ] = array_slice($this->getArrayCopy(), $offset, $limit, $preserveKeys); |
||||
| 185 | $offset = $limit; |
||||
| 186 | } |
||||
| 187 | |||||
| 188 | return $arrayChunks; |
||||
| 189 | } |
||||
| 190 | |||||
| 191 | // ------------------------------------------------------------------------ |
||||
| 192 | |||||
| 193 | /** |
||||
| 194 | * ArrayFunctionsTrait::getShuffle |
||||
| 195 | * |
||||
| 196 | * Shuffle the array copy. |
||||
| 197 | * |
||||
| 198 | * @see http://php.net/manual/en/function.shuffle.php |
||||
| 199 | * |
||||
| 200 | * @return array Return shuffle array of the array copy. |
||||
| 201 | */ |
||||
| 202 | public function getShuffle() |
||||
| 203 | {
|
||||
| 204 | $arrayCopy = $this->getArrayCopy(); |
||||
| 205 | shuffle($arrayCopy); |
||||
| 206 | |||||
| 207 | return $arrayCopy; |
||||
| 208 | } |
||||
| 209 | |||||
| 210 | // ------------------------------------------------------------------------ |
||||
| 211 | |||||
| 212 | /** |
||||
| 213 | * ArrayFunctionsTrait::getReverse |
||||
| 214 | * |
||||
| 215 | * Return the array copy with elements in reverse order. |
||||
| 216 | * |
||||
| 217 | * @param bool $preserveKey If set to TRUE numeric keys are preserved. Non-numeric keys are not affected by this |
||||
| 218 | * setting and will always be preserved. |
||||
| 219 | * |
||||
| 220 | * @return array Returns the reversed array. |
||||
| 221 | */ |
||||
| 222 | public function getReverse($preserveKey = false) |
||||
| 223 | {
|
||||
| 224 | return array_reverse($this->getArrayCopy(), $preserveKey); |
||||
| 225 | } |
||||
| 226 | |||||
| 227 | // ------------------------------------------------------------------------ |
||||
| 228 | |||||
| 229 | /** |
||||
| 230 | * ArrayFunctionsTrait::getColumn |
||||
| 231 | * |
||||
| 232 | * Return the values from a single column of the array copy. |
||||
| 233 | * |
||||
| 234 | * @see http://php.net/manual/en/function.array-column.php |
||||
| 235 | * |
||||
| 236 | * @param mixed $columnKey The column of values to return. This value may be an integer key of the column you wish |
||||
| 237 | * to retrieve, or it may be a string key name for an associative array or property name. |
||||
| 238 | * It may also be NULL to return complete arrays or objects (this is useful together with |
||||
| 239 | * index_key to reindex the array). |
||||
| 240 | * @param mixed $indexKey The column to use as the index/keys for the returned array. This value may be the |
||||
| 241 | * integer key of the column, or it may be the string key name. |
||||
| 242 | * |
||||
| 243 | * @return array Returns an array of values representing a single column from the input array. |
||||
| 244 | */ |
||||
| 245 | public function getColumn($columnKey, $indexKey = null) |
||||
| 246 | {
|
||||
| 247 | return array_column($this->getArrayCopy(), $columnKey, $indexKey); |
||||
| 248 | } |
||||
| 249 | |||||
| 250 | // ------------------------------------------------------------------------ |
||||
| 251 | |||||
| 252 | /** |
||||
| 253 | * ArrayFunctionsTrait::getFlip |
||||
| 254 | * |
||||
| 255 | * Exchanges all keys with their associated values of the array copy. |
||||
| 256 | * |
||||
| 257 | * @see http://php.net/manual/en/function.array-flip.php |
||||
| 258 | * |
||||
| 259 | * @return array Returns the flipped array on success and NULL on failure. |
||||
| 260 | */ |
||||
| 261 | public function getFlip() |
||||
| 262 | {
|
||||
| 263 | return array_flip($this->getArrayCopy()); |
||||
| 264 | } |
||||
| 265 | |||||
| 266 | // ------------------------------------------------------------------------ |
||||
| 267 | |||||
| 268 | /** |
||||
| 269 | * ArrayFunctionsTrait::filter |
||||
| 270 | * |
||||
| 271 | * Filters elements of the array copy using a callback function. |
||||
| 272 | * |
||||
| 273 | * @see http://php.net/manual/en/function.array-filter.php |
||||
| 274 | * |
||||
| 275 | * @param callable $callback The callback function to use, if no callback is supplied, all entries of array equal |
||||
| 276 | * to FALSE (see converting to boolean) will be removed. |
||||
| 277 | * @param int $flag Flag determining what arguments are sent to callback. |
||||
| 278 | * |
||||
| 279 | * @return array Returns the filtered array. |
||||
| 280 | */ |
||||
| 281 | public function filter($callback, $flag = 0) |
||||
| 282 | {
|
||||
| 283 | return array_filter($this->getArrayCopy(), $callback, $flag); |
||||
| 284 | } |
||||
| 285 | |||||
| 286 | // ------------------------------------------------------------------------ |
||||
| 287 | |||||
| 288 | /** |
||||
| 289 | * ArrayFunctionsTrait::getSum |
||||
| 290 | * |
||||
| 291 | * Calculate the sum of values of the array copy. |
||||
| 292 | * |
||||
| 293 | * @see http://php.net/manual/en/function.array-sum.php |
||||
| 294 | * |
||||
| 295 | * @return int|float Returns the sum of values as an integer or float. |
||||
| 296 | */ |
||||
| 297 | public function getSum() |
||||
| 298 | {
|
||||
| 299 | return array_sum($this->getArrayCopy()); |
||||
| 300 | } |
||||
| 301 | |||||
| 302 | // ------------------------------------------------------------------------ |
||||
| 303 | |||||
| 304 | /** |
||||
| 305 | * ArrayFunctionsTrait::getCountValues |
||||
| 306 | * |
||||
| 307 | * Counts all the values of the array copy. |
||||
| 308 | * |
||||
| 309 | * @see http://php.net/manual/en/function.array-count-values.php |
||||
| 310 | * |
||||
| 311 | * @return array Returns an associative array of values from array as keys and their count as value. |
||||
| 312 | */ |
||||
| 313 | public function getCountValues() |
||||
| 314 | {
|
||||
| 315 | return array_count_values($this->getArrayCopy()); |
||||
| 316 | } |
||||
| 317 | } |