Completed
Pull Request — master (#212)
by Alexandru
45:00 queued 03:00
created

ComparisonOperator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 80.95%

Importance

Changes 0
Metric Value
wmc 9
eloc 50
dl 0
loc 91
ccs 34
cts 42
cp 0.8095
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A is() 0 4 1
A isValidQueryOperator() 0 5 1
A getSupportedOperators() 0 3 1
A isValidQueryDynamoDbOperator() 0 3 1
A isValidOperator() 0 7 1
A getDynamoDbOperator() 0 7 1
A getQuerySupportedOperators() 0 15 2
A getOperatorMapping() 0 16 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
        return $mapping[strtolower($op)] === $dynamoDbOperator;
99
    }
100
}
101