Delete::orderBy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace QB\MySQL\Statement;
6
7
use QB\Generic\Statement\Delete as GenericDelete;
8
use QB\Generic\Statement\ISelect;
9
10
class Delete extends GenericDelete
11
{
12
    public const LOW_PRIORITY = 'LOW_PRIORITY';
13
    public const QUICK        = 'QUICK';
14
    public const IGNORE       = 'IGNORE';
15
16
    /** @var array<int,string> */
17
    protected array $modifiers = [];
18
19
    /** @var array<string,string> */
20
    protected array $orderByParts = [];
21
22
    protected ?int $limit = null;
23
24
    /**
25
     * @param string ...$modifiers
26
     *
27
     * @return $this
28
     */
29 1
    public function modifier(string ...$modifiers): static
30
    {
31 1
        foreach ($modifiers as $modifier) {
32
            switch ($modifier) {
33 1
                case static::LOW_PRIORITY:
34 1
                    $this->modifiers[0] = $modifier;
35 1
                    break;
36 1
                case static::QUICK:
37 1
                    $this->modifiers[1] = $modifier;
38 1
                    break;
39 1
                case static::IGNORE:
40 1
                    $this->modifiers[2] = $modifier;
41 1
                    break;
42
            }
43
        }
44
45 1
        return $this;
46
    }
47
48
    /**
49
     * @param string $column
50
     * @param string $direction
51
     *
52
     * @return $this
53
     */
54 1
    public function orderBy(string $column, string $direction = ISelect::DIRECTION_ASC): static
55
    {
56 1
        $this->orderByParts[$column] = $direction;
57
58 1
        return $this;
59
    }
60
61
    /**
62
     * @param int|null $limit
63
     *
64
     * @return $this
65
     */
66 1
    public function limit(?int $limit): static
67
    {
68 1
        $this->limit = $limit;
69
70 1
        return $this;
71
    }
72
73
    /**
74
     * @return string
75
     */
76 5
    public function __toString(): string
77
    {
78 5
        $parts = array_merge(
79 5
            [parent::__toString()],
80 4
            $this->getOrderBy(),
81 4
            $this->getLimit(),
82
        );
83
84 4
        return implode(PHP_EOL, $parts);
85
    }
86
87
    /**
88
     * @return string[]
89
     */
90 4
    protected function getOrderBy(): array
91
    {
92 4
        if (count($this->orderByParts) === 0) {
93 3
            return [];
94
        }
95
96 1
        $parts = [];
97 1
        foreach ($this->orderByParts as $column => $direction) {
98 1
            $parts[] = "$column $direction";
99
        }
100
101 1
        return ['ORDER BY ' . implode(', ', $parts)];
102
    }
103
104 4
    protected function getLimit(): array
105
    {
106 4
        $parts = [];
107 4
        if ($this->limit !== null) {
108 1
            $parts[] = sprintf('LIMIT %d', $this->limit);
109
        }
110
111 4
        return $parts;
112
    }
113
}
114