Skip to content

Commit

Permalink
Merge 08bb9ff into 9bcb30a
Browse files Browse the repository at this point in the history
  • Loading branch information
dependabot[bot] committed Apr 2, 2024
2 parents 9bcb30a + 08bb9ff commit 7472f0f
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 44 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"nette/tester": "~2.5",
"mockery/mockery": ">=1.5.1",
"phpstan/extension-installer": "1.3.1",
"phpstan/phpstan": "1.10.59",
"phpstan/phpstan": "1.10.66",
"phpstan/phpstan-deprecation-rules": "1.1.4",
"phpstan/phpstan-nette": "1.2.9",
"phpstan/phpstan-mockery": "1.1.2",
Expand Down
6 changes: 3 additions & 3 deletions src/Collection/Functions/BaseCompareFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ public function processDbalExpression(
$value = $args[1];
}

return $this->evaluateInDb($expression, $value, $expression->dbalModifier ?? '%any');
return $this->evaluateInDb($expression, $value, $expression->dbalModifier);
}


abstract protected function evaluateInPhp(mixed $sourceValue, mixed $targetValue): bool;


/**
* @param literal-string $modifier
* @param literal-string|array<literal-string|null>|null $modifier
*/
abstract protected function evaluateInDb(
DbalExpressionResult $expression,
mixed $value,
string $modifier,
string|array|null $modifier,
): DbalExpressionResult;
}
16 changes: 8 additions & 8 deletions src/Collection/Functions/CompareEqualsFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@


use Nextras\Orm\Collection\Functions\Result\DbalExpressionResult;
use Nextras\Orm\Exception\InvalidArgumentException;
use function count;
use function explode;
use function in_array;
use function is_array;

