mirror of
https://github.com/Mibew/handlebars.php.git
synced 2024-11-15 08:44:12 +03:00
fix a bug in context when get invalid variable in strict mode
This commit is contained in:
parent
f2a30c6ef1
commit
fa65e99dfe
@ -11,6 +11,7 @@
|
||||
* @author Behrooz Shabani <everplays@gmail.com>
|
||||
* @copyright 2012 (c) ParsPooyesh Co
|
||||
* @copyright 2013 (c) Behrooz Shabani
|
||||
* @copyright 2013 (c) f0ruD A
|
||||
* @license MIT <http://opensource.org/licenses/MIT>
|
||||
* @version GIT: $Id$
|
||||
* @link http://xamin.ir
|
||||
@ -174,11 +175,11 @@ class Context
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a avariable from current context
|
||||
* Get a available from current context
|
||||
* Supported types :
|
||||
* variable , ../variable , variable.variable , .
|
||||
*
|
||||
* @param string $variableName variavle name to get from current context
|
||||
* @param string $variableName variable name to get from current context
|
||||
* @param boolean $strict strict search? if not found then throw exception
|
||||
*
|
||||
* @throws \InvalidArgumentException in strict mode and variable not found
|
||||
@ -248,17 +249,17 @@ class Context
|
||||
return $variable;
|
||||
} elseif (is_array($variable)) {
|
||||
if (isset($variable[$inside])) {
|
||||
$value = $variable[$inside];
|
||||
return $variable[$inside];
|
||||
}
|
||||
} elseif (is_object($variable)) {
|
||||
if (isset($variable->$inside)) {
|
||||
$value = $variable->$inside;
|
||||
return $variable->$inside;
|
||||
} elseif (is_callable(array($variable, $inside))) {
|
||||
$value = call_user_func(array($variable, $inside));
|
||||
return call_user_func(array($variable, $inside));
|
||||
}
|
||||
} elseif ($inside === '.') {
|
||||
$value = $variable;
|
||||
} elseif ($strict) {
|
||||
}
|
||||
|
||||
if ($strict) {
|
||||
throw new \InvalidArgumentException('can not find variable in context');
|
||||
}
|
||||
|
||||
|
@ -168,8 +168,7 @@ class Helpers
|
||||
* @var $args array
|
||||
* @var $source string
|
||||
*/
|
||||
$tmp = $context->get($args);
|
||||
$context->push($tmp);
|
||||
$context->with($args);
|
||||
$buffer = $template->render($context);
|
||||
$context->pop();
|
||||
|
||||
|
@ -1,4 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Handlebars-php
|
||||
* Base on mustache-php https://github.com/bobthecow/mustache.php
|
||||
*
|
||||
* PHP version 5.3
|
||||
*
|
||||
* @category Xamin
|
||||
* @package Handlebars
|
||||
* @author fzerorubigd <fzerorubigd@gmail.com>
|
||||
* @copyright 2013 (c) f0ruD A
|
||||
* @license MIT <http://opensource.org/licenses/MIT>
|
||||
* @version GIT: $Id$
|
||||
* @link http://xamin.ir
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class AutoloaderTest
|
||||
@ -352,4 +366,54 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals($time, $engine->render('{{time.getTimestamp}}', array('time' => $obj)));
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function testContext()
|
||||
{
|
||||
$test = new stdClass();
|
||||
$test->value = 'value';
|
||||
$test->array = array('a' => '1', 'b' => '2');
|
||||
$context = new \Handlebars\Context($test);
|
||||
$this->assertEquals('value', $context->get('value'));
|
||||
$this->assertEquals('value', $context->get('value', true));
|
||||
$this->assertEquals('1', $context->get('array.a', true));
|
||||
$this->assertEquals('2', $context->get('array.b', true));
|
||||
$new = array('value' => 'new value');
|
||||
$context->push($new);
|
||||
$this->assertEquals('new value', $context->get('value'));
|
||||
$this->assertEquals('new value', $context->get('value', true));
|
||||
$this->assertEquals('value', $context->get('../value'));
|
||||
$this->assertEquals('value', $context->get('../value', true));
|
||||
$this->assertEquals($new, $context->last());
|
||||
$this->assertEquals($new, $context->get('.'));
|
||||
$this->assertEquals($new, $context->get('this'));
|
||||
$this->assertEquals($new, $context->get('this.'));
|
||||
$this->assertEquals($test, $context->get('../.'));
|
||||
$context->pop();
|
||||
$this->assertEquals('value', $context->get('value'));
|
||||
$this->assertEquals('value', $context->get('value', true));
|
||||
$this->assertFalse($context->lastIndex());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @dataProvider getInvalidData
|
||||
*/
|
||||
public function testInvalidAccessContext($invalid)
|
||||
{
|
||||
$context = new \Handlebars\Context(array());
|
||||
$this->assertEmpty($context->get($invalid));
|
||||
$context->get($invalid, true);
|
||||
}
|
||||
|
||||
public function getInvalidData()
|
||||
{
|
||||
return array (
|
||||
array('../../data'),
|
||||
array('data'),
|
||||
array(''),
|
||||
array('data.key.key'),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user