Completed
Push — master ( 20a668...02e94f )
by Guillermo A.
02:17
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
dl 0
loc 19
c 0
b 0
f 0
ccs 16
cts 16
cp 1
rs 8.4444
cc 8
nc 8
nop 2
crap 8
1
<?php
2
3
namespace Guillermoandrae\DynamoDb;
4
5
use ErrorException;
6
7
abstract class AbstractFilterExpressionAwareRequest extends AbstractItemRequest
8
{
9
    use LimitAwareRequestTrait;
10
11
    /**
12
     * @var string The filter expression.
13
     */
14
    protected $filterExpression;
15
16
    /**
17
     * Registers the filter expression with this object.
18
     *
19
     * @param array $data The filter expression data.
20
     * @return AbstractFilterExpressionAwareRequest An implementation of this abstract.
21
     * @throws ErrorException
22
     */
23 6
    final public function setFilterExpression(array $data): AbstractFilterExpressionAwareRequest
24
    {
25 6
        $filterExpressionArray = [];
26 6
        foreach ($data as $key => $options) {
27 6
            $filterExpressionArray[] = $this->parseExpression($options['operator'], $key);
28 5
            $this->addExpressionAttributeValue($key, $options['value']);
29
        }
30 5
        $this->filterExpression = implode(' and ', $filterExpressionArray);
31 5
        return $this;
32
    }
33
34
    /**
35
     * {@inheritDoc}
36
     */
37 21
    public function get(): array
38
    {
39 21
        $query = parent::get();
40 21
        if ($this->limit) {
41 3
            $query['Limit'] = $this->limit;
42
        }
43 21
        if ($this->filterExpression) {
44 5
            $query['FilterExpression'] = $this->filterExpression;
45
        }
46 21
        return $query;
47
    }
48
49
    /**
50
     * Uses the operator to build the filter expression.
51
     *
52
     * @param string $operator The request operator.
53
     * @param string $key The attribute key.
54
     * @return string The expression.
55
     * @throws ErrorException
56
     */
57 9
    protected function parseExpression(string $operator, string $key): string
58
    {
59
        switch ($operator) {
60 9
            case RequestOperators::BEGINS_WITH:
61 1
                return sprintf('begins_with(%s, :%s)', $key, $key);
62 9
            case RequestOperators::CONTAINS:
63 1
                return sprintf('contains(%s, :%s)', $key, $key);
64 9
            case RequestOperators::EQ:
65 4
                return sprintf('%s = :%s', $key, $key);
66 7
            case RequestOperators::GT:
67 1
                return sprintf('%s > :%s', $key, $key);
68 6
            case RequestOperators::GTE:
69 3
                return sprintf('%s >= :%s', $key, $key);
70 3
            case RequestOperators::LT:
71 1
                return sprintf('%s < :%s', $key, $key);
72 2
            case RequestOperators::LTE:
73 1
                return sprintf('%s <= :%s', $key, $key);
74
            default:
75 1
                throw new ErrorException('The provided operator is not supported.');
76
        }
77
    }
78
}
79