Last active
April 12, 2021 17:52
-
-
Save meglio/8438408 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Arr | |
{ | |
static function get($arr, $keys = null, $default = null) | |
{ | |
if (is_null($keys)) | |
return $arr; | |
foreach((array)$keys as $k) | |
{ | |
# We expect an array because we are given a key to extract | |
if (!is_array($arr) || !array_key_exists($k, $arr)) | |
return $default; | |
$arr = $arr[$k]; | |
} | |
return $arr; | |
} | |
static function clear(&$arr, $keys) | |
{ | |
if (is_array($arr) && !is_null($keys)) | |
foreach((array)$keys as $k) | |
unset($arr[$k]); | |
} | |
static function hasKeys($arr, $keys) | |
{ | |
if (!is_array($arr) || is_null($keys)) | |
return false; | |
foreach((array)$keys as $k) | |
if (!array_key_exists($k, $arr)) | |
return false; | |
return true; | |
} | |
/*static function test() | |
{ | |
$a = array('x' => 'x-value', 'y' => 'y-value'); | |
$aCleared = array('y' => 'y-value'); | |
$bInner = array('y1' => 'y1-value', 'y2' => 'y2-value'); | |
$b = array('x' => 'x-value', 'y' => $bInner); | |
$tests = array( | |
array('get', array($a, null, 3), $a), | |
array('get', array($a, 'x', '~'), 'x-value'), | |
array('get', array($a, 'y', '~'), 'y-value'), | |
array('get', array($a, 'z', '~'), '~'), | |
array('get', array($b, 'y', '~'), $bInner), | |
array('get', array($b, array('y', 'y2'), '~'), 'y2-value'), | |
array('get', array($b, array('y', 'z'), '~'), '~'), | |
array('get', array($b, array('y', 'y1', 'z'), '~'), '~'), | |
array('clear', array($a, 'x'), null, array(0 => $aCleared)), | |
array('clear', array($a, array('x')), null, array(0 => $aCleared)), | |
array('clear', array($a, array('x', 'y')), null, array(0 => array())), | |
array('clear', array($a, 'z'), null, array(0 => $a)), | |
array('hasKeys', array($a, null), false), | |
array('hasKeys', array($a, 'x'), true), | |
array('hasKeys', array($a, array('x', 'y')), true), | |
array('hasKeys', array($a, array('x', 'y', 'z')), false), | |
array('hasKeys', array($a, 'z'), false), | |
); | |
foreach($tests as $ind => $t) | |
{ | |
$paramsValues = array_values($t[1]); | |
$params = array(); # copy | |
foreach($paramsValues as $key => &$value) | |
$params[$key] = &$value; | |
$paramsJson = json_encode($params); | |
$res = call_user_func_array(array(__CLASS__, $t[0]), $params); | |
if ($res != $t[2]) | |
return 'Error in test #' . $ind . "(zero-based)\n" | |
. $t[0] . '( ' . substr($paramsJson, 1, strlen($paramsJson) - 2) . " )\n" | |
. 'Result: ' . json_encode($res) . "\n" | |
. 'Expected: ' . json_encode($t[2]); | |
if (array_key_exists(3, $t)) # check input parameters | |
foreach($t[3] as $i => $expectedParamValue) | |
if ($params[$i] != $expectedParamValue) | |
return 'Error in test #' . $ind . "(zero-based)\n" | |
. $t[0] . '( ' . substr($paramsJson, 1, strlen($paramsJson) - 2) . " )\n" | |
. 'After call, parameter #' . $i .' (zero-based) is expected to be' . json_encode($expectedParamValue) . "\n" | |
. 'Actual parameter value: ' . json_encode($params[$i]); | |
} | |
return 'OK'; | |
}*/ | |
} | |
#echo Arr::test(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment