Skip to content

Commit

Permalink
Merge pull request #3 from formal-php/specification-v4
Browse files Browse the repository at this point in the history
Use `innmind/specification` 4
  • Loading branch information
Baptouuuu authored Jul 7, 2024
2 parents 4087969 + aa4e6a4 commit 8ce90d1
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 84 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

### Changed

- Requires `innmind/specification:~4.0`

## 2.17.0 - 2024-05-29

### Changed
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"php": "~8.2",
"innmind/immutable": "~4.0|~5.0",
"innmind/url": "~4.0",
"innmind/specification": "^3.0.1",
"innmind/specification": "~4.0",
"psr/log": "~3.0"
},
"autoload": {
Expand Down
98 changes: 88 additions & 10 deletions src/Query/Where.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,19 @@ private function buildComparator(Comparator $specification): string
$column = $this->buildColumn($specification);
$sign = match ($specification->sign()) {
Sign::equality => '=',
Sign::inequality => '<>',
Sign::lessThan => '<',
Sign::moreThan => '>',
Sign::lessThanOrEqual => '<=',
Sign::moreThanOrEqual => '>=',
Sign::isNull => 'IS NULL',
Sign::isNotNull => 'IS NOT NULL',
Sign::startsWith => 'LIKE',
Sign::endsWith => 'LIKE',
Sign::contains => 'LIKE',
Sign::in => 'IN',
};

if ($specification->sign() === Sign::equality && \is_null($specification->value())) {
return \sprintf('%s IS NULL', $column);
}

return match ($specification->sign()) {
Sign::isNull => \sprintf('%s %s', $column, $sign),
Sign::isNotNull => \sprintf('%s %s', $column, $sign),
Sign::in => $this->buildInSql($specification),
default => \sprintf(
'%s %s ?',
Expand All @@ -122,6 +119,34 @@ private function buildComparator(Comparator $specification): string

private function buildComposite(Composite $specification): string
{
if (
$specification->operator() === Operator::or &&
$specification->left() instanceof Comparator &&
$specification->left()->sign() === Sign::moreThan &&
$specification->right() instanceof Comparator &&
$specification->right()->sign() === Sign::equality &&
$specification->left()->value() === $specification->right()->value()
) {
return \sprintf(
'%s >= ?',
$this->buildColumn($specification->left()),
);
}

if (
$specification->operator() === Operator::or &&
$specification->left() instanceof Comparator &&
$specification->left()->sign() === Sign::lessThan &&
$specification->right() instanceof Comparator &&
$specification->right()->sign() === Sign::equality &&
$specification->left()->value() === $specification->right()->value()
) {
return \sprintf(
'%s <= ?',
$this->buildColumn($specification->left()),
);
}

return \sprintf(
'(%s %s %s)',
$this->buildSql($specification->left()),
Expand All @@ -132,9 +157,32 @@ private function buildComposite(Composite $specification): string

private function negate(Not $specification): string
{
$inner = $specification->specification();

if (
$inner instanceof Comparator &&
$inner->sign() === Sign::equality &&
\is_null($inner->value())
) {
return \sprintf(
'%s IS NOT NULL',
$this->buildColumn($inner),
);
}

if (
$inner instanceof Comparator &&
$inner->sign() === Sign::equality
) {
return \sprintf(
'%s <> ?',
$this->buildColumn($inner),
);
}

return \sprintf(
'NOT(%s)',
$this->buildSql($specification->specification()),
$this->buildSql($inner),
);
}

Expand Down Expand Up @@ -171,6 +219,36 @@ private function findParamaters(
Sequence $parameters,
Specification $specification,
): Sequence {
if (
$specification instanceof Composite &&
$specification->operator() === Operator::or &&
$specification->left() instanceof Comparator &&
$specification->left()->sign() === Sign::moreThan &&
$specification->right() instanceof Comparator &&
$specification->right()->sign() === Sign::equality &&
$specification->left()->value() === $specification->right()->value()
) {
return $this->findComparatorParameters(
$parameters,
$specification->left(),
);
}

if (
$specification instanceof Composite &&
$specification->operator() === Operator::or &&
$specification->left() instanceof Comparator &&
$specification->left()->sign() === Sign::lessThan &&
$specification->right() instanceof Comparator &&
$specification->right()->sign() === Sign::equality &&
$specification->left()->value() === $specification->right()->value()
) {
return $this->findComparatorParameters(
$parameters,
$specification->left(),
);
}

return match (true) {
$specification instanceof Not => $this->findParamaters(
$parameters,
Expand Down Expand Up @@ -200,8 +278,8 @@ private function findComparatorParameters(
Comparator $specification,
): Sequence {
if (
$specification->sign() === Sign::isNull ||
$specification->sign() === Sign::isNotNull
$specification->sign() === Sign::equality &&
\is_null($specification->value())
) {
return $parameters;
}
Expand Down
Loading

0 comments on commit 8ce90d1

Please sign in to comment.