Skip to content

Commit

Permalink
Implement mangodb operator
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Apr 7, 2024
1 parent 5fcba17 commit df9b4f6
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 44 deletions.
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd"
cacheDirectory=".cache/phpunit"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
>
<testsuites>
<testsuite name="Crazy PHP | Library tests">
Expand Down
47 changes: 44 additions & 3 deletions src/Driver/Model/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
/**
* Dependances
*/
use CrazyPHP\Library\Database\Operation\MangodbOperation;
use CrazyPHP\Library\File\Config as FileConfig;
use CrazyPHP\Library\Database\Driver\Mangodb;
use CrazyPHP\Interface\CrazyDriverModel;
use CrazyPHP\Exception\CrazyException;
use CrazyPHP\Library\Array\Arrays;
use CrazyPHP\Library\Form\Process;
use CrazyPHP\Library\Router\Router;
use CrazyPHP\Library\Model\Schema;
use CrazyPHP\Library\Array\Arrays;
use CrazyPHP\Library\Form\Process;
use CrazyPHP\Library\Form\Query;
use CrazyPHP\Model\Context;
use MongoDB\Client;
Expand Down Expand Up @@ -142,11 +143,16 @@ public function parseFilter(?array $filters, ?array $options = null):self {
$this->_ingestPageStateProcess($options);

# Check filters
if(isset($filters) && is_array($filters))
if(isset($filters) && is_array($filters)){

# Process Operations In Filters
$filters = $this->_processOperationsInFilters($filters);

# Push filters in filters
$this->findOptions["filters"] = $filters;

}

# Check limit in options
if($options["limit"] ?? false && is_numeric($options["limit"])){

Expand Down Expand Up @@ -495,6 +501,41 @@ public function _pageStateProcess(array $input):array {

}

/**
* Process Operations In Filters
*
* @param array $input
* @return array
*/
private function _processOperationsInFilters(array $filters = []):array {

# Set result
$result = $filters;

# Check filters
if(!empty($result)){

# New operations
$operation = new MangodbOperation();

# Iteration filters
foreach($result as &$value)

# Check if value is string
if(is_string($value) && strpos($value, "*") !== false){

# Run operation
$value = $operation->run($value);

}

}

# Return result
return $result;

}

/** Private methods
******************************************************
*/
Expand Down
75 changes: 75 additions & 0 deletions src/Library/Database/Operation/MangodbOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php declare(strict_types=1);
/**
* Form
*
* Useful class for manipulate form
*
* PHP version 8.1.2
*
* @package kzarshenas/crazyphp
* @author kekefreedog <kevin.zarshenas@gmail.com>
* @copyright 2022-2024 Kévin Zarshenas
*/
namespace CrazyPHP\Library\Database\Operation;

/**
* Dependances
*/
use CrazyPHP\Library\Form\Operation;
use MongoDB\BSON\Regex;

/**
* Process operations in string
*
* Parse operations into string
*
* @package kzarshenas/crazyphp
* @author kekefreedog <kevin.zarshenas@gmail.com>
* @copyright 2022-2024 Kévin Zarshenas
*/
class MangodbOperation extends Operation {

/**
* 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 = ["*"]){

# Parent
parent::__construct($operations);

}

/** Public parameters | Operations
******************************************************
*/

/**
* Like
*
* Exemple : `*value`
* Description : Performs a pattern match (like SQL's LIKE)
*
* @param string|array $input
* @param array $operation
* @return mixed
*/
public function parseLike(string|array $input, array $operation):mixed {

# Set result of parent
$parentResult = parent::parseLike($input, $operation);

# Regex result
/** @disregard P1009 Assume $regex = MongoDB\BSON\Regex is available */
$regexResult = new Regex($parentResult["value"][1], 'i');

# Return regex result
return $regexResult;

}

}
73 changes: 33 additions & 40 deletions src/Library/Form/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ class Operation {
*
* Construct and prepare instance
*
* @param string|array $Operation Exemple ["=", "[]"] or ["contains", "between"] or "@>" or "contains" or "*" (for all operations)
* @param string|array $Operation Exemple ["=", "[]"] or ["contains", "between"] or "@>" or "contains" or "@all" (for all operations)
* @return self
*/
public function __construct(string|array $operations = "*"){
public function __construct(string|array $operations = "@all"){

# Set operations
$this->set($operations);
Expand All @@ -122,13 +122,13 @@ public function __construct(string|array $operations = "*"){
* @param string|array $operations
* @return void
*/
final public function set(string|array $operations = "*"):void {
final public function set(string|array $operations = "@all"):void {

# Reset current operations
$this->_currentOperations = [];

# Check if empty
if($operations == "*"){
if($operations == "@all"){

# Set operation
$this->_currentOperations = self::LIST;
Expand Down Expand Up @@ -253,19 +253,22 @@ final public function run(string|array $input):mixed {
# Set method name
$methodName = "parse".ucfirst($operation["name"]);

# Process matches
$matches = $this->_processMatches($matches, $operation);

# Check if method exists
if(method_exists($this, $methodName))

# Check if isString
if($isString)

# Run method found
$result = $this->_processResult($this->{$methodName}($matches, $operation));
$result = $this->{$methodName}($matches, $operation);

else

# Run method found
$result[] = $this->_processResult($this->{$methodName}($matches, $operation));
$result[] = $this->{$methodName}($matches, $operation);

# Continue
continue 2;
Expand Down Expand Up @@ -300,45 +303,29 @@ final public function run(string|array $input):mixed {
* Process Result
*
* Method to filter some specific anomaly produced by regex
*
* @param array $matches
* @param array $operation
* @return array
*/
private function _processResult(array $input = []):array {
private function _processMatches(array $matches, array $operation):array {

# Set result
$result = $input;

## Like

# Check like and remove "*" if in second value
if(($input["name"] ?? false) == "like" && ($input["value"][1] ?? false) == "*"){
$result = $matches;

# Remove first key of value
unset($result["value"][1]);
# Check if like
if(($operation["name"] ?? false) == "like" && !empty($matches ?? [])){

# Result keys
$result["value"] = array_values($result["value"]);
# Iteration value
foreach($result as $key => $value)

}else
# Other like issue
if(($input["name"] ?? false) == "like" && ($input["value"][1] ?? false) == "" && ($input["value"][3] ?? false) == "*"){

# Remove first key of value
unset($result["value"][1]);
# Check value
if($value == '*' || $value == "")

# Remove first key of value
unset($result["value"][3]);
# Unset value
unset($result[$key]);

# Result keys
$result["value"] = array_values($result["value"]);

}
# Check like and remove "*" if in second value
if(($result["name"] ?? false) == "like" && ($result["value"][2] ?? false) == "*"){

# Remove first key of value
unset($result["value"][2]);

# Result keys
$result["value"] = array_values($result["value"]);
$result = array_values($result);

}

Expand Down Expand Up @@ -545,15 +532,21 @@ public function parseDefault(string|array $input):mixed {
*/
public function parseLike(string|array $input, array $operation):mixed {

# Push input in operations
$operation["value"] = $input;

/* # Check if *
if(strpos($operation["value"][0], "*") === false)
# Stop method
return $this->parseDefault($input); */

# Set start
$start = false;

# Set end
$end = false;

# Push input in operations
$operation["value"] = $input;

# Check * at the start
if(strpos($operation["value"][0], '*') === 0)

Expand Down
2 changes: 1 addition & 1 deletion tests/Library/Form/OperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public function testOperationLike():void {

# Run empty
$result = $instance->run("*value");

# Check result
$this->assertEquals([
"*value",
Expand Down

0 comments on commit df9b4f6

Please sign in to comment.