This repository was archived by the owner on Jan 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
This repository was archived by the owner on Jan 26, 2019. It is now read-only.
Option to accept object properties with null values #139
Copy link
Copy link
Open
Description
When an object has a property but with the value null, it says that the object does not have the property
I think this is incorrect behavior, if a class is defined with attributes it should use the attribute values even if it null
at least for objects, because the logic of arrays and objects must match!
you have a check array_key_exists in arrays, why not use property_exists for objects??
Context.php
private function _findVariableInContext($variable, $inside, $strict = false)
{
$value = null;
if (($inside !== '0' && empty($inside)) || ($inside == 'this')) {
return $variable;
} elseif (is_array($variable)) {
if (isset($variable[$inside]) || array_key_exists($inside, $variable)) {
return $variable[$inside];
} elseif ($inside == "length") {
return count($variable);
}
} elseif (is_object($variable)) {
if (isset($variable->$inside)) {
return $variable->$inside;
} elseif (is_callable(array($variable, $inside))) {
return call_user_func(array($variable, $inside));
}
}
if ($strict) {
throw new \InvalidArgumentException(
sprintf(
'Can not find variable in context: "%s"',
$inside
)
);
}
return $value;
}
should be
private function _findVariableInContext($variable, $inside, $strict = false)
{
$value = null;
if (($inside !== '0' && empty($inside)) || ($inside == 'this')) {
return $variable;
} elseif (is_array($variable)) {
if (isset($variable[$inside]) || array_key_exists($inside, $variable)) { // this should be matched
return $variable[$inside];
} elseif ($inside == "length") {
return count($variable);
}
} elseif (is_object($variable)) {
if (isset($variable->$inside) || property_exists($variable, $inside)) { // match array logic above
return $variable->$inside;
} elseif (is_callable(array($variable, $inside))) {
return call_user_func(array($variable, $inside));
}
}
if ($strict) {
throw new \InvalidArgumentException(
sprintf(
'Can not find variable in context: "%s"',
$inside
)
);
}
return $value;
}
because
$ob->prop = null;
isset($obj->prop) // false
property_exists($obj, 'prop'') // true
what I suggest is to add an option in handlebars to switch on this kind of behavior
Metadata
Metadata
Assignees
Labels
No labels