ComparisonOperator::getDynamoDbOperator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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