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