From 0339a12b3d010e237046263052d33527b027398e Mon Sep 17 00:00:00 2001 From: bzick Date: Sat, 31 Jan 2015 19:16:43 +0300 Subject: [PATCH 1/6] Fix #116 --- src/Fenom/Modifier.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Fenom/Modifier.php b/src/Fenom/Modifier.php index cf9fc57..8d9439b 100644 --- a/src/Fenom/Modifier.php +++ b/src/Fenom/Modifier.php @@ -107,9 +107,9 @@ class Modifier if (preg_match('#^(.{' . $length . '}).*?(.{' . $length . '})?$#usS', $string, $match)) { if (count($match) == 3) { if ($by_words) { - return preg_replace('#\s.*$#usS', "", $match[1]) . + return preg_replace('#\s\S*$#usS', "", $match[1]) . $etc . - preg_replace('#.*\s#usS', "", $match[2]); + preg_replace('#\s\S*$#usS', "", $match[2]); } else { return $match[1] . $etc . $match[2]; } @@ -118,7 +118,7 @@ class Modifier } else { if (preg_match('#^(.{' . $length . '})#usS', $string, $match)) { if ($by_words) { - return preg_replace('#\s.*$#usS', "", $match[1]) . $etc; + return preg_replace('#\s\S*$#usS', "", $match[1]) . $etc; } else { return $match[1] . $etc; } From 0592e248e2cf6dd5c81d3c21b84fff9a5f043b5c Mon Sep 17 00:00:00 2001 From: bzick Date: Sun, 1 Feb 2015 00:35:41 +0300 Subject: [PATCH 2/6] Add test for 'in' operator --- tests/cases/Fenom/TemplateTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/cases/Fenom/TemplateTest.php b/tests/cases/Fenom/TemplateTest.php index cd8a093..baf60c2 100644 --- a/tests/cases/Fenom/TemplateTest.php +++ b/tests/cases/Fenom/TemplateTest.php @@ -1184,6 +1184,7 @@ class TemplateTest extends TestCase array('{if $one in ["one", "two", "three"]} block1 {else} block2 {/if}', 'block2'), array('{if $one in keys [1 => "one", 2 => "two", 3 => "three"]} block1 {else} block2 {/if}', 'block1'), array('{if $one in $two} block1 {else} block2 {/if}', 'block2'), + array('{if $one in $list} block1 {else} block2 {/if}', 'block1'), ); } From 4beacd57b98557b5e013d76bcbbc834e5a227125 Mon Sep 17 00:00:00 2001 From: bzick Date: Sun, 1 Feb 2015 00:49:59 +0300 Subject: [PATCH 3/6] Fix #116 (broken regexp) --- src/Fenom/Modifier.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fenom/Modifier.php b/src/Fenom/Modifier.php index 8d9439b..5fd13ca 100644 --- a/src/Fenom/Modifier.php +++ b/src/Fenom/Modifier.php @@ -109,7 +109,7 @@ class Modifier if ($by_words) { return preg_replace('#\s\S*$#usS', "", $match[1]) . $etc . - preg_replace('#\s\S*$#usS', "", $match[2]); + preg_replace('#\S*\s#usS', "", $match[2]); } else { return $match[1] . $etc . $match[2]; } From 76b5d3e6fa35eb6c08effd26eddd4038aa5e1bed Mon Sep 17 00:00:00 2001 From: bzick Date: Sun, 1 Feb 2015 00:51:49 +0300 Subject: [PATCH 4/6] Add array of variable to tag-function's arguments --- src/Fenom/Compiler.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Fenom/Compiler.php b/src/Fenom/Compiler.php index 5b0f2e3..4b11604 100644 --- a/src/Fenom/Compiler.php +++ b/src/Fenom/Compiler.php @@ -648,10 +648,10 @@ class Compiler public static function stdFuncParser(Tokenizer $tokens, Tag $tag) { if(is_string($tag->callback)) { - return $tag->out($tag->callback . "(" . self::toArray($tag->tpl->parseParams($tokens)) . ', $tpl)'); + return $tag->out($tag->callback . "(" . self::toArray($tag->tpl->parseParams($tokens)) . ', $tpl, $var)'); } else { return '$info = $tpl->getStorage()->getTag('.var_export($tag->name, true).');'.PHP_EOL. - $tag->out('call_user_func($info["function"], '.self::toArray($tag->tpl->parseParams($tokens)).', $tpl)'); + $tag->out('call_user_func_array($info["function"], array('.self::toArray($tag->tpl->parseParams($tokens)).', $tpl, &$var))'); } } @@ -709,10 +709,10 @@ class Compiler { $tag->restore(\Fenom::AUTO_ESCAPE); if(is_string($tag->callback)) { - return $tag->out($tag->callback . "(" . $tag["params"] . ', ob_get_clean(), $tpl)'); + return $tag->out($tag->callback . "(" . $tag["params"] . ', ob_get_clean(), $tpl, $var)'); } else { return '$info = $tpl->getStorage()->getTag('.var_export($tag->name, true).');'.PHP_EOL. - $tag->out('call_user_func($info["function"], ' . $tag["params"] . ', ob_get_clean(), $tpl)'); + $tag->out('call_user_func_array($info["function"], array(' . $tag["params"] . ', ob_get_clean(), $tpl, &$var))'); } } From fb544b10e8d403e11e2caddc557265b31d335e63 Mon Sep 17 00:00:00 2001 From: bzick Date: Sun, 1 Feb 2015 00:58:40 +0300 Subject: [PATCH 5/6] Add more test for 'in' --- tests/TestCase.php | 6 ++++++ tests/cases/Fenom/TemplateTest.php | 2 ++ 2 files changed, 8 insertions(+) diff --git a/tests/TestCase.php b/tests/TestCase.php index d20effd..9f97fdb 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -29,6 +29,12 @@ class TestCase extends \PHPUnit_Framework_TestCase "b" => 2, "two" => 2 ), + "level1" => array( + "level2" => array( + "one" => 1, + "two" => 2 + ) + ), "num" => array( 1 => "one", 2 => "two", diff --git a/tests/cases/Fenom/TemplateTest.php b/tests/cases/Fenom/TemplateTest.php index baf60c2..22af39e 100644 --- a/tests/cases/Fenom/TemplateTest.php +++ b/tests/cases/Fenom/TemplateTest.php @@ -1185,6 +1185,8 @@ class TemplateTest extends TestCase array('{if $one in keys [1 => "one", 2 => "two", 3 => "three"]} block1 {else} block2 {/if}', 'block1'), array('{if $one in $two} block1 {else} block2 {/if}', 'block2'), array('{if $one in $list} block1 {else} block2 {/if}', 'block1'), + array('{if "one" in $num} block1 {else} block2 {/if}', 'block1'), + array('{if "one" in $level1.level2} block1 {else} block2 {/if}', 'block1'), ); } From 5d87060f4216e8aee0d890b9a74739bd37d8e3ce Mon Sep 17 00:00:00 2001 From: bzick Date: Sun, 1 Feb 2015 01:13:39 +0300 Subject: [PATCH 6/6] Dummy protect --- src/Fenom/Modifier.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Fenom/Modifier.php b/src/Fenom/Modifier.php index 5fd13ca..cde94f3 100644 --- a/src/Fenom/Modifier.php +++ b/src/Fenom/Modifier.php @@ -170,10 +170,12 @@ class Modifier */ public static function in($value, $haystack) { - if (is_array($haystack)) { - return in_array($value, $haystack) || array_key_exists($value, $haystack); - } elseif (is_string($haystack)) { - return strpos($haystack, $value) !== false; + if(is_scalar($value)) { + if (is_array($haystack)) { + return in_array($value, $haystack) || array_key_exists($value, $haystack); + } elseif (is_string($haystack)) { + return strpos($haystack, $value) !== false; + } } return false; }