Passed
Push — master ( 4c916a...c716e1 )
by compolom
01:44
created

src/System/Conditions.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php declare(strict_types=1);
2
3
namespace Compolomus\LSQLQueryBuilder\System;
4
5
class Conditions
6
{
7
    use Traits\Placeholders,
8
        Traits\Helper;
9
10
    private $conditionTypes = [
11
        '=',
12
        '!=', // <>
13
        '>',
14
        '<',
15
        '<=',
16
        '>=',
17
        'like',
18
        'not like',
19
        'regexp', // rlike
20
        'not regexp', // not rlike
21
        'in',
22
        'not in',
23
        'between',
24
        'not between',
25
    ];
26
27
    private $conditions = [];
28
29
    public function conditions(): array
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
30
    {
31
        return $this->conditions;
32
    }
33
34
    public function add(string $field, string $condition, $value): void
35
    {
36
        if (!in_array(strtolower($condition), $this->conditionTypes)) {
37
            throw new \InvalidArgumentException('Передан неверный тип |CONDITIONS add|');
38
        }
39
        $key = $this->uid('w');
40
        $value = $this->type($condition, $value);
41
        $this->placeholders()->set($key, $value);
42
        $this->conditions[] = $this->escapeField($field) . ' ' . strtoupper($condition) . ' :' . $key;
43
    }
44
45
    public function type(string $condition, $value)
46
    {
47
        return in_array($condition, ['in', 'not in'])
48
            ? '(' . implode(',', $value) . ')'
49
            : (
50
            in_array($condition, ['between', 'not between'])
51
                ? implode(' AND ', $value)
52
                : $value
53
            );
54
    }
55
}
56