Treat null entries as non-existent. Add a startsWith() method.

This is necessary to avoid fatal errors when trying to retrieve existing but inaccessible properties (i.e. private or protected).
This commit is contained in:
Yahnis Elsts 2017-01-10 13:13:01 +02:00
parent aeeda3c330
commit 844516d1f5
1 changed files with 14 additions and 2 deletions

View File

@ -24,9 +24,9 @@ if ( !class_exists('Puc_v4_Utils', false) ):
$currentValue = $array;
$pathExists = true;
foreach ($path as $node) {
if ( is_array($currentValue) && array_key_exists($node, $currentValue) ) {
if ( is_array($currentValue) && isset($currentValue[$node]) ) {
$currentValue = $currentValue[$node];
} else if ( is_object($currentValue) && property_exists($currentValue, $node) ) {
} else if ( is_object($currentValue) && isset($currentValue->$node) ) {
$currentValue = $currentValue->$node;
} else {
$pathExists = false;
@ -60,6 +60,18 @@ if ( !class_exists('Puc_v4_Utils', false) ):
return $default;
}
/**
* Check if the input string starts with the specified prefix.
*
* @param string $input
* @param string $prefix
* @return bool
*/
public static function startsWith($input, $prefix) {
$length = strlen($prefix);
return (substr($input, 0, $length) === $prefix);
}
}
endif;