Skip to content

Commit 98e4bb3

Browse files
committed
feat: added criterion and criteria
Signed-off-by: Julien Guittard <julien.guittard@me.com>
1 parent 3a34072 commit 98e4bb3

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

src/Operation/Query/Criteria.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/**
4+
* This file is part of phayne-io/php-dynamodb and is proprietary and confidential.
5+
* Unauthorized copying of this file, via any medium is strictly prohibited.
6+
*
7+
* @see https://github.com/phayne-io/php-dynamodb for the canonical source repository
8+
* @copyright Copyright (c) 2024-2025 Phayne Limited. (https://phayne.io)
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace Phayne\DynamoDB\Operation\Query;
14+
15+
/**
16+
* Class Criteria
17+
*
18+
* @package Phayne\DynamoDB\Operation\Query
19+
*/
20+
final readonly class Criteria
21+
{
22+
private array $criteria;
23+
24+
public function __construct(Criterion ...$criteria)
25+
{
26+
$this->criteria = $criteria;
27+
}
28+
29+
public static function from(Criterion ...$criteria): Criteria
30+
{
31+
return new self(...$criteria);
32+
}
33+
34+
public function toArray(): array
35+
{
36+
$criteria = [];
37+
foreach ($this->criteria as $criterion) {
38+
$criteria[$criterion->field] = [
39+
'operator' => $criterion->operator->value,
40+
'value' => $criterion->value,
41+
];
42+
}
43+
44+
return $criteria;
45+
}
46+
}

src/Operation/Query/Criterion.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* This file is part of phayne-io/php-dynamodb and is proprietary and confidential.
5+
* Unauthorized copying of this file, via any medium is strictly prohibited.
6+
*
7+
* @see https://github.com/phayne-io/php-dynamodb for the canonical source repository
8+
* @copyright Copyright (c) 2024-2025 Phayne Limited. (https://phayne.io)
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace Phayne\DynamoDB\Operation\Query;
14+
15+
use Phayne\DynamoDB\Enum\Operator;
16+
17+
/**
18+
* Class Criterion
19+
*
20+
* @package Phayne\DynamoDB\Operation\Query
21+
*/
22+
final readonly class Criterion
23+
{
24+
private function __construct(
25+
public Operator $operator,
26+
public string $field,
27+
public mixed $value,
28+
) {
29+
}
30+
31+
public static function from(Operator $operator, string $field, mixed $value): Criterion
32+
{
33+
return new self($operator, $field, $value);
34+
}
35+
36+
public function toArray(bool $fieldAsKey = false): array
37+
{
38+
return $fieldAsKey
39+
? [$this->field => ['operator' => $this->operator->value, 'value' => $this->value]]
40+
: ['operator' => $this->operator->value, 'name' => $this->field, 'value' => $this->value];
41+
}
42+
}

0 commit comments

Comments
 (0)