From 58f22437d81c719bbd7cb8e89c199c9a73b89263 Mon Sep 17 00:00:00 2001 From: kekefreedog <70959083+kekefreedog@users.noreply.github.com> Date: Thu, 4 Apr 2024 01:22:20 +0200 Subject: [PATCH] Implement operation class --- resources/Scss/style/component_enhance.scss | 3 + src/Library/Form/Operation.php | 565 ++++++++++++++++++++ tests/Library/Form/OperationTest.php | 152 ++++++ 3 files changed, 720 insertions(+) create mode 100644 src/Library/Form/Operation.php create mode 100644 tests/Library/Form/OperationTest.php diff --git a/resources/Scss/style/component_enhance.scss b/resources/Scss/style/component_enhance.scss index e8668a9..23926b6 100644 --- a/resources/Scss/style/component_enhance.scss +++ b/resources/Scss/style/component_enhance.scss @@ -128,6 +128,9 @@ ul.collapsible { /* Children */ p { + /* Font */ + word-break: break-all!important; + /* Children */ i.material-icons { diff --git a/src/Library/Form/Operation.php b/src/Library/Form/Operation.php new file mode 100644 index 0000000..bb8388b --- /dev/null +++ b/src/Library/Form/Operation.php @@ -0,0 +1,565 @@ + + * @copyright 2022-2024 Kévin Zarshenas + */ +namespace CrazyPHP\Library\Form; + +/** + * Dependances + */ +use CrazyPHP\Library\File\Config as FileConfig; +use CrazyPHP\Exception\CrazyException; +use CrazyPHP\Library\Form\Validate; +use CrazyPHP\Library\Array\Arrays; +use CrazyPHP\Library\File\File; +use CrazyPHP\Model\Config; +use CrazyPHP\Model\Env; + +/** + * Process operations in string + * + * Parse operations into string + * + * @package kzarshenas/crazyphp + * @author kekefreedog + * @copyright 2022-2024 Kévin Zarshenas + */ +class Operation { + + /** Public constant + ****************************************************** + */ + + /** @var array $perations */ + public const LIST = [ + "=" => [ + "name" => "equal", + "operation" => "=", + "regex" => "/^=(.*)$/" + ], + "!=" => [ + "name" => "notEqual", + "operation" => "!=", + "regex" => "/^!=(.*)$/" + ], + "<" => [ + "name" => "smaller", + "operation" => "<", + "regex" => "/^<(.*)$/" + ], + ">" => [ + "name" => "greater", + "operation" => ">", + "regex" => "/^>(.*)$/" + ], + "<=" => [ + "name" => "lessThanOrEqual", + "operation" => "<=", + "regex" => "/^<=(.*)$/" + ], + ">=" => [ + "name" => "greaterThanOrEqual", + "operation" => ">=", + "regex" => "/^>=(.*)$/" + ], + "*" => [ + "name" => "like", + "operation" => "*", + "regex" => "/^(.*)\*(.*)$/" + ], + "~*" => [ + "name" => "caseInsensitiveLike", + "operation" => "~*", + "regex" => "/^~\*(.*)$/" + ], + "[]" => [ + "name" => "between", + "operation" => "[]", + "regex" => "/^\[\s*(.+?),\s*(.+?)\s*\]$/" + ], + "![]" => [ + "name" => "notBetween", + "operation" => "![]", + "regex" => "/^!\[\s*(.+?),\s*(.+?)\s*\]$/" + ], + "@>" => [ + "name" => "contains", + "operation" => "@>", + "regex" => "/^@>(.*)$/" + ], + "<@" => [ + "name" => "containedBy", + "operation" => "<@", + "regex" => "/^<@(.*)$/" + ], + ]; + + /** Private parameters + ****************************************************** + */ + + /** @param string $_currentOperations */ + private $_currentOperations = []; + + /** + * Constructor + * + * Construct and prepare instance + * + * @param string|array $Operation Exemple ["=", "[]"] or ["contains", "between"] or "@>" or "contains" or "*" (for all operations) + * @return self + */ + public function __construct(string|array $operations = "*"){ + + # Set operations + $this->set($operations); + + } + + /** Public parameters | Operations + ****************************************************** + */ + + /** + * Set + * + * Set Operations + * + * @param string|array $operations + * @return void + */ + final public function set(string|array $operations = "*"):void { + + # Reset current operations + $this->_currentOperations = []; + + # Check if empty + if($operations == "*"){ + + # Set operation + $this->_currentOperations = self::LIST; + + }else + # If string + if(is_string($operations) && $operations){ + + # Check if key set + if(array_key_exists($operations, self::LIST)) + + # Set operations + $this->_currentOperations[$operations] = self::LIST[$operations]; + + else + + # Iteration of operations + foreach(self::LIST as $key => $operation) + + # Check operations name + if(($operation["name"] ?? false) == $operations) + + # Set current operations + $this->_currentOperations[$key] = $operations; + + }else + # If array + if(is_array($operations) && !empty($operations)){ + + # Filter unique value + $operation = array_unique($operations); + + # Iteration of operations + foreach($operations as $operation) + + # Check if key set + if(array_key_exists($operation, self::LIST)) + + # Set operations + $this->_currentOperations[$operation] = self::LIST[$operation]; + + else + + # Iteration of operations + foreach(self::LIST as $key => $value) + + # Check operations name + if(($value["name"] ?? false) == $operation) + + # Set current operations + $this->_currentOperations[$key] = $value; + + } + + } + + /** + * Get + * + * Get Operations + * + * @param string|array $operations + * @return void + */ + final public function get():string|array { + + # Set result + $result = $this->_currentOperations; + + # Return result + return $result; + + } + + /** + * Run + * + * Process input value + * + * @param string|array $input + */ + final public function run(string|array $input):mixed { + + # Set result + $result = null; + + # Is string + $isString = false; + + # Check input is string + if(is_string($input)){ + + # Convert to array + $input = $input + ? [$input] + : [] + ; + + # Set is string + $isString = true; + + } + + # Iteration of current operation + if(!empty($input) && !empty($this->_currentOperations)){ + + + }else + # check is string + if($isString){ + + # Set result + $result = $this->parseDefault($input[0] ?? ""); + + }else + + # Iteration input + foreach($input as $v) + + # Set result + $result[] = $this->parseDefault($v); + + # Return result + return $result; + + } + + /** Public parameters | Parser + ****************************************************** + */ + + /** + * Equal + * + * Exemple : `=value` + * Description : Checks if a value is equal to `value` + * + * @param string $input + * @param array $operation + * @return mixed + */ + public function parseEqual(string $input, array $operation):mixed { + + # Push input in operations + $operation["value"] = $input; + + # Return input + return $operation; + + } + + /** + * Not Equal + * + * Exemple : `!=value` + * Description : Checks if a value is not equal to `value` + * + * @param string $input + * @param array $operation + * @return mixed + */ + public function parseNotEqual(string $input, array $operation):mixed { + + # Push input in operations + $operation["value"] = $input; + + # Return input + return $operation; + + } + + /** + * Smaller + * + * Exemple : `<10` + * Description : Checks if a value is smaller than 10. + * + * @param string $input + * @param array $operation + * @return mixed + */ + public function parseSmaller(string $input, array $operation):mixed { + + # Push input in operations + $operation["value"] = $input; + + # Return input + return $operation; + + } + + /** + * Greater + * + * Exemple : `>10` + * Description : Checks if a value is greater than 10 + * + * @param string $input + * @param array $operation + * @return mixed + */ + public function parseGreater(string $input, array $operation):mixed { + + # Push input in operations + $operation["value"] = $input; + + # Return input + return $operation; + + } + + /** + * Less Than or Equal + * + * Exemple : `<=10` + * Description : Checks if a value is less than or equal to 10 + * + * @param string $input + * @param array $operation + * @return mixed + */ + public function parseLessThanOrEqual(string $input, array $operation):mixed { + + # Push input in operations + $operation["value"] = $input; + + # Return input + return $operation; + + } + + /** + * Greater Than or Equal + * + * Exemple : `>=10` + * Description : Checks if a value is greater than or equal to 10 + * + * @param string $input + * @param array $operation + * @return mixed + */ + public function parseGreaterThanOrEqual(string $input, array $operation):mixed { + + # Push input in operations + $operation["value"] = $input; + + # Return input + return $operation; + + } + + /** + * Like + * + * Exemple : `*value` + * Description : Performs a pattern match (like SQL's LIKE) + * + * @param string $input + * @param array $operation + * @return mixed + */ + public function parseLike(string $input, array $operation):mixed { + + # Push input in operations + $operation["value"] = $input; + + # Check at start + if($operation["matches"][1] === '' && !empty($matches[2])) + + # Set position + $operation["position"] = "start"; + + else + # Check at the end + if(!empty($operation["matches"][1]) && $operation["matches"][2] === '') + + # Set position + $operation["position"] = "end"; + + else + # Check in the end + if(!empty($operation["matches"][1]) && !empty($operation["matches"][2])) + + # Set position + $operation["position"] = "middle"; + + # If not found + else + + # Set position + $operation["position"] = null; + + # Return input + return $operation; + + } + + /** + * Case-Insensitive Like + * + * Exemple : `~*Value` + * Description : Performs a case-insensitive pattern match + * + * @param string $input + * @param array $operation + * @return mixed + */ + public function parseCaseInsensitiveLike(string $input, array $operation):mixed { + + # Push input in operations + $operation["value"] = $input; + + # Return input + return $operation; + + } + + /** + * Between + * + * Exemple : `[1,10]` + * Description : Checks if a value is between 1 and 10 (inclusive) + * + * @param string $input + * @param array $operation + * @return mixed + */ + public function parseBetween(string $input, array $operation):mixed { + + # Push input in operations + $operation["value"] = $input; + + # Return input + return $operation; + + } + + /** + * Not Between + * + * Exemple : `![1,10]` + * Description : Checks if a value is not between 1 and 10 + * + * @param string $input + * @param array $operation + * @return mixed + */ + public function parseNotBetween(string $input, array $operation):mixed { + + # Push input in operations + $operation["value"] = $input; + + # Return input + return $operation; + + } + + /** + * Contains + * + * Exemple : `@>value` + * Description : Checks if an array or set contains `value` + * + * @param string $input + * @param array $operation + * @return mixed + */ + public function parseContains(string $input, array $operation):mixed { + + # Push input in operations + $operation["value"] = $input; + + # Return input + return $operation; + + } + + /** + * Contained By + * + * Exemple : `<@value` + * Description : Checks if a value is contained by an array or set + * + * @param string $input + * @param array $operation + * @return mixed + */ + public function parseContainedBy(string $input, array $operation):mixed { + + # Push input in operations + $operation["value"] = $input; + + # Return input + return $operation; + + } + + /** + * Parse Default + * + * Description : No operations found + * + * @param string $input + * @param array $operation + * @return mixed + */ + public function parseDefault(string $input):mixed { + + # Push input in operations + $operation = [ + "name" => "default", + "value" => $input + ]; + + # Return input + return $operation; + + } + +} \ No newline at end of file diff --git a/tests/Library/Form/OperationTest.php b/tests/Library/Form/OperationTest.php new file mode 100644 index 0000000..923bd48 --- /dev/null +++ b/tests/Library/Form/OperationTest.php @@ -0,0 +1,152 @@ + + * @copyright 2022-2024 Kévin Zarshenas + */ +namespace Tests\Library\File; + +/** + * Dependances + */ +use CrazyPHP\Library\Form\Operation; +use CrazyPHP\Model\Docker\Install; +use PHPUnit\Framework\TestCase; +use CrazyPHP\Model\Env; + +/** + * Header Test + * + * Methods for test structure folder generator + * + * @package kzarshenas/crazyphp + * @author kekefreedog + * @copyright 2022-2024 Kévin Zarshenas + */ +class OperationTest extends TestCase{ + + /** Public constants + ****************************************************** + */ + + /** 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([ + # App root for composer class + "crazyphp_root" => getcwd(), + "phpunit_test" => true, + "config_location" => "@crazyphp_root/resources/Yml" + ]); + + } + + + /** + * 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 Operation Construct + * + * Test envAndConfigValues function + * + * @return void + */ + public function testOperationConstruct():void { + + # New operation instance + $instance = new Operation(""); + + # Check operations + $this->assertEmpty($instance->get()); + + # Update instance + $instance->set(); + + # Check operations + $this->assertEquals(Operation::LIST, $instance->get()); + + # Value A + $valueA = "@>"; + + # Update instance + $instance->set($valueA); + + # Check operations + $this->assertEquals([$valueA => Operation::LIST[$valueA]], $instance->get()); + + # Set value b + $valueB = ["~*", "*", $valueA]; + + # Update instance + $instance->set($valueB); + + # Check operations + $this->assertEquals([ + $valueA => Operation::LIST[$valueA], + "~*" => Operation::LIST["~*"], + "*" => Operation::LIST["*"], + ], $instance->get()); + + } + + /** + * Test Operation Run + * + * @return void + */ + public function testOperationRun():void { + + # New instance + $instance = new Operation(); + + # Run empty + $result = $instance->run(""); + + # Check result + $this->assertEquals([ + "name" => "default", + "value" => "" + ] , $result); + + # Run empty + $result = $instance->run([]); + + # Check result + $this->assertNull($result); + + } + +} \ No newline at end of file