mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-14 19:36:43 +00:00
add hasser support to getAttribute()
This commit is contained in:
committed by
Fabien Potencier
parent
892302f848
commit
d3c3c605d5
@@ -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:
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -539,6 +539,8 @@ abstract class Twig_Template
|
||||
$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;
|
||||
|
||||
@@ -286,6 +286,7 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
|
||||
'null' => null,
|
||||
'1' => 1,
|
||||
'bar' => true,
|
||||
'foo' => true,
|
||||
'09' => '09',
|
||||
'+4' => '+4',
|
||||
);
|
||||
@@ -314,6 +315,7 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
|
||||
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'),
|
||||
);
|
||||
@@ -483,6 +485,7 @@ class Twig_TemplateArrayAccessObject implements ArrayAccess
|
||||
'null' => null,
|
||||
'1' => 1,
|
||||
'bar' => true,
|
||||
'foo' => true,
|
||||
'09' => '09',
|
||||
'+4' => '+4',
|
||||
);
|
||||
@@ -515,6 +518,7 @@ class Twig_TemplateMagicPropertyObject
|
||||
'null' => null,
|
||||
'1' => 1,
|
||||
'bar' => true,
|
||||
'foo' => true,
|
||||
'09' => '09',
|
||||
'+4' => '+4',
|
||||
);
|
||||
@@ -546,6 +550,7 @@ class Twig_TemplatePropertyObject
|
||||
public $zero = 0;
|
||||
public $null = null;
|
||||
public $bar = true;
|
||||
public $foo = true;
|
||||
|
||||
protected $protected = 'protected';
|
||||
}
|
||||
@@ -622,6 +627,11 @@ class Twig_TemplateMethodObject
|
||||
return true;
|
||||
}
|
||||
|
||||
public function hasFoo()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function getProtected()
|
||||
{
|
||||
return 'protected';
|
||||
|
||||
Reference in New Issue
Block a user