parseExpression()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8

Importance

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