Passed
Push — master ( 05e825...103a6d )
by compolom
01:37
created

Order::asc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace Compolomus\LSQLQueryBuilder\Parts;
4
5
use Compolomus\LSQLQueryBuilder\System\Traits\{
6
    Helper,
7
    Caller
8
};
9
10
class Order
11
{
12
    use Caller, Helper;
13
14
    private $asc = [];
15
16
    private $desc = [];
17
18
    public function __construct(array $fields = [], string $type = 'asc')
19
    {
20
        if (!in_array(strtolower($type), ['asc', 'desc'])) {
21
            throw new \InvalidArgumentException('Передан неверный тип |ORDER add|');
22
        }
23
        if (count($fields)) {
24
            $this->map($fields, $type);
25
        }
26
    }
27
28
    private function map(array $fields, string $type = 'asc'): void
29
    {
30
        array_map([$this, 'add'], $fields, array_fill(0, count($fields), $type));
31
    }
32
33
34
    public function add(string $field, string $type = 'asc'): Order
35
    {
36
        $this->$type[] = $field;
37
        return $this;
38
    }
39
40
    public function desc(array $desc): Order
41
    {
42
        $this->map($desc, 'desc');
43
        return $this;
44
    }
45
46
    public function asc(array $asc): Order
47
    {
48
        $this->map($asc, 'asc');
49
        return $this;
50
    }
51
52
    public function result(): string
53
    {
54
        $order = '';
55
        $asc = $this->concatOrder($this->asc, 'asc');
56
        $desc = $this->concatOrder($this->desc, 'desc');
57
        if ($asc | $desc) {
58
            $order = 'ORDER BY ' . $asc . ($asc & $desc ? ',' : '') . $desc;
59
        }
60
        return $order;
61
    }
62
}
63