From d3e1efa1c6f776aa87fe47164d77e7ea67fc196f Mon Sep 17 00:00:00 2001 From: Jorge Castro Date: Wed, 29 Sep 2021 12:23:25 -0300 Subject: [PATCH] * 4.1 see changelog. --- lib/BladeOne.php | 110 ++++++++++++++++++++++++++++++++++++---- tests/OtherTest.php | 13 ++++- tests/VariablesTest.php | 5 ++ 3 files changed, 115 insertions(+), 13 deletions(-) diff --git a/lib/BladeOne.php b/lib/BladeOne.php index b3ea656..24e050b 100644 --- a/lib/BladeOne.php +++ b/lib/BladeOne.php @@ -37,7 +37,7 @@ * @copyright Copyright (c) 2016-2021 Jorge Patricio Castro Castillo MIT License. * Don't delete this comment, its part of the license. * Part of this code is based in the work of Laravel PHP Components. - * @version 4.00 + * @version 4.1 * @link https://github.com/EFTEC/BladeOne */ class BladeOne @@ -2554,12 +2554,12 @@ protected function compileSwitch($expression) protected function compileDump($expression) { - return $this->phpTagEcho . " \$this->dump$expression;?>"; + return $this->phpTagEcho . "\$this->dump$expression;?>"; } protected function compileRelative($expression) { - return $this->phpTagEcho . " \$this->relative$expression;?>"; + return $this->phpTagEcho . "\$this->relative$expression;?>"; } protected function compileMethod($expression) @@ -2577,7 +2577,7 @@ protected function compilecsrf($expression = null) protected function compileDd($expression) { - return $this->phpTagEcho . " '
'; var_dump$expression; echo '
';?>"; + return $this->phpTagEcho . "'
'; var_dump$expression; echo '
';?>"; } /** @@ -2919,13 +2919,102 @@ protected function getArgs($expression) } /** - * It separates a string using a separator and excluding quotes and double quotes. + * It separates a string using a separator and a identifier
+ * It excludes quotes,double quotes and the "¬" symbol.
+ * Example
+ *
+     * $this->parseArgs('a=2,b='a,b,c',d'); // ['a'=>'2','b'=>'a,b,c','d'=>null]
+     * $this->parseArgs('a=2,b=c,d'); // ['a'=>'2','b'=>'c','d'=>null]
+     * $this->parseArgs('a=2 b=c',' '); // ['a'=>'2','b'=>'c']
+     * $this->parseArgs('a:2 b:c',' ',':'); // ['a'=>'2','b'=>'c']
+     * 
+ * Note: parseArgs('a = 2 b = c',' '); with return 4 values instead of 2. * - * @param string $text - * @param string $separator + * @param string $text the text to separate + * @param string $separator the separator of arguments + * @param string $assigment the character used to assign a new value + * @param bool $emptyKey if the argument is without value, we return it as key (true) or value (false) ? * @return array */ - public function parseArgs($text, $separator = ',') + public function parseArgs($text, $separator = ',', $assigment = '=', $emptyKey = true) + { + if ($text === null || $text === '') { + return []; //nothing to convert. + } + $chars = $text; // str_split($text); + $parts = []; + $nextpart = ''; + $strL = strlen($chars); + $stringArr='"\'¬'; + $parenthesis='([{'; + $parenthesisClose=')]}'; + $insidePar=false; + for ($i = 0; $i < $strL; $i++) { + $char = $chars[$i]; + // we check if the character is a parenthesis. + $pp=strpos($parenthesis, $char); + if ($pp!==false) { + // is a parenthesis, so we mark as inside a parenthesis. + $insidePar=$parenthesisClose[$pp]; + } + if ($char===$insidePar) { + // we close the parenthesis. + $insidePar=false; + } + if (strpos($stringArr, $char)!==false) { // if ($char === '"' || $char === "'" || $char === "¬") { + // we found a string initializer + $inext = strpos($text, $char, $i + 1); + $inext = $inext === false ? $strL : $inext; + $nextpart .= substr($text, $i, $inext - $i + 1); + $i = $inext; + } else { + $nextpart .= $char; + } + if ($char === $separator && $insidePar==false) { + $parts[] = substr($nextpart, 0, -1); + $nextpart = ''; + } + } + if ($nextpart !== '') { + $parts[] = $nextpart; + } + $result = []; + // duct taping for key= argument (it has a space). however, it doesn't work with key =argument + /* + foreach ($parts as $k=>$part) { + if(substr($part,-1)===$assigment && isset($parts[$k+1])) { + var_dump('ok'); + $parts[$k].=$parts[$k+1]; + unset($parts[$k+1]); + } + } + */ + foreach ($parts as $part) { + if ($part) { + $part=trim($part); + $char = $part[0]; + if (strpos($stringArr, $char)!==false) { // if ($char === '"' || $char === "'" || $char === "¬") { + if ($emptyKey) { + $result[$part] = null; + } else { + $result[] = $part; + } + } else { + $r = explode($assigment, $part, 2); + if (count($r) === 2) { + // key=value. + $result[trim($r[0])] = trim($r[1]); + } elseif ($emptyKey) { + $result[trim($r[0])] = null; + } else { + $result[] = trim($r[0]); + } + } + } + } + return $result; + } + public function parseArgsOld($text, $separator = ',') { if ($text === null || $text === '') { return []; //nothing to convert. @@ -2960,7 +3049,6 @@ public function parseArgs($text, $separator = ',') } return $result; } - /** * Compile the "raw" echo statements. * @@ -3868,7 +3956,7 @@ protected function compileEndSlot() protected function compileAsset($expression) { - return $this->phpTagEcho . " (isset(\$this->assetDict[$expression]))?\$this->assetDict[$expression]:\$this->baseUrl.'/'.$expression; ?>"; + return $this->phpTagEcho . "(isset(\$this->assetDict[$expression]))?\$this->assetDict[$expression]:\$this->baseUrl.'/'.$expression; ?>"; } protected function compileJSon($expression) @@ -3876,7 +3964,7 @@ protected function compileJSon($expression) $parts = \explode(',', $this->stripParentheses($expression)); $options = isset($parts[1]) ? \trim($parts[1]) : JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT; $depth = isset($parts[2]) ? \trim($parts[2]) : 512; - return $this->phpTagEcho . " json_encode($parts[0], $options, $depth); ?>"; + return $this->phpTagEcho . "json_encode($parts[0], $options, $depth); ?>"; } // diff --git a/tests/OtherTest.php b/tests/OtherTest.php index 74d1795..be5d59f 100644 --- a/tests/OtherTest.php +++ b/tests/OtherTest.php @@ -1,4 +1,9 @@ -getArgs($expression); // args separates the arguments by name - $name=isset($args['name']) ? $args['name'] : '--empty--'; + $name= $args['name'] ?? '--empty--'; return ""; } @@ -116,5 +121,9 @@ public function test2() $this->assertEquals('aaa"bcd', $this->blade->addInsideQuote('aaa"', "bcd")); $this->assertEquals('bcd', $this->blade->addInsideQuote('', "bcd")); $this->assertEquals('"bcd"', $this->blade->addInsideQuote('""', "bcd")); + + $arr=$this->blade->parseArgs('a1=1 a2=function(1 2 3)', ' '); + $compare=['a1'=>'1','a2'=>'function(1 2 3)']; + $this->assertEquals($compare, $arr); } } diff --git a/tests/VariablesTest.php b/tests/VariablesTest.php index 214638b..5b1a5a3 100644 --- a/tests/VariablesTest.php +++ b/tests/VariablesTest.php @@ -23,6 +23,11 @@ public function testPrintVariable() $this->assertEqualsIgnoringWhitespace("content2", $this->blade->runString($bladeString, ["var1" => "content2"])); $this->assertEqualsIgnoringWhitespace("<a href="/">My Link</a>", $this->blade->runString($bladeString, ["var1" => "My Link"])); } + public function testSetFunction() + { + $bladeString = '@set($info=funcion(1,222+funcion(2,3,4),"abc",3))'; + self::assertEquals('', $this->blade->compileString($bladeString)); + } public function testSet() { $bladeString='@set($info=$abc)';