Order::map()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Compolomus\LSQLQueryBuilder\Parts;
4
5
use Compolomus\LSQLQueryBuilder\BuilderException;
6
use Compolomus\LSQLQueryBuilder\System\Traits\{
7
    Helper,
8
    Caller
9
};
10
11
class Order
12
{
13
    use Caller, Helper;
14
15
    private $asc = [];
16
17
    private $desc = [];
18
19
    public function __construct(array $fields = [], string $type = 'asc')
20
    {
21 View Code Duplication
        if (!\in_array(strtolower($type), ['asc', 'desc'], true)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
            throw new BuilderException('Передан неверный тип ' . $type . ' |ORDER add|');
23
        }
24
        if (\count($fields)) {
25
            $this->map($fields, $type);
26
        }
27
    }
28
29
    private function map(array $fields, string $type = 'asc'): void
30
    {
31
        array_map([$this, 'add'], $fields, array_fill(0, \count($fields), $type));
32
    }
33
34
35
    public function add(string $field, string $type = 'asc'): Order
36
    {
37
        $this->$type[] = $field;
38
        return $this;
39
    }
40
41
    public function desc(array $desc): Order
42
    {
43
        $this->map($desc, 'desc');
44
        return $this;
45
    }
46
47
    public function asc(array $asc): Order
48
    {
49
        $this->map($asc, 'asc');
50
        return $this;
51
    }
52
53
    public function result(): string
54
    {
55
        $order = '';
56
        $asc = $this->concatOrder($this->asc, 'asc');
57
        $desc = $this->concatOrder($this->desc, 'desc');
58
        if ($asc | $desc) {
59
            $order = 'ORDER BY ' . $asc . ($asc & $desc ? ',' : '') . $desc;
60
        }
61
        return $order;
62
    }
63
}
64