-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
521 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,79 @@ | ||
<?php | ||
namespace micetm\conditionsBase\services\builders\monbodb; | ||
namespace micetm\conditionsBase\services\builders\mongodb; | ||
|
||
use micetm\conditionsBase\exceptions\WrongComparison; | ||
use micetm\conditionsBase\models\ConditionInterface; | ||
use micetm\conditionsBase\services\BuilderInterface; | ||
use micetm\conditionsBase\services\builders\mongodb\comparisons\ComparisonManager; | ||
|
||
class QueryBuilder implements BuilderInterface | ||
{ | ||
/** @var ComparisonManager */ | ||
public $comparisonManager; | ||
|
||
const OPERATOR_AND = '$and'; | ||
const OPERATOR_OR = '$or'; | ||
const OPERATOR_NOT = '$not'; | ||
|
||
const CONDITIONS = [ | ||
ConditionInterface::OPERATOR_AND => self::OPERATOR_AND, | ||
ConditionInterface::OPERATOR_OR => self::OPERATOR_OR, | ||
ConditionInterface::OPERATOR_NOT => self::OPERATOR_NOT, | ||
ConditionInterface::OPERATOR_STATEMENT => self::OPERATOR_OR, | ||
]; | ||
|
||
public function __construct(ComparisonManager $comparisonManager) | ||
{ | ||
$this->comparisonManager = $comparisonManager; | ||
} | ||
|
||
/** | ||
* @param $conditions | ||
* @return array | ||
* @throws WrongComparison | ||
*/ | ||
public function create($conditions):array | ||
{ | ||
$query = []; | ||
|
||
if ($conditions instanceof \ArrayObject || is_array($conditions)) { | ||
foreach ($conditions as $condition) { | ||
$query[] = $this->getQuery($condition); | ||
|
||
} | ||
} | ||
$query = array_filter($query); | ||
|
||
if (empty($query)) { | ||
return []; | ||
} | ||
if (1 == count($query)) { | ||
return array_shift($query); | ||
} | ||
|
||
return [ | ||
self::OPERATOR_OR => $query | ||
]; | ||
} | ||
|
||
/** | ||
* @return array|null | ||
* @throws WrongComparison | ||
*/ | ||
protected function getQuery(ConditionInterface $condition) | ||
{ | ||
if (count($condition->conditionModels)) { | ||
$query = []; | ||
foreach ($condition->conditionModels as $childCondition) { | ||
if (!empty($filter = $this->getQuery($childCondition))) { | ||
$query[self::CONDITIONS[$condition->operator]][] = $filter; | ||
} | ||
} | ||
return $query; | ||
} elseif ($condition->attribute) { | ||
$comparison = $this->comparisonManager->getComparison($condition); | ||
return $comparison->buildFilter($condition); | ||
} | ||
return; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/services/builders/mongodb/comparisons/ComparisonManager.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
namespace micetm\conditionsBase\services\builders\mongodb\comparisons; | ||
|
||
use micetm\conditionsBase\exceptions\WrongComparison; | ||
use micetm\conditionsBase\models\ComparisonInterface; | ||
use micetm\conditionsBase\models\ConditionInterface; | ||
use micetm\conditionsBase\services\ComparisonManagerInterface; | ||
|
||
class ComparisonManager implements ComparisonManagerInterface | ||
{ | ||
const AVAILABLE_COMPARISONS = [ | ||
RangeComparison::class, | ||
LikeComparison::class, | ||
InComparison::class, | ||
DefaultComparison::class, | ||
EmbeddedComparison::class | ||
]; | ||
|
||
/** | ||
* @param ConditionInterface $condition | ||
* @return ComparisonInterface | ||
* @throws WrongComparison | ||
*/ | ||
public function getComparison(ConditionInterface $condition): ComparisonInterface | ||
{ | ||
foreach (self::AVAILABLE_COMPARISONS as $className) { | ||
if ($className::isMaster($condition)) { | ||
return new $className(); | ||
} | ||
} | ||
|
||
throw new WrongComparison(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/services/builders/mongodb/comparisons/DefaultComparison.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace micetm\conditionsBase\services\builders\mongodb\comparisons; | ||
|
||
use micetm\conditionsBase\models\AttributeInterface; | ||
use micetm\conditionsBase\models\ComparisonInterface; | ||
use micetm\conditionsBase\models\ConditionInterface; | ||
use micetm\conditionsBase\services\builders\mongodb\QueryBuilder; | ||
|
||
class DefaultComparison implements ComparisonInterface | ||
{ | ||
const RANGE_PARAMETER_GREATER_THAN_OR_EQUAL_TO = '$gte'; | ||
const RANGE_PARAMETER_GREATER_THAN = '$gt'; | ||
const RANGE_PARAMETER_LESS_THAN_OR_EQUAL_TO = '$lte'; | ||
const RANGE_PARAMETER_LESS_THAN = '$lt'; | ||
const IN_COMPARISON = '$in'; | ||
|
||
public static function isMaster(ConditionInterface $condition): bool | ||
{ | ||
return AttributeInterface::EQUAL_TO_COMPARISON === $condition->comparison; | ||
} | ||
|
||
public function buildFilter(ConditionInterface $condition): array | ||
{ | ||
if (is_array($condition->value)) { | ||
return $this->buikdNestedFilter($condition); | ||
} | ||
$query[$condition->attribute] = $condition->value; | ||
return $query; | ||
} | ||
|
||
protected function buikdNestedFilter(ConditionInterface $condition): array | ||
{ | ||
$query[$condition->attribute][self::IN_COMPARISON] = array_values($condition->value); | ||
return $query; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/services/builders/mongodb/comparisons/EmbeddedComparison.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace micetm\conditionsBase\services\builders\mongodb\comparisons; | ||
|
||
use micetm\conditionsBase\models\AttributeInterface; | ||
use micetm\conditionsBase\models\ComparisonInterface; | ||
use micetm\conditionsBase\models\ConditionInterface; | ||
|
||
class EmbeddedComparison extends DefaultComparison | ||
{ | ||
public static function isMaster(ConditionInterface $condition): bool | ||
{ | ||
return AttributeInterface::EMBEDDED_COMPARISON === $condition->comparison; | ||
} | ||
|
||
public function buildFilter(ConditionInterface $condition): array | ||
{ | ||
if (!is_array($condition->value)) { | ||
throw new \RuntimeException('Nested comparison should have value of type Array'); | ||
} | ||
|
||
foreach ($condition->value as $key => $value) { | ||
$query[$condition->attribute]['$elemMatch'][$key] = $value; | ||
} | ||
|
||
return $query; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/services/builders/mongodb/comparisons/InComparison.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace micetm\conditionsBase\services\builders\mongodb\comparisons; | ||
|
||
use micetm\conditionsBase\models\AttributeInterface; | ||
use micetm\conditionsBase\models\ComparisonInterface; | ||
use micetm\conditionsBase\models\ConditionInterface; | ||
use micetm\conditionsBase\services\builders\mongodb\QueryBuilder; | ||
|
||
class InComparison extends DefaultComparison | ||
{ | ||
public static function isMaster(ConditionInterface $condition): bool | ||
{ | ||
return AttributeInterface::MORE_THAN_ONE_IN_COMPARISON === $condition->comparison; | ||
} | ||
|
||
public function buildFilter(ConditionInterface $condition): array | ||
{ | ||
return parent::buikdNestedFilter($condition); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/services/builders/mongodb/comparisons/LikeComparison.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace micetm\conditionsBase\services\builders\mongodb\comparisons; | ||
|
||
use micetm\conditionsBase\models\AttributeInterface; | ||
use micetm\conditionsBase\models\ComparisonInterface; | ||
use micetm\conditionsBase\models\ConditionInterface; | ||
use micetm\conditionsBase\services\builders\mongodb\QueryBuilder; | ||
|
||
class LikeComparison implements ComparisonInterface | ||
{ | ||
public static function isMaster(ConditionInterface $condition): bool | ||
{ | ||
return AttributeInterface::LIKE_COMPARISON === $condition->comparison; | ||
} | ||
|
||
public function buildFilter(ConditionInterface $condition): array | ||
{ | ||
$query['$text']['$search'] = $condition->value; | ||
return $query; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/services/builders/mongodb/comparisons/RangeComparison.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace micetm\conditionsBase\services\builders\mongodb\comparisons; | ||
|
||
use micetm\conditionsBase\models\AttributeInterface; | ||
use micetm\conditionsBase\models\ComparisonInterface; | ||
use micetm\conditionsBase\models\ConditionInterface; | ||
use micetm\conditionsBase\services\builders\mongodb\QueryBuilder; | ||
|
||
class RangeComparison implements ComparisonInterface | ||
{ | ||
public static function isMaster(ConditionInterface $condition): bool | ||
{ | ||
return in_array( | ||
$condition->comparison, | ||
[ | ||
AttributeInterface::GREATER_THAN_COMPARISON, | ||
AttributeInterface::LESS_THAN_COMPARISON, | ||
AttributeInterface::GREATER_THAN_OR_EQUAL_TO_COMPARISON, | ||
AttributeInterface::LESS_THAN_OR_EQUAL_TO_COMPARISON | ||
] | ||
); | ||
} | ||
|
||
public function buildFilter(ConditionInterface $condition): array | ||
{ | ||
if (AttributeInterface::GREATER_THAN_COMPARISON === $condition->comparison) { | ||
$query[$condition->attribute][DefaultComparison::RANGE_PARAMETER_GREATER_THAN] | ||
= $condition->value; | ||
return $query; | ||
} | ||
if (AttributeInterface::LESS_THAN_COMPARISON === $condition->comparison) { | ||
$query[$condition->attribute][DefaultComparison::RANGE_PARAMETER_LESS_THAN] | ||
= $condition->value; | ||
return $query; | ||
} | ||
if (AttributeInterface::GREATER_THAN_OR_EQUAL_TO_COMPARISON === $condition->comparison) { | ||
$query[$condition->attribute][DefaultComparison::RANGE_PARAMETER_GREATER_THAN_OR_EQUAL_TO] | ||
= $condition->value; | ||
return $query; | ||
} | ||
if (AttributeInterface::LESS_THAN_OR_EQUAL_TO_COMPARISON === $condition->comparison) { | ||
$query[$condition->attribute][DefaultComparison::RANGE_PARAMETER_LESS_THAN_OR_EQUAL_TO] | ||
= $condition->value; | ||
return $query; | ||
} | ||
|
||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.