ComparisonOperator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 80.95%

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidQueryDynamoDbOperator() 0 3 1
A getQuerySupportedOperators() 0 15 2
A isValidQueryOperator() 0 5 1
A is() 0 4 1
A isValidOperator() 0 7 1
A getDynamoDbOperator() 0 7 1
A getSupportedOperators() 0 3 1
A getOperatorMapping() 0 16 1
1
<?php
2
3
namespace BaoPham\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 76
    public static function getOperatorMapping()
25
    {
26
        return [
27 76
            '=' => static::EQ,
28 76
            '>' => static::GT,
29 76
            '>=' => static::GE,
30 76
            '<' => static::LT,
31 76
            '<=' => static::LE,
32 76
            'in' => static::IN,
33 76
            '!=' => static::NE,
34 76
            'begins_with' => static::BEGINS_WITH,
35 76
            'between' => static::BETWEEN,
36 76
            'not_contains' => static::NOT_CONTAINS,
37 76
            'contains' => static::CONTAINS,
38 76
            'null' => static::NULL,
39 76
            'not_null' => static::NOT_NULL,
40
        ];
41
    }
42
43
    public static function getSupportedOperators()
44
    {
45
        return array_keys(static::getOperatorMapping());
46
    }
47
48 76
    public static function isValidOperator($operator)
49
    {
50 76
        $operator = strtolower($operator);
51
52 76
        $mapping = static::getOperatorMapping();
53
54 76
        return isset($mapping[$operator]);
55
    }
56
57 76
    public static function getDynamoDbOperator($operator)
58
    {
59 76
        $mapping = static::getOperatorMapping();
60
61 76
        $operator = strtolower($operator);
62
63 76
        return $mapping[$operator];
64
    }
65
66 78
    public static function getQuerySupportedOperators($isRangeKey = false)
67
    {
68 78
        if ($isRangeKey) {
69
            return [
70 12
                static::EQ,
71 12
                static::LE,
72 12
                static::LT,
73 12
                static::GE,
74 12
                static::GT,
75 12
                static::BEGINS_WITH,
76 12
                static::BETWEEN,
77
            ];
78
        }
79
80 78
        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 78
    public static function isValidQueryDynamoDbOperator($dynamoDbOperator, $isRangeKey = false)
91
    {
92 78
        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