Passed
Push — dev_2x ( 5f9618...4436d9 )
by Adrian
01:44
created

Operators   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 25
c 1
b 0
f 0
dl 0
loc 38
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B getOperatorAndValue() 0 22 9
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm\Query;
5
6
class Operators
7
{
8
    protected static $map = [
9
        'lt' => '<',
10
        'lte' => '<=',
11
        'less_than' => '<',
12
        'less_or_equal' => '<=',
13
        'gte' => '>=',
14
        'gt' => '>',
15
        'greater_or_equal' => '>=',
16
        'greater_than' => '>=',
17
        'starts_with' => 'LIKE',
18
        'ends_with' => 'LIKE',
19
        'contains' => 'LIKE',
20
    ];
21
22
    public static function getOperatorAndValue($operator, $value)
23
    {
24
        if (is_int($operator) || $operator === 'IN' || $operator === '=') {
25
            $value = strpos($value, ',') ? explode(',', $value) : $value;
26
            $operator = is_array($value) ? 'IN' : '=';
27
            return [$operator, $value];
28
        }
29
30
        if ($operator === 'starts_with') {
31
            $value = $value . '%';
32
        }
33
34
        if ($operator === 'ends_with') {
35
            $value = '%' . $value;
36
        }
37
38
        if ($operator === 'contains') {
39
            $value = '%' . $value . '%';
40
        }
41
42
        $operator = static::$map[$operator] ?? $operator;
43
        return [$operator, $value];
44
    }
45
}
46