1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
<?php namespace amekusa\plz; main::required;
/**
* Array utilities
*
* To get started, place the following line around top of your code.
* ```php
* use amekusa\plz\arr;
* ```
*/
abstract class arr {
/**
* Returns whether or not `$X` equals `$Y`
*
* Specifically:
*
* + Tests each elements with {@link op}`::eq()`
* + Recurses a multidimensional array
* + Treats an object as an array with {@link type}`::arr()`
*
* @example Comparing arrays
* ```php
* $X = array ('A', 'B', 'C');
* $Y = array ('a', 'b', 'c');
* $Z = array ('A', 'B', 'C');
*
* echo 'Does $X equal to $Y? - ';
* echo arr::eq($X, $Y) ? 'Yes.' : 'No.';
* echo "\n";
* echo 'Does $X equal to $Z? - ';
* echo arr::eq($X, $Z) ? 'Yes.' : 'No.';
* ```
* ```php
* Does $X equal to $Y? - No.
* Does $X equal to $Z? - Yes.
* ```
* @example Comparing array-like objects
* ```php
* $X = new ArrayObject(array ('A', 'B', 'C'));
* $Y = new ArrayObject(array ('a', 'b', 'c'));
* $Z = new ArrayObject(array ('A', 'B', 'C'));
*
* echo 'Does $X equal to $Y? - ';
* echo arr::eq($X, $Y) ? 'Yes.' : 'No.';
* echo "\n";
* echo 'Does $X equal to $Z? - ';
* echo arr::eq($X, $Z) ? 'Yes.' : 'No.';
* ```
* ```php
* Does $X equal to $Y? - No.
* Does $X equal to $Z? - Yes.
* ```
* @param array|object $X A variable to compare with `$Y`
* @param array|object $Y A variable to compare with `$X`
* @return boolean
*/
static function eq($X, $Y) {
if ($X != $Y) return false;
$x = type::arr($X, null);
if (is_null($x)) return false;
$y = type::arr($Y, null);
if (is_null($y)) return false;
if (count($x) != count($y)) return false;
foreach ($x as $i => $iX) {
if (!isset($y[$i])) return false;
if (!op::eq($iX, $y[$i])) return false;
}
return true;
}
/**
* Returns the number of elements in `$X`
*
* Additionally:
*
* + If `$X` is uncountable, `1` is returned.
* + If `$X` is `null`, `0` is returned.
*
* @example Demonstration
* ```php
* $var1 = array ('A', 'B'); // Array
* $var2 = new ArrayObject(array ('A', 'B', 'C')); // Countable object
* $var3 = 'ABCD'; // String
* $var4 = null; // Null
* var_dump( arr::count($var1) );
* var_dump( arr::count($var2) );
* var_dump( arr::count($var3) );
* var_dump( arr::count($var4) );
* ```
* ```php
* int(2)
* int(3)
* int(1)
* int(0)
* ```
* @example Recursively counting
* ```php
* $var = array (
* 'A', 'B',
* array ('C', 'D'),
* 'E', 'F'
* );
* var_dump( arr::count($var) ); // Normal
* var_dump( arr::count($var, true) ); // Recursive
* ```
* ```php
* int(5)
* int(7)
* ```
* @param array|object $X An array, countable object, or iterable object
* @param boolean $Recursive *(optional)* Whether or not to count recursively
* @return integer
*/
static function count($X, $Recursive = false) {
if (is_object($X)) {
if (!$X instanceof \Countable) {
if ($X instanceof \Traversable) { // Computes
$r = 0;
if ($Recursive) foreach ($X as $iX) $r += (1 + arr::count($iX, true));
else foreach ($X as $iX) $r++;
return $r;
}
}
}
return \count($X, $Recursive ? COUNT_RECURSIVE : COUNT_NORMAL);
}
/**
* Returns the first element of `$X`
*
* **CAUTION:** Calling in a `foreach` loop over `$X` can cause unpredictable results.
*
* @example Demonstration
* ```php
* $var1 = array ('A', 'B', 'C'); // Array
* $var2 = new ArrayObject(array ('A', 'B', 'C')); // Iterable object
* var_dump( arr::first($var1) );
* var_dump( arr::first($var2) );
* ```
* ```php
* string(1) "A"
* string(1) "A"
* ```
* @param array|object $X An array or an iterable object
* @return mixed
*/
static function first($X) {
if (is_array($X)) return reset($X);
foreach ($X as $iX) return $iX;
}
/**
* Returns the last element of `$X`
*
* **CAUTION:** Calling this in a `foreach` loop over `$X` can cause unpredictable results.
*
* @example Demonstration
* ```php
* $var1 = array ('A', 'B', 'C'); // Array
* $var2 = new ArrayObject(array ('A', 'B', 'C')); // Iterable object
* var_dump( arr::last($var1) );
* var_dump( arr::last($var2) );
* ```
* ```php
* string(1) "C"
* string(1) "C"
* ```
* @param array|object $X An array or an iterable object
* @return mixed
*/
static function last($X) {
if (is_array($X)) {
$r = end($X);
reset($X);
return $r;
}
foreach ($X as $iX);
return $iX;
}
/**
* Returns whether `$X` has the supplied key
* @example Demonstration
* ```php
* $var = array (
* 'X' => 'A',
* 'Y' => 'B',
* 'Z' => 'C'
* );
* var_dump( arr::has_key($var, 'X') );
* var_dump( arr::has_key($var, 'W') );
* ```
* ```php
* bool(true)
* bool(false)
* ```
* @param array|object $X An array, array-like object, or traversable object
* @param mixed $Key A key to find
* @return boolean
*/
static function has_key($X, $Key) {
if (is_array($X)) return array_key_exists($Key, $X);
if (is_object($X)) {
if ($X instanceof \ArrayAccess) return $X->offsetExists($Key);
if ($X instanceof \Traversable) {
foreach ($X as $i => $iX) {
if ($i === $Key) return true;
}
return false;
}
}
return false;
}
/**
* Returns `$X`’s element indexed by `$Key`
*
* If the element doesn’t exist, returns `$Alt`.
*
* @example Demonstration
* ```php
* $var = array (
* 'X' => 'A',
* 'Y' => 'B',
* 'Z' => 'C'
* );
* var_dump( arr::get($var, 'X') ); // Same as $var['X']
* var_dump( arr::get($var, 'W') ); // Alternates with NULL
* var_dump( arr::get($var, 'W', 'No such key!') ); // Alternates with a string
* ```
* ```php
* string(1) "A"
* NULL
* string(12) "No such key!"
* ```
* @param array|object $X An array, array-like object, or traversable object
* @param mixed $Key The key of an element to be returned
* @param mixed $Alt *(optional)* An alternative value to return if `$X` doesn’t have the key
* @return mixed
*/
static function get($X, $Key, $Alt = null) {
if (is_array($X)) return array_key_exists($Key, $X) ? $X[$Key] : $Alt;
if (is_object($X)) {
if ($X instanceof \ArrayAccess) return $X->offsetExists($Key) ? $X[$Key] : $Alt;
if ($X instanceof \Traversable) {
foreach ($X as $i => $iX) {
if ($i === $Key) return $iX;
}
return $Alt;
}
}
return $Alt;
}
/**
* Treats arguments as an one-dimensional array
* @example Converting a multi-dimensional array into one-dimensional
* ```php
* $var = array (
* 'A',
* array (
* 'B',
* array (
* 'C'
* ),
* 'D'
* ),
* 'E'
* );
* var_export( arr::flat($var) );
* ```
* ```php
* array (
* 0 => 'A',
* 1 => 'B',
* 2 => 'C',
* 3 => 'D',
* 4 => 'E',
* )
* ```
* @example Converting multiple arguments into an one-dimentional array
* ```php
* $r = arr::flat('A', 'B', array ('C', 'D'), 'E', 'F');
* var_export( $r );
* ```
* ```php
* array (
* 0 => 'A',
* 1 => 'B',
* 2 => 'C',
* 3 => 'D',
* 4 => 'E',
* 5 => 'F',
* )
* ```
* @param mixed $X Any number of parameters are accepted
* @return array
*/
static function flat($X) {
$r = array ();
$args = (func_num_args() > 1) ? func_get_args() : (type::is_iterable($X) ? $X : array ($X));
foreach ($args as $iArg) {
if (is_array($iArg)) $r = array_merge($r, arr::flat($iArg));
else $r[] = $iArg;
}
return $r;
}
}