diff --git a/src/Library/Database/Operation/MangodbOperation.php b/src/Library/Database/Operation/MangodbOperation.php index fda9794..91fda83 100644 --- a/src/Library/Database/Operation/MangodbOperation.php +++ b/src/Library/Database/Operation/MangodbOperation.php @@ -54,11 +54,12 @@ public function __construct(string|array $operations = ["*"]){ * Exemple : `*value` * Description : Performs a pattern match (like SQL's LIKE) * - * @param string|array $input + * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseLike(string|array $input, array $operation):mixed { + public function parseLike(string|array $input, array $operation, array $options = []):mixed { # Set result of parent $parentResult = parent::parseLike($input, $operation); diff --git a/src/Library/Database/Operation/SqlOperation.php b/src/Library/Database/Operation/SqlOperation.php index b8c264c..d72e656 100644 --- a/src/Library/Database/Operation/SqlOperation.php +++ b/src/Library/Database/Operation/SqlOperation.php @@ -31,6 +31,13 @@ */ class SqlOperation extends Operation { + /** Private parameters + ****************************************************** + */ + + /** @var string $entity Table to add as prefix */ + private string $entity = ""; + /** * Constructor * @@ -39,10 +46,12 @@ class SqlOperation extends Operation { * @param string|array $Operation Exemple ["=", "[]"] or ["contains", "between"] or "@>" or "contains" or "*" (for all operations) * @return self */ - public function __construct(string|array $operations = ["*"]){ + public function __construct(string|array $operations = "@all", string $entity = ""){ # Parent - parent::__construct($operations); + parent::__construct($operations, [ + "prefix" => $entity ? trim($entity)."." : $entity + ]); } @@ -58,15 +67,22 @@ public function __construct(string|array $operations = ["*"]){ * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseEqual(string|array $input, array $operation):mixed { + public function parseEqual(string|array $input, array $operation, array $options = []):mixed { # Set result of parent $parentResult = parent::parseEqual($input, $operation); + # Get value + $value = $parentResult["value"][1] ?? ""; + + # Process options + $this->_processOptions($value, $options); + # Push input in operations - $result = '= "'.$parentResult["value"].'"'; + $result = "= `$value`"; # Return input return $result; @@ -81,15 +97,22 @@ public function parseEqual(string|array $input, array $operation):mixed { * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseNotEqual(string|array $input, array $operation):mixed { + public function parseNotEqual(string|array $input, array $operation, array $options = []):mixed { # Set result of parent $parentResult = parent::parseNotEqual($input, $operation); + # Get value + $value = $parentResult["value"][1] ?? ""; + + # Process options + $this->_processOptions($value, $options); + # Push input in operations - $result = '<> "'.$parentResult["value"].'"'; + $result = "<> `$value`"; # Return input return $result; @@ -104,24 +127,32 @@ public function parseNotEqual(string|array $input, array $operation):mixed { * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseLessThanOrEqual(string|array $input, array $operation):mixed { + public function parseLessThanOrEqual(string|array $input, array $operation, array $options = []):mixed { # Set result of parent $parentResult = parent::parseLessThanOrEqual($input, $operation); + # Get value + $value = $parentResult["value"][1] ?? ""; + + # Process options + $this->_processOptions($value, $options); + # Check is numeric - if(is_numeric($parentResult["value"])) + if(is_numeric($value)) # Push input in operations - $result = '<= '.floatval($parentResult["value"]); + $result = '<= '.floatval($value); + # Error else # Error throw new CrazyException( - "\"".$parentResult["value"]."\" value cannot be less than or equal...", + "\"".$parentResult["value"][1]."\" value cannot be less than or equal...", 500, [ "custom_code" => "sqloperator-001" @@ -141,18 +172,25 @@ public function parseLessThanOrEqual(string|array $input, array $operation):mixe * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseGreaterThanOrEqual(string|array $input, array $operation):mixed { + public function parseGreaterThanOrEqual(string|array $input, array $operation, array $options = []):mixed { # Set result of parent $parentResult = parent::parseGreaterThanOrEqual($input, $operation); + # Get value + $value = $parentResult["value"][1] ?? ""; + + # Process options + $this->_processOptions($value, $options); + # Check is numeric - if(is_numeric($parentResult["value"])) + if(is_numeric($value)) # Push input in operations - $result = '>= '.floatval($parentResult["value"]); + $result = '>= '.floatval($value); else @@ -178,18 +216,25 @@ public function parseGreaterThanOrEqual(string|array $input, array $operation):m * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseSmaller(string|array $input, array $operation):mixed { + public function parseSmaller(string|array $input, array $operation, array $options = []):mixed { # Set result of parent $parentResult = parent::parseSmaller($input, $operation); + # Get value + $value = $parentResult["value"][1] ?? ""; + + # Process options + $this->_processOptions($value, $options); + # Check is numeric - if(is_numeric($parentResult["value"])) + if(is_numeric($value)) # Push input in operations - $result = '< '.floatval($parentResult["value"]); + $result = '< '.floatval($value); else @@ -215,18 +260,25 @@ public function parseSmaller(string|array $input, array $operation):mixed { * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseGreater(string|array $input, array $operation):mixed { + public function parseGreater(string|array $input, array $operation, array $options = []):mixed { # Set result of parent $parentResult = parent::parseGreater($input, $operation); + # Get value + $value = $parentResult["value"][1] ?? ""; + + # Process options + $this->_processOptions($value, $options); + # Check is numeric - if(is_numeric($parentResult["value"])) + if(is_numeric($value)) # Push input in operations - $result = '> '.floatval($parentResult["value"]); + $result = '> '.floatval($value); else @@ -252,15 +304,31 @@ public function parseGreater(string|array $input, array $operation):mixed { * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseNotBetween(string|array $input, array $operation):mixed { + public function parseNotBetween(string|array $input, array $operation, array $options = []):mixed { # Set result of parent $parentResult = parent::parseNotBetween($input, $operation); + # Get value A + $valueA = $parentResult["value"][2]; + + # Get value B + $valueA = $parentResult["value"][3]; + + # Process options on value A + $this->_processOptions($valueA, $options); + + # Process options on value B + $this->_processOptions($valueB, $options); + # Push input in operations - $result = 'NOT BETWEEN '.$parentResult[0].' AND '.$parentResult[1]; + $result = is_numeric($valueA) && is_numeric($valueB) + ? "NOT BETWEEN ".floatval($valueA)." AND ".floatval($valueB) + : "NOT BETWEEN `$valueA` AND `$valueB`" + ; # Return input return $result; @@ -275,9 +343,10 @@ public function parseNotBetween(string|array $input, array $operation):mixed { * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseBetween(string|array $input, array $operation):mixed { + public function parseBetween(string|array $input, array $operation, array $options = []):mixed { # Set result of parent $parentResult = parent::parseBetween($input, $operation); @@ -297,9 +366,10 @@ public function parseBetween(string|array $input, array $operation):mixed { * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseDefault(string|array $input):mixed { + public function parseDefault(string|array $input, array $options = []):mixed { # Set result of parent $parentResult = parent::parseDefault($input); @@ -320,9 +390,10 @@ public function parseDefault(string|array $input):mixed { * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseLike(string|array $input, array $operation):mixed { + public function parseLike(string|array $input, array $operation, array $options = []):mixed { # Set result of parent $parentResult = parent::parseLike($input, $operation); @@ -352,4 +423,30 @@ public function parseLike(string|array $input, array $operation):mixed { } + /** Private parameters + ****************************************************** + */ + + /** + * Process Options + * + * @param array &$result + * @param array $options + */ + private function _processOptions(&$result, $options):void { + + # Check prefix + if($options["prefix"] ?? false) + + # Set prefix in result + $result = $options["prefix"].$result; + + # Check suffix + if($options["suffix"] ?? false) + + # Set prefix in result + $result = $result.$options["suffix"]; + + } + } \ No newline at end of file diff --git a/src/Library/Form/Operation.php b/src/Library/Form/Operation.php index c34f593..51ef69b 100644 --- a/src/Library/Form/Operation.php +++ b/src/Library/Form/Operation.php @@ -74,20 +74,26 @@ class Operation { "[]" => [ "name" => "between", "operation" => "[]", - "regex" => '/^\[\s*(\d+)\s*:\s*(\d+)\s*\]$/' + "regex" => '/^\[\s*(\d+)\s*:\s*(\d+)\s*\]$/' # REGEX NOT WORKING 🔴 ], "![]" => [ "name" => "notBetween", "operation" => "![]", - "regex" => "/^!\[\s*(.+?):\s*(.+?)\s*\]$/" + "regex" => "/^!\[\s*(.+?):\s*(.+?)\s*\]$/" # REGEX NOT WORKING 🔴 ], "*" => [ "name" => "like", "operation" => "*", - "regex" => "/^(\*)?(.*?)(\*)?$/" + "regex" => "/^(\*)?(.*?)(\*)?$/" # REGEX WORKING BUT CATCHING [10:10] or [!10:10] 🔴 ], ]; + /** @param array $_options */ + public $options = [ + "prefix" => "", + "suffix" => '' + ]; + /** Private parameters ****************************************************** */ @@ -101,9 +107,16 @@ class Operation { * Construct and prepare instance * * @param string|array $Operation Exemple ["=", "[]"] or ["contains", "between"] or "@>" or "contains" or "@all" (for all operations) + * @param array $options [ + * suffix:string + * prefix:string + * ] * @return self */ - public function __construct(string|array $operations = "@all"){ + public function __construct(string|array $operations = "@all", $options = []){ + + # Ingest options + $this->_ingestOptions($options); # Set operations $this->set($operations); @@ -209,8 +222,18 @@ final public function get():string|array { * Process input value * * @param string|array $input + * @param array $options OVerride existing options */ - final public function run(string|array $input):mixed { + final public function run(string|array $input, array $options = []):mixed { + + # Get options + $runOptions = $this->options; + + # Check options + if(!empty($options)) + + # Set options + $this->_ingestOptions($options, $runOptions); # Set result $result = null; @@ -239,7 +262,7 @@ final public function run(string|array $input):mixed { foreach($input as $v) # Iteration of current operations - foreach($this->_currentOperations as $operation) + foreach($this->_currentOperations as $operation){ # Check regex if( @@ -263,17 +286,19 @@ final public function run(string|array $input):mixed { if($isString) # Run method found - $result = $this->{$methodName}($matches, $operation); + $result = $this->{$methodName}($matches, $operation, $runOptions); else # Run method found - $result[] = $this->{$methodName}($matches, $operation); + $result[] = $this->{$methodName}($matches, $operation, $runOptions); # Continue - continue 2; + break 2; } + + } }else # check is string @@ -346,9 +371,10 @@ private function _processMatches(array $matches, array $operation):array { * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseEqual(string|array $input, array $operation):mixed { + public function parseEqual(string|array $input, array $operation, array $options = []):mixed { # Push input in operations $operation["value"] = $input; @@ -366,9 +392,10 @@ public function parseEqual(string|array $input, array $operation):mixed { * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseNotEqual(string|array $input, array $operation):mixed { + public function parseNotEqual(string|array $input, array $operation, array $options = []):mixed { # Push input in operations $operation["value"] = $input; @@ -386,9 +413,10 @@ public function parseNotEqual(string|array $input, array $operation):mixed { * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseLessThanOrEqual(string|array $input, array $operation):mixed { + public function parseLessThanOrEqual(string|array $input, array $operation, array $options = []):mixed { # Push input in operations $operation["value"] = $input; @@ -406,9 +434,10 @@ public function parseLessThanOrEqual(string|array $input, array $operation):mixe * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseGreaterThanOrEqual(string|array $input, array $operation):mixed { + public function parseGreaterThanOrEqual(string|array $input, array $operation, array $options = []):mixed { # Push input in operations $operation["value"] = $input; @@ -426,9 +455,10 @@ public function parseGreaterThanOrEqual(string|array $input, array $operation):m * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseSmaller(string|array $input, array $operation):mixed { + public function parseSmaller(string|array $input, array $operation, array $options = []):mixed { # Push input in operations $operation["value"] = $input; @@ -446,9 +476,10 @@ public function parseSmaller(string|array $input, array $operation):mixed { * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseGreater(string|array $input, array $operation):mixed { + public function parseGreater(string|array $input, array $operation, array $options = []):mixed { # Push input in operations $operation["value"] = $input; @@ -466,9 +497,10 @@ public function parseGreater(string|array $input, array $operation):mixed { * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseBetween(string|array $input, array $operation):mixed { + public function parseBetween(string|array $input, array $operation, array $options = []):mixed { # Push input in operations $operation["value"] = $input; @@ -486,9 +518,10 @@ public function parseBetween(string|array $input, array $operation):mixed { * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseNotBetween(string|array $input, array $operation):mixed { + public function parseNotBetween(string|array $input, array $operation, array $options = []):mixed { # Push input in operations $operation["value"] = $input; @@ -505,9 +538,10 @@ public function parseNotBetween(string|array $input, array $operation):mixed { * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseDefault(string|array $input):mixed { + public function parseDefault(string|array $input, array $options = []):mixed { # Push input in operations $operation = [ @@ -528,9 +562,10 @@ public function parseDefault(string|array $input):mixed { * * @param string|array $input * @param array $operation + * @param array $options * @return mixed */ - public function parseLike(string|array $input, array $operation):mixed { + public function parseLike(string|array $input, array $operation, array $options = []):mixed { # Push input in operations $operation["value"] = $input; @@ -591,4 +626,47 @@ public function parseLike(string|array $input, array $operation):mixed { } + /** Private methods + ****************************************************** + */ + + /** + * Ingest Options + * + * @param array $options + * @param array|null $tempOptions (set options if is array) + * @return void + */ + private function _ingestOptions($options = [], array|null &$tempOptions = null):void { + + # Check options + if(!empty($options)) + + # Iteration options + foreach($this->options as $k => $v) + + # Check key exists in options + if(array_key_exists($k, $options)){ + + # Check temp options + if(is_array($tempOptions)) + + # Set temp options + $tempOptions[$k] = $options[$k]; + + # Fill instance option + else + + # Push value in instance option + $this->options[$k] = $options[$k]; + + }else + # Check temp options + if(is_array($tempOptions)) + + # Fill only temp options + $tempOptions[$k] = $this->options[$k]; + + } + } \ No newline at end of file diff --git a/tests/Library/Database/SqlOperationTest.php b/tests/Library/Database/SqlOperationTest.php new file mode 100644 index 0000000..e58bfbb --- /dev/null +++ b/tests/Library/Database/SqlOperationTest.php @@ -0,0 +1,245 @@ + + * @copyright 2022-2024 Kévin Zarshenas + */ +namespace Tests\Library\Database; + +/** + * Dependances + */ +use CrazyPHP\Library\Database\Operation\SqlOperation; +use PHPUnit\Framework\TestCase; +use CrazyPHP\Model\Env; + +/** + * Sql Operation Test + * + * Methods for test Mongodb + * + * @package kzarshenas/crazyphp + * @author kekefreedog + * @copyright 2022-2024 Kévin Zarshenas + */ +class SqlOperationTest extends TestCase { + + /** Public static parameters + ****************************************************** + */ + + /** @var @sqlOperation */ + public static SqlOperation $sqlOperation; + + /** Public method | Preparation + ****************************************************** + */ + + /** + * Set Up Before Class + * + * This method is called before the first test of this test class is run. + * + * @return void + */ + public static function setUpBeforeClass():void { + + # Setup env + Env::set([ + "phpunit_test" => true, + ]); + + # Set sql operation instance + static::$sqlOperation = new SqlOperation(); + + } + + /** + * Tear Down After Class + * + * This method is called after the last test of this test class is run. + * + * @return void + */ + public static function tearDownAfterClass():void { + + # Reset env variables + Env::reset(); + + } + + /** Public method | Tests + ****************************************************** + */ + + /** + * Test Parse Equal + * + * Test parseEqual function + * + * @return void + */ + public function testParseEqual():void { + + # Get result simple + $resultSimple = static::$sqlOperation->run("=toto"); + + # Check result + $this->assertEquals("= `toto`", $resultSimple); + + # Get result complex + $resultComplex = static::$sqlOperation->run("=toto", [ + "prefix" => "Table" + ]); + + # Check complex result + $this->assertEquals("= `Tabletoto`", $resultComplex); + + # Check suffix + $resultComplex = static::$sqlOperation->run("=toto", [ + "suffix" => "Suffix" + ]); + + # Check complex result + $this->assertEquals("= `totoSuffix`", $resultComplex); + + } + + /** + * Test Parse Not Equal + * + * Test parseNotEqual function + * + * @return void + */ + public function testParseNotEqual():void { + + # Get result simple + $resultSimple = static::$sqlOperation->run("!=toto"); + + # Check result + $this->assertEquals("<> `toto`", $resultSimple); + + } + + /** + * Test Less Than or Equal + * + * Test parseLessThanOrEqual function + * + * @return void + */ + public function testParseLessThanOrEqual():void { + + # Get result simple + $resultSimple = static::$sqlOperation->run("<=10"); + + # Check result + $this->assertEquals("<= 10", $resultSimple); + + } + + /** + * Test Greater Than or Equal + * + * Test parseGreaterThanOrEqual function + * + * @return void + */ + public function testParseGreaterThanOrEqual():void { + + # Get result simple + $resultSimple = static::$sqlOperation->run(">=10"); + + # Check result + $this->assertEquals(">= 10", $resultSimple); + + } + + /** + * Test Smaller + * + * Test parseSmaller function + * + * @return void + */ + public function testParseSmaller():void { + + # Get result simple + $resultSimple = static::$sqlOperation->run("<10"); + + # Check result + $this->assertEquals("< 10", $resultSimple); + + } + + /** + * Test Greater + * + * Test parseGreater function + * + * @return void + */ + public function testParseGreater():void { + + # Get result simple + $resultSimple = static::$sqlOperation->run(">10"); + + # Check result + $this->assertEquals("> 10", $resultSimple); + + } + + /** + * Test parse between + * + * Test parseBetween function + * + * @return void + */ + public function _testParseBetween_():void { + + # Get result A + $resultA = static::$sqlOperation->run("[1:10]"); + + # Check result A + $this->assertEquals("BETWEEN 1 AND 10", $resultA); + + # Get result B + $resultB = static::$sqlOperation->run("[A:Z]"); + + # Check result B + $this->assertEquals("BETWEEN A AND Z", $resultB); + + } + + /** + * Test parse new between + * + * Test parseNotBetween function + * + * @return void + */ + public function _testParseNotBetween_():void { + + # Get result A + $resultA = static::$sqlOperation->run("![1:10]"); + + # Check result A + $this->assertEquals("NOT BETWEEN 1 AND 10", $resultA); + + # Get result B + $resultB = static::$sqlOperation->run("![A:Z]"); + + # Check result B + $this->assertEquals("NOT BETWEEN A AND Z", $resultB); + + } + +} \ No newline at end of file diff --git a/tests/Library/Database/_SqlOperationTest_.php b/tests/Library/Database/_SqlOperationTest_.php deleted file mode 100644 index 03a29bf..0000000 --- a/tests/Library/Database/_SqlOperationTest_.php +++ /dev/null @@ -1,92 +0,0 @@ - - * @copyright 2022-2024 Kévin Zarshenas - */ -namespace Tests\Library\Database; - -/** - * Dependances - */ -use CrazyPHP\Library\Database\Driver\Mangodb; -use CrazyPHP\Library\Database\Operation\SqlOperation; -use PHPUnit\Framework\TestCase; -use CrazyPHP\Model\Env; - -/** - * Sql Operation Test - * - * Methods for test Mongodb - * - * @package kzarshenas/crazyphp - * @author kekefreedog - * @copyright 2022-2024 Kévin Zarshenas - */ -class SqlOperationTest extends TestCase { - - /** Public method | Preparation - ****************************************************** - */ - - /** - * Set Up Before Class - * - * This method is called before the first test of this test class is run. - * - * @return void - */ - public static function setUpBeforeClass():void { - - # Setup env - Env::set([ - "phpunit_test" => true, - ]); - - } - - /** - * Tear Down After Class - * - * This method is called after the last test of this test class is run. - * - * @return void - */ - public static function tearDownAfterClass():void { - - # Reset env variables - Env::reset(); - - } - - /** Public method | Tests - ****************************************************** - */ - - /** - * Test Parse Equal - * - * Test parseEqual function - * - * @return void - */ - public function testParseEqual():void { - - # Set sql operation instance - $sqlOperation = new SqlOperation(); - - # Get result - $result = $sqlOperation->run("=toto"); - - var_dump($result); - exit; - - } - -} \ No newline at end of file