Completed
Push — master ( 19c355...d500cb )
by Guillermo A.
02:33
created

parseExpression()   B

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
c 0
b 0
f 0
dl 0
loc 19
ccs 16
cts 16
cp 1
rs 8.4444
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
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