Expand All @@ -25,30 +25,28 @@ protected function evaluateInPhp(mixed $sourceValue, mixed $targetValue): bool
protected function evaluateInDb(
DbalExpressionResult $expression,
mixed $value,
string $modifier,
string|array|null $modifier,
): DbalExpressionResult
{
if (is_array($value)) {
if (count($value) > 0) {
// Multi-column primary key handling
// Construct multiOr simplification as array{list<Fqn>, modifiers: list<string>, values: list<list<mixed>>}
$args = $expression->getArgumentsForExpansion();
if (count($args) === 2 && $args[0] === '%column' && is_array($args[1])) {
if (count($args) === 2 && $args[0] === '%column' && is_array($args[1]) && is_array($modifier)) {
$columns = $args[1];
$modifiers = array_map(
fn (string $modifier): ?string => strlen($modifier) === 0 ? null : $modifier,
explode(',', $modifier)
);
$data = [];
foreach ($value as $dataSet) {
$set = [];
foreach ($dataSet as $i => $dataSetValue) {
$set[] = [$columns[$i], $dataSetValue, $modifiers[$i] ?? null];
$set[] = [$columns[$i], $dataSetValue, $modifier[$i] ?? null];
}
$data[] = $set;
}
return $expression->withArgs('%multiOr', [$data]);
} else {
if (is_array($modifier)) throw new InvalidArgumentException();
$modifier = $modifier ?? '%any';
if ($modifier !== '%any') $modifier .= '[]';
return $expression->append("IN $modifier", $value);
}
Expand All @@ -58,6 +56,8 @@ protected function evaluateInDb(
} elseif ($value === null) {
return $expression->append('IS NULL');
} else {
if (is_array($modifier)) throw new InvalidArgumentException();
$modifier = $modifier ?? '%any';
return $expression->append("= $modifier", $value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@


use Nextras\Orm\Collection\Functions\Result\DbalExpressionResult;
use Nextras\Orm\Exception\InvalidArgumentException;
use function is_array;


class CompareGreaterThanEqualsFunction extends BaseCompareFunction
Expand All @@ -17,9 +19,11 @@ protected function evaluateInPhp(mixed $sourceValue, mixed $targetValue): bool
protected function evaluateInDb(
DbalExpressionResult $expression,
mixed $value,
string $modifier,
string|array|null $modifier,
): DbalExpressionResult
{
if (is_array($modifier)) throw new InvalidArgumentException();
$modifier = $modifier ?? '%any';
return $expression->append(">= $modifier", $value);
}
}
6 changes: 5 additions & 1 deletion src/Collection/Functions/CompareGreaterThanFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@


use Nextras\Orm\Collection\Functions\Result\DbalExpressionResult;
use Nextras\Orm\Exception\InvalidArgumentException;
use function is_array;


class CompareGreaterThanFunction extends BaseCompareFunction
Expand All @@ -17,9 +19,11 @@ protected function evaluateInPhp(mixed $sourceValue, mixed $targetValue): bool
protected function evaluateInDb(
DbalExpressionResult $expression,
mixed $value,
string $modifier,
string|array|null $modifier,
): DbalExpressionResult
{
if (is_array($modifier)) throw new InvalidArgumentException();
$modifier = $modifier ?? '%any';
return $expression->append("> $modifier", $value);
}
}
14 changes: 7 additions & 7 deletions src/Collection/Functions/CompareNotEqualsFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,28 @@ protected function evaluateInPhp(mixed $sourceValue, mixed $targetValue): bool
protected function evaluateInDb(
DbalExpressionResult $expression,
mixed $value,
string $modifier,
string|array|null $modifier,
): DbalExpressionResult
{
if (is_array($value)) {
if (count($value) > 0) {
// Multi-column primary key handling
// Construct multiOr simplification as array{list<Fqn>, modifiers: list<string>, values: list<list<mixed>>}
$args = $expression->getArgumentsForExpansion();
if (count($args) === 2 && $args[0] === '%column' && is_array($args[1])) {
if (count($args) === 2 && $args[0] === '%column' && is_array($args[1]) && is_array($modifier)) {
$columns = $args[1];
$modifiers = array_map(
fn (string $modifier): ?string => strlen($modifier) === 0 ? null : $modifier,
explode(',', $modifier)
);
$data = [];
foreach ($value as $dataSet) {
$set = [];
foreach ($dataSet as $i => $dataSetValue) {
$set[] = [$columns[$i], $dataSetValue, $modifiers[$i] ?? null];
$set[] = [$columns[$i], $dataSetValue, $modifier[$i] ?? null];
}
$data[] = $set;
}
return $expression->withArgs('NOT (%multiOr)', [$data]);
} else {
if (is_array($modifier)) throw new InvalidArgumentException();
$modifier = $modifier ?? '%any';
if ($modifier !== '%any') $modifier .= '[]';
return $expression->append("NOT IN $modifier", $value);
}
Expand All @@ -61,6 +59,8 @@ protected function evaluateInDb(
} elseif ($value === null) {
return $expression->append('IS NOT NULL');
} else {
if (is_array($modifier)) throw new InvalidArgumentException();
$modifier = $modifier ?? '%any';
return $expression->append("!= $modifier", $value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@


use Nextras\Orm\Collection\Functions\Result\DbalExpressionResult;
use Nextras\Orm\Exception\InvalidArgumentException;
use function is_array;


class CompareSmallerThanEqualsFunction extends BaseCompareFunction
Expand All @@ -17,9 +19,11 @@ protected function evaluateInPhp(mixed $sourceValue, mixed $targetValue): bool
protected function evaluateInDb(
DbalExpressionResult $expression,
mixed $value,
string $modifier,
string|array|null $modifier,
): DbalExpressionResult
{
if (is_array($modifier)) throw new InvalidArgumentException();
$modifier = $modifier ?? '%any';
return $expression->append("<= $modifier", $value);
}
}
6 changes: 5 additions & 1 deletion src/Collection/Functions/CompareSmallerThanFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@


use Nextras\Orm\Collection\Functions\Result\DbalExpressionResult;
use Nextras\Orm\Exception\InvalidArgumentException;
use function is_array;


class CompareSmallerThanFunction extends BaseCompareFunction
Expand All @@ -17,9 +19,11 @@ protected function evaluateInPhp(mixed $sourceValue, mixed $targetValue): bool
protected function evaluateInDb(
DbalExpressionResult $expression,
mixed $value,
string $modifier,
string|array|null $modifier,
): DbalExpressionResult
{
if (is_array($modifier)) throw new InvalidArgumentException();
$modifier = $modifier ?? '%any';
return $expression->append("< $modifier", $value);
}
}
11 changes: 6 additions & 5 deletions src/Collection/Functions/FetchPropertyFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private function processTokens(
$propertyPrefixTokens = "";
$makeDistinct = false;

/** @var DbalTableJoin[] $joins */
/** @var list<DbalTableJoin> $joins */
$joins = [];

foreach ($tokens as $tokenIndex => $token) {
Expand Down Expand Up @@ -242,7 +242,6 @@ private function processTokens(
throw new InvalidArgumentException("Property expression '$propertyExpression' does not fetch specific property.");
}

$modifier = '';
$column = $this->toColumnExpr(
$currentEntityMetadata,
$propertyMetadata,
Expand Down Expand Up @@ -270,7 +269,7 @@ private function processTokens(

/**
* @param array<string> $tokens
* @param DbalTableJoin[] $joins
* @param list<DbalTableJoin> $joins
* @param Aggregator<mixed>|null $aggregator
* @param DbalMapper<IEntity> $currentMapper
* @return array{string, IConventions, EntityMetadata, DbalMapper<IEntity>}
Expand Down Expand Up @@ -368,6 +367,7 @@ private function processRelationship(


/**
* @param literal-string|list<literal-string|null>|null $modifier
* @return Fqn|list<Fqn>
*/
private function toColumnExpr(
Expand All @@ -376,20 +376,21 @@ private function toColumnExpr(
IConventions $conventions,
string $alias,
string $propertyPrefixTokens,
string &$modifier,
string|array|null &$modifier,
): Fqn|array
{
if ($propertyMetadata->isPrimary && $propertyMetadata->isVirtual) { // primary-proxy
$primaryKey = $entityMetadata->getPrimaryKey();
if (count($primaryKey) > 1) { // composite primary key
$pair = [];
/** @var list<literal-string|null> $modifiers */
$modifiers = [];
foreach ($primaryKey as $columnName) {
$columnName = $conventions->convertEntityToStorageKey($propertyPrefixTokens . $columnName);
$pair[] = new Fqn(schema: $alias, name: $columnName);
$modifiers[] = $conventions->getModifier($columnName);
}
$modifier = implode(',', $modifiers);
$modifier = $modifiers;
return $pair;
} else {
$propertyName = $primaryKey[0];
Expand Down
4 changes: 2 additions & 2 deletions src/Collection/Functions/Result/DbalExpressionResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DbalExpressionResult
* @param bool $isHavingClause True if the expression represents HAVING clause instead of WHERE clause.
* @param PropertyMetadata|null $propertyMetadata Reference to backing property of the expression. If null, the expression is no more a simple property expression.
* @param (callable(mixed): mixed)|null $valueNormalizer Normalizes the value for better PHP comparison, it considers the backing property type.
* @param literal-string|null $dbalModifier Dbal modifier for particular column. Null if expression is a general expression.
* @param literal-string|list<literal-string|null>|null $dbalModifier Dbal modifier for particular column. Array if multi-column. Null value means expression is a general expression.
*/
public function __construct(
public readonly string $expression,
Expand All @@ -52,7 +52,7 @@ public function __construct(
public readonly bool $isHavingClause = false,
public readonly ?PropertyMetadata $propertyMetadata = null,
?callable $valueNormalizer = null,
public readonly ?string $dbalModifier = null,
public readonly string|array|null $dbalModifier = null,
)
{
$this->valueNormalizer = $valueNormalizer;
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Reflection/IMetadataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ interface IMetadataParser
* @param class-string $entityClass
* @param list<string>|null $fileDependencies
*/
public function parseMetadata(string $entityClass, ?array &$fileDependencies): EntityMetadata;
public function parseMetadata(string $entityClass, array|null &$fileDependencies): EntityMetadata;
}
17 changes: 10 additions & 7 deletions src/Entity/Reflection/MetadataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,25 @@ public function addModifier(string $modifier, callable $processor)
}


public function parseMetadata(string $entityClass, ?array &$fileDependencies): EntityMetadata
public function parseMetadata(string $entityClass, array|null &$fileDependencies): EntityMetadata
{
$this->reflection = new ReflectionClass($entityClass);
$this->metadata = new EntityMetadata($entityClass);

$this->loadProperties($fileDependencies);
$this->initPrimaryKey();

$fileDependencies = array_unique($fileDependencies);
if ($fileDependencies !== null) {
$fileDependencies = array_values(array_unique($fileDependencies));
}
return $this->metadata;
}


/**
* @param string[] $fileDependencies
* @param list<string> $fileDependencies
* @param list<string>|null $fileDependencies
*/
protected function loadProperties(?array &$fileDependencies): void
protected function loadProperties(array|null &$fileDependencies): void
{
$classTree = [$current = $this->reflection->name];
while (($current = get_parent_class($current)) !== false) {
Expand All @@ -126,13 +127,15 @@ protected function loadProperties(?array &$fileDependencies): void
foreach ($traits !== false ? $traits : [] as $traitName) {
assert(trait_exists($traitName));
$reflectionTrait = new ReflectionClass($traitName);
$fileDependencies[] = $reflectionTrait->getFileName();
$file = $reflectionTrait->getFileName();
if ($file !== false) $fileDependencies[] = $file;
$this->currentReflection = $reflectionTrait;
$this->classPropertiesCache[$traitName] = $this->parseAnnotations($reflectionTrait, $methods);
}

$reflection = new ReflectionClass($class);
$fileDependencies[] = $reflection->getFileName();
$file = $reflection->getFileName();
if ($file !== false) $fileDependencies[] = $file;
$this->currentReflection = $reflection;
$this->classPropertiesCache[$class] = $this->parseAnnotations($reflection, $methods);
}
Expand Down

0 comments on commit 7472f0f

Please sign in to comment.