Issues (590)

src/Query/Extension/OrderableTrait.php (4 issues)

1
<?php
2
3
namespace Bdf\Prime\Query\Extension;
4
5
use Bdf\Prime\Query\Contract\Orderable;
6
7
/**
8
 * Trait for @see Orderable
9
 *
10
 * @property array{orders:array<array{sort:string,order:Orderable::ORDER_*}>} $statements
11
 *
12
 * @psalm-require-implements Orderable
13
 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{orders:array<array...r:Orderable::ORDER_*}>} at position 14 could not be parsed: Expected '}' at position 14, but found 'Orderable'.
Loading history...
14
trait OrderableTrait
15
{
16
    /**
17
     * {@inheritdoc}
18
     *
19
     * @see Orderable::order()
20
     */
21 65
    public function order($sort, ?string $order = null)
22
    {
23 65
        $this->compilerState->invalidate('orders');
24
25 65
        $this->statements['orders'] = [];
0 ignored issues
show
Bug Best Practice introduced by
The property statements does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
26
27 65
        if (!is_array($sort)) {
28 64
            $sort = [$sort => $order];
29
        }
30
31 65
        foreach ($sort as $column => $order) {
32 65
            if (is_int($column)) {
33 1
                $column = $order;
34 1
                $order = Orderable::ORDER_ASC;
35
            }
36
37 65
            $this->statements['orders'][] = [
38 65
                'sort'  => $column,
39 65
                'order' => !$order ? Orderable::ORDER_ASC : $order,
40 65
            ];
41
        }
42
43 65
        return $this;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     *
49
     * @see Orderable::addOrder()
50
     */
51 1
    public function addOrder($sort, ?string $order = null)
52
    {
53 1
        $this->compilerState->invalidate('orders');
54
55 1
        if (!is_array($sort)) {
56 1
            $sort = [$sort => $order];
57
        }
58
59 1
        foreach ($sort as $column => $order) {
60 1
            if (is_int($column)) {
61
                $column = $order;
62
                $order = Orderable::ORDER_ASC;
63
            }
64
65 1
            $this->statements['orders'][] = [
0 ignored issues
show
Bug Best Practice introduced by
The property statements does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
66 1
                'sort'  => $column,
67 1
                'order' => !$order ? Orderable::ORDER_ASC : $order,
68 1
            ];
69
        }
70
71 1
        return $this;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     *
77
     * @see Orderable::getOrders()
78
     * @return array<string, Orderable::ORDER_*>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, Orderable::ORDER_*> at position 4 could not be parsed: Expected '>' at position 4, but found 'Orderable'.
Loading history...
79
     */
80 21
    public function getOrders(): array
81
    {
82 21
        $orders = [];
83
84 21
        foreach ($this->statements['orders'] as $part) {
85 16
            $orders[$part['sort']] = $part['order'];
86
        }
87
88 21
        return $orders;
89
    }
90
}
91