From 63787511b1f106fd490d3cbfd2ea8f864a38d423 Mon Sep 17 00:00:00 2001 From: Julian Vennen Date: Wed, 7 Jan 2026 13:07:50 +0100 Subject: [PATCH] Throw errors if a query is created with an invalid WHERE .. IN condition --- src/Query/Query.php | 4 +++- src/Query/Validatable.php | 15 ++++++++++++++ src/Query/WhereCondition.php | 19 +++++++++++++++-- src/Query/WhereGroup.php | 18 +++++++++++----- test/tests/QueryTest.php | 40 ++++++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 src/Query/Validatable.php create mode 100644 test/tests/QueryTest.php diff --git a/src/Query/Query.php b/src/Query/Query.php index 0023752..bd6c682 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -4,6 +4,7 @@ use Aternos\Model\ModelInterface; use InvalidArgumentException; +use UnexpectedValueException; /** * Class Query @@ -72,6 +73,7 @@ public function where(WhereCondition|array|WhereGroup $where): static } else if ($where instanceof WhereGroup) { $this->where = $where; } + $this->where?->validate(); return $this; } @@ -192,4 +194,4 @@ public function getLimit(): ?Limit { return $this->limit; } -} \ No newline at end of file +} diff --git a/src/Query/Validatable.php b/src/Query/Validatable.php new file mode 100644 index 0000000..d3d1aeb --- /dev/null +++ b/src/Query/Validatable.php @@ -0,0 +1,15 @@ +valueRaw = $valueRaw; return $this; } -} \ No newline at end of file + + public function validate(): void + { + if ($this->operator === "IN" || $this->operator === "NOT IN") { + if (!is_array($this->value)) { + throw new UnexpectedValueException("Value for IN or NOT IN operator must be an array."); + } + + if (count($this->value) === 0) { + throw new UnexpectedValueException("Value array for IN or NOT IN operator must not be empty."); + } + } + } +} diff --git a/src/Query/WhereGroup.php b/src/Query/WhereGroup.php index 0ea6d60..80940e5 100644 --- a/src/Query/WhereGroup.php +++ b/src/Query/WhereGroup.php @@ -9,8 +9,9 @@ * Class WhereConditionGroup * * @package Aternos\Model\Query + * @implements Iterator */ -class WhereGroup implements Iterator, Countable +class WhereGroup implements Iterator, Countable, Validatable { /** * Conjunction values @@ -21,7 +22,7 @@ class WhereGroup implements Iterator, Countable /** * Multiple WhereGroup or WhereCondition objects * - * @var array + * @var (WhereCondition|WhereGroup)[] */ protected array $group = []; @@ -40,7 +41,7 @@ class WhereGroup implements Iterator, Countable /** * WhereGroup constructor. * - * @param array $conditions + * @param (WhereCondition|WhereGroup)[] $conditions * @param int $conjunction */ public function __construct(array $conditions = [], int $conjunction = self:: AND) @@ -62,13 +63,20 @@ public function add(WhereCondition|WhereGroup $conditionOrGroup) /** * Get all group elements as array * - * @return array + * @return (WhereCondition|WhereGroup)[] */ public function getAll(): array { return $this->group; } + public function validate(): void + { + foreach ($this->group as $element) { + $element->validate(); + } + } + /** * Return the current element * @@ -128,4 +136,4 @@ public function count(): int { return count($this->group); } -} \ No newline at end of file +} diff --git a/test/tests/QueryTest.php b/test/tests/QueryTest.php new file mode 100644 index 0000000..6c6f3a3 --- /dev/null +++ b/test/tests/QueryTest.php @@ -0,0 +1,40 @@ +expectException(UnexpectedValueException::class); + $this->expectExceptionMessage("Value for IN or NOT IN operator must be an array."); + new SelectQuery( + new WhereCondition('number', "asdf", 'IN'), + ); + } + + public function testSelectWhereConditionINEmptyArray() + { + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage("Value array for IN or NOT IN operator must not be empty."); + + new SelectQuery( + new WhereCondition('number', [], 'IN'), + ); + } + + public function testWhereConvertInvalidArrayLength(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Argument $where has an invalid array element with a length of 5.'); + new SelectQuery([ + ['field', '=', 'value', 'extra', 'invalid'], + ]); + } +}