Completed
Pull Request — master (#212)
by Alexandru
43:58 queued 01:14
created

ComparisonOperator::getOperatorMapping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 16
ccs 14
cts 14
cp 1
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Rennokki\DynamoDb;
4
5
/**
6
 * Class DynamoDbOperator.
7
 */
8
class ComparisonOperator
9
{
10
    const EQ = 'EQ';
11
    const GT = 'GT';
12
    const GE = 'GE';
13
    const LT = 'LT';
14
    const LE = 'LE';
15
    const IN = 'IN';
16
    const NE = 'NE';
17
    const BEGINS_WITH = 'BEGINS_WITH';
18
    const BETWEEN = 'BETWEEN';
19
    const NOT_CONTAINS = 'NOT_CONTAINS';
20
    const CONTAINS = 'CONTAINS';
21
    const NULL = 'NULL';
22
    const NOT_NULL = 'NOT_NULL';
23
24 7
    public static function getOperatorMapping()
25
    {
26
        return [
27 7
            '=' => static::EQ,
28 7
            '>' => static::GT,
29 7
            '>=' => static::GE,
30 7
            '<' => static::LT,
31 7
            '<=' => static::LE,
32 7
            'in' => static::IN,
33 7
            '!=' => static::NE,
34 7
            'begins_with' => static::BEGINS_WITH,
35 7
            'between' => static::BETWEEN,
36 7
            'not_contains' => static::NOT_CONTAINS,
37 7
            'contains' => static::CONTAINS,
38 7
            'null' => static::NULL,
39 7
            'not_null' => static::NOT_NULL,
40
        ];
41
    }
42
43
    public static function getSupportedOperators()
44
    {
45
        return array_keys(static::getOperatorMapping());
46
    }
47
48 7
    public static function isValidOperator($operator)
49
    {
50 7
        $operator = strtolower($operator);
51
52 7
        $mapping = static::getOperatorMapping();
53
54 7
        return isset($mapping[$operator]);
55
    }
56
57 7
    public static function getDynamoDbOperator($operator)
58
    {
59 7
        $mapping = static::getOperatorMapping();
60
61 7
        $operator = strtolower($operator);
62
63 7
        return $mapping[$operator];
64
    }
65
66 5
    public static function getQuerySupportedOperators($isRangeKey = false)
67
    {
68 5
        if ($isRangeKey) {
69
            return [
70 2
                static::EQ,
71 2
                static::LE,
72 2
                static::LT,
73 2
                static::GE,
74 2
                static::GT,
75 2
                static::BEGINS_WITH,
76 2
                static::BETWEEN,
77
            ];
78
        }
79
80 5
        return [static::EQ];
81
    }
82
83
    public static function isValidQueryOperator($operator, $isRangeKey = false)
84
    {
85
        $dynamoDbOperator = static::getDynamoDbOperator($operator);
86
87
        return static::isValidQueryDynamoDbOperator($dynamoDbOperator, $isRangeKey);
88
    }
89
90 5
    public static function isValidQueryDynamoDbOperator($dynamoDbOperator, $isRangeKey = false)
91
    {
92 5
        return in_array($dynamoDbOperator, static::getQuerySupportedOperators($isRangeKey));
93
    }
94
95
    public static function is($op, $dynamoDbOperator)
96
    {
97
        $mapping = static::getOperatorMapping();
98
99
        return $mapping[strtolower($op)] === $dynamoDbOperator;
100
    }
101
}
102