Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
Order
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
6 / 6
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 empty
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 parse
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 buildSortField
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Projom\Storage\SQL\Component;
6
7use Projom\Storage\SQL\Component\ComponentInterface;
8use Projom\Storage\SQL\Util;
9use Projom\Storage\SQL\Util\Sort;
10
11class Order implements ComponentInterface
12{
13    private readonly array $orders;
14
15    public function __construct(array $sortFields)
16    {
17        $this->parse($sortFields);
18    }
19
20    public static function create(array $sortFields): Order
21    {
22        return new Order($sortFields);
23    }
24
25    public function __toString(): string
26    {
27        return Util::join($this->orders, ', ');
28    }
29
30    public function empty(): bool
31    {
32        return empty($this->orders);
33    }
34
35    private function parse(array $sortFields): void
36    {
37        $orders = [];
38        foreach ($sortFields as [$field, $sort])
39            $orders[] = $this->buildSortField($field, $sort);
40
41        $this->orders = $orders;
42    }
43
44    private function buildSortField(string $field, Sort $sort): string
45    {
46        $sortUC = strtoupper($sort->value);
47        $quotedField = Util::splitAndQuoteThenJoin($field, '.');
48        return "{$quotedField} {$sortUC}";
49    }
50}