1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Guillermoandrae\DynamoDb\Contract; |
4
|
|
|
|
5
|
|
|
use ErrorException; |
6
|
|
|
use Guillermoandrae\DynamoDb\Constant\Operators; |
7
|
|
|
|
8
|
|
|
trait FilterExpressionAwareOperationTrait |
9
|
|
|
{ |
10
|
|
|
use DynamoDbClientAwareTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string The filter expression. |
14
|
|
|
*/ |
15
|
|
|
protected $filterExpression; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array Values that can be substituted in an expression. |
19
|
|
|
*/ |
20
|
|
|
protected $expressionAttributeValues = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Registers the filter expression with this object. |
24
|
|
|
* |
25
|
|
|
* @param array $data The filter expression data. |
26
|
|
|
* @return mixed This object. |
27
|
|
|
* @throws ErrorException |
28
|
|
|
*/ |
29
|
6 |
|
final public function setFilterExpression(array $data) |
30
|
|
|
{ |
31
|
6 |
|
$filterExpressionArray = []; |
32
|
6 |
|
foreach ($data as $key => $options) { |
33
|
6 |
|
$filterExpressionArray[] = $this->parseExpression($options['operator'], $key); |
34
|
5 |
|
$this->addExpressionAttributeValue($key, $options['value']); |
35
|
|
|
} |
36
|
5 |
|
$this->filterExpression = implode(' and ', $filterExpressionArray); |
37
|
5 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Uses the operator to build the filter expression. |
42
|
|
|
* |
43
|
|
|
* @param string $operator The request operator. |
44
|
|
|
* @param string $key The attribute key. |
45
|
|
|
* @return string The expression. |
46
|
|
|
* @throws ErrorException |
47
|
|
|
*/ |
48
|
9 |
|
protected function parseExpression(string $operator, string $key): string |
49
|
|
|
{ |
50
|
|
|
switch ($operator) { |
51
|
9 |
|
case Operators::BEGINS_WITH: |
52
|
1 |
|
return sprintf('begins_with(%s, :%s)', $key, $key); |
53
|
9 |
|
case Operators::CONTAINS: |
54
|
1 |
|
return sprintf('contains(%s, :%s)', $key, $key); |
55
|
9 |
|
case Operators::EQ: |
56
|
4 |
|
return sprintf('%s = :%s', $key, $key); |
57
|
7 |
|
case Operators::GT: |
58
|
1 |
|
return sprintf('%s > :%s', $key, $key); |
59
|
6 |
|
case Operators::GTE: |
60
|
3 |
|
return sprintf('%s >= :%s', $key, $key); |
61
|
3 |
|
case Operators::LT: |
62
|
1 |
|
return sprintf('%s < :%s', $key, $key); |
63
|
2 |
|
case Operators::LTE: |
64
|
1 |
|
return sprintf('%s <= :%s', $key, $key); |
65
|
|
|
default: |
66
|
1 |
|
throw new ErrorException('The provided operator is not supported.'); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Adds an ExpressionAttributeValue to the request. |
72
|
|
|
* |
73
|
|
|
* @param string $key The attribute token. |
74
|
|
|
* @param mixed $value The attribute value. |
75
|
|
|
* @return mixed This object. |
76
|
|
|
*/ |
77
|
8 |
|
final public function addExpressionAttributeValue(string $key, $value) |
78
|
|
|
{ |
79
|
8 |
|
$this->expressionAttributeValues[sprintf(':%s', $key)] = $this->getMarshaler()->marshalValue($value); |
80
|
8 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritDoc} |
85
|
|
|
*/ |
86
|
23 |
|
public function toArray(): array |
87
|
|
|
{ |
88
|
23 |
|
$query = []; |
89
|
23 |
|
if (!empty($this->filterExpression)) { |
90
|
5 |
|
$query['FilterExpression'] = $this->filterExpression; |
91
|
|
|
} |
92
|
23 |
|
if (!empty($this->expressionAttributeValues)) { |
93
|
8 |
|
$query['ExpressionAttributeValues'] = $this->expressionAttributeValues; |
94
|
|
|
} |
95
|
23 |
|
return $query; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|