Skip to content

Commit

Permalink
add hasser support to getAttribute()
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh authored and fabpot committed Jan 31, 2016
1 parent 892302f commit d3c3c60
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/templates.rst
Expand Up @@ -110,6 +110,7 @@ is set, Twig will throw an error (see :ref:`environment options<environment_opti
(even if ``bar`` is the constructor - use ``__construct()`` instead);
* if not, and if ``foo`` is an object, check that ``getBar`` is a valid method;
* if not, and if ``foo`` is an object, check that ``isBar`` is a valid method;
* if not, and if ``foo`` is an object, check that ``hasBar`` is a valid method;
* if not, return a ``null`` value.

``foo['bar']`` on the other hand only works with PHP arrays:
Expand Down
5 changes: 5 additions & 0 deletions ext/twig/twig.c
Expand Up @@ -972,15 +972,18 @@ PHP_FUNCTION(twig_template_get_attributes)
char *method = NULL;
char *tmp_method_name_get;
char *tmp_method_name_is;
char *tmp_method_name_has;
zval *zmethod;
zval *tmp_methods;

lcItem_length = strlen(lcItem);
tmp_method_name_get = emalloc(4 + lcItem_length);
tmp_method_name_is = emalloc(3 + lcItem_length);
tmp_method_name_has = emalloc(4 + lcItem_length);

sprintf(tmp_method_name_get, "get%s", lcItem);
sprintf(tmp_method_name_is, "is%s", lcItem);
sprintf(tmp_method_name_has, "has%s", lcItem);

tmp_methods = TWIG_GET_ARRAY_ELEMENT(tmp_class, "methods", strlen("methods") TSRMLS_CC);

Expand All @@ -990,6 +993,8 @@ PHP_FUNCTION(twig_template_get_attributes)
method = tmp_method_name_get;
} else if (TWIG_GET_ARRAY_ELEMENT(tmp_methods, tmp_method_name_is, lcItem_length + 2 TSRMLS_CC)) {
method = tmp_method_name_is;
} else if (TWIG_GET_ARRAY_ELEMENT(tmp_methods, tmp_method_name_has, lcItem_length + 3 TSRMLS_CC)) {
method = tmp_method_name_has;
} else if (TWIG_GET_ARRAY_ELEMENT(tmp_methods, "__call", 6 TSRMLS_CC)) {
method = item;
call = 1;
Expand Down
2 changes: 2 additions & 0 deletions lib/Twig/Template.php
Expand Up @@ -539,6 +539,8 @@ protected function getAttribute($object, $item, array $arguments = array(), $typ
$method = 'get'.$item;
} elseif (isset(self::$cache[$class]['methods']['is'.$lcItem])) {
$method = 'is'.$item;
} elseif (isset(self::$cache[$class]['methods']['has'.$lcItem])) {
$method = 'has'.$item;
} elseif (isset(self::$cache[$class]['methods']['__call'])) {
$method = (string) $item;
$call = true;
Expand Down
10 changes: 10 additions & 0 deletions test/Twig/Tests/TemplateTest.php
Expand Up @@ -286,6 +286,7 @@ public function getGetAttributeTests()
'null' => null,
'1' => 1,
'bar' => true,
'foo' => true,
'09' => '09',
'+4' => '+4',
);
Expand Down Expand Up @@ -314,6 +315,7 @@ public function getGetAttributeTests()
array(true, 1, 1.0),
array(true, null, 'null'),
array(true, true, 'bar'),
array(true, true, 'foo'),
array(true, '09', '09'),
array(true, '+4', '+4'),
);
Expand Down Expand Up @@ -483,6 +485,7 @@ class Twig_TemplateArrayAccessObject implements ArrayAccess
'null' => null,
'1' => 1,
'bar' => true,
'foo' => true,
'09' => '09',
'+4' => '+4',
);
Expand Down Expand Up @@ -515,6 +518,7 @@ class Twig_TemplateMagicPropertyObject
'null' => null,
'1' => 1,
'bar' => true,
'foo' => true,
'09' => '09',
'+4' => '+4',
);
Expand Down Expand Up @@ -546,6 +550,7 @@ class Twig_TemplatePropertyObject
public $zero = 0;
public $null = null;
public $bar = true;
public $foo = true;

protected $protected = 'protected';
}
Expand Down Expand Up @@ -622,6 +627,11 @@ public function isBar()
return true;
}

public function hasFoo()
{
return true;
}

protected function getProtected()
{
return 'protected';
Expand Down

0 comments on commit d3c3c60

Please sign in to comment.