-
Notifications
You must be signed in to change notification settings - Fork 0
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
217 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Micoli\Elql\Exception; | ||
|
||
use LogicException; | ||
|
||
class ElqlException extends LogicException | ||
{ | ||
} |
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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Micoli\Elql\Exception; | ||
|
||
class NonUniqueException extends ElqlException | ||
{ | ||
} |
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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Micoli\Elql\ExpressionLanguage; | ||
|
||
use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
use Symfony\Component\ExpressionLanguage\ExpressionFunction; | ||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
|
||
class ExpressionLanguageEvaluator implements ExpressionLanguageEvaluatorInterface | ||
{ | ||
public function __construct( | ||
private readonly ExpressionLanguage $expressionLanguage = new ExpressionLanguage(new ArrayAdapter()), | ||
) { | ||
foreach ([ | ||
'strtoupper', 'strtolower', | ||
'str_starts_with', 'str_ends_with', 'str_contains', | ||
'substr', 'strlen', | ||
'trim', 'ltrim', 'rtrim', | ||
'abs', 'min', 'max', 'floor', 'ceil', | ||
] as $nativeFunction) { | ||
$this->expressionLanguage->addFunction(ExpressionFunction::fromPhp($nativeFunction)); | ||
} | ||
} | ||
|
||
public function evaluate(string $expression, mixed $record): mixed | ||
{ | ||
return $this->expressionLanguage->evaluate($expression, ['record' => $record]); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/ExpressionLanguage/ExpressionLanguageEvaluatorInterface.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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Micoli\Elql\ExpressionLanguage; | ||
|
||
interface ExpressionLanguageEvaluatorInterface | ||
{ | ||
public function evaluate(string $expression, mixed $record): mixed; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Micoli\Elql\Metadata; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] | ||
class Unique | ||
{ | ||
public function __construct( | ||
public readonly string $expression, | ||
public readonly ?string $name = null, | ||
) { | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Micoli\Elql\Persister; | ||
|
||
use Micoli\Elql\Exception\NonUniqueException; | ||
use Micoli\Elql\ExpressionLanguage\ExpressionLanguageEvaluatorInterface; | ||
use Micoli\Elql\Metadata\MetadataManagerInterface; | ||
|
||
class IndexChecker | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
public function checkUniqueIndexes( | ||
MetadataManagerInterface $metadataManager, | ||
ExpressionLanguageEvaluatorInterface $expressionLanguageEvaluator, | ||
object $record, | ||
InMemoryTable $inMemoryTable, | ||
): void { | ||
$errors = []; | ||
foreach ($metadataManager->uniques($record::class) as $indexName => $indexExpression) { | ||
/** | ||
* @var string $newRecordIndexValue | ||
*/ | ||
$newRecordIndexValue = $expressionLanguageEvaluator->evaluate($indexExpression, $record); | ||
/** | ||
* @var mixed $data | ||
*/ | ||
foreach ($inMemoryTable->data as $data) { | ||
/** | ||
* @var string $existingRecordIndexValue | ||
*/ | ||
$existingRecordIndexValue = $expressionLanguageEvaluator->evaluate($indexExpression, $data); | ||
if ($existingRecordIndexValue === $newRecordIndexValue) { | ||
$errors[] = sprintf('[%s:%s]', $indexName, json_encode($existingRecordIndexValue)); | ||
} | ||
} | ||
} | ||
if (count($errors) > 0) { | ||
throw new NonUniqueException(sprintf('Duplicate on %s', implode(', ', $errors))); | ||
} | ||
} | ||
} |
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