Skip to content
This repository has been archived by the owner on Jan 26, 2019. It is now read-only.

Option to accept object properties with null values #139

Open
workguy66 opened this issue Feb 5, 2016 · 1 comment
Open

Option to accept object properties with null values #139

workguy66 opened this issue Feb 5, 2016 · 1 comment

Comments

@workguy66
Copy link

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

@everplays
Copy link
Contributor

@workguy66 feel free to make a PR for it with tests for the problem you're describing. :-)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants