XaminProject / handlebars.php

Handlebars processor for php
331 stars 132 forks source link

Option to accept object properties with null values #139

Open workguy66 opened 8 years ago

workguy66 commented 8 years ago

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 commented 8 years ago

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