Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Aternos\Model\ModelInterface;
use InvalidArgumentException;
use UnexpectedValueException;

/**
* Class Query
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -192,4 +194,4 @@ public function getLimit(): ?Limit
{
return $this->limit;
}
}
}
15 changes: 15 additions & 0 deletions src/Query/Validatable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Aternos\Model\Query;

use UnexpectedValueException;

interface Validatable
{
/**
* Validate that this object is correctly configured
* @return void
* @throws UnexpectedValueException if the object is not valid
*/
public function validate(): void;
}
19 changes: 17 additions & 2 deletions src/Query/WhereCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Aternos\Model\Query;

use UnexpectedValueException;

/**
* Class WhereCondition
*
* @package Aternos\Model\Query
*/
class WhereCondition
class WhereCondition implements Validatable
{
/**
* Name of the field
Expand Down Expand Up @@ -73,4 +75,17 @@ public function setValueRaw(bool $valueRaw = true): WhereCondition
$this->valueRaw = $valueRaw;
return $this;
}
}

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.");
}
}
}
}
18 changes: 13 additions & 5 deletions src/Query/WhereGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
* Class WhereConditionGroup
*
* @package Aternos\Model\Query
* @implements Iterator<WhereCondition|WhereGroup>
*/
class WhereGroup implements Iterator, Countable
class WhereGroup implements Iterator, Countable, Validatable
{
/**
* Conjunction values
Expand All @@ -21,7 +22,7 @@ class WhereGroup implements Iterator, Countable
/**
* Multiple WhereGroup or WhereCondition objects
*
* @var array
* @var (WhereCondition|WhereGroup)[]
*/
protected array $group = [];

Expand All @@ -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)
Expand All @@ -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
*
Expand Down Expand Up @@ -128,4 +136,4 @@ public function count(): int
{
return count($this->group);
}
}
}
40 changes: 40 additions & 0 deletions test/tests/QueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Aternos\Model\Test\Tests;

use Aternos\Model\Query\SelectQuery;
use Aternos\Model\Query\WhereCondition;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use UnexpectedValueException;

class QueryTest extends TestCase
{
public function testSelectWhereConditionINNotArray()
{
$this->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'],
]);
}
}
Loading