ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Order.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Data;
22
28class Order
29{
30 public const ASC = 'ASC';
31 public const DESC = 'DESC';
32
36 protected array $order = [];
37
38 public function __construct(string $subject, string $direction)
39 {
40 $this->checkDirection($direction);
41 $this->order[$subject] = $direction;
42 }
43
44 protected function checkSubject(string $subject): void
45 {
46 if (array_key_exists($subject, $this->order)) {
47 throw new \InvalidArgumentException("already sorted by subject '$subject'", 1);
48 }
49 }
50
51 protected function checkDirection(string $direction): void
52 {
53 if ($direction !== self::ASC && $direction !== self::DESC) {
54 throw new \InvalidArgumentException("Direction bust be Order::ASC or Order::DESC.", 1);
55 }
56 }
57
58 public function append(string $subject, string $direction): Order
59 {
60 $this->checkSubject($subject);
61 $this->checkDirection($direction);
62 $clone = clone $this;
63 $clone->order[$subject] = $direction;
64 return $clone;
65 }
66
70 public function get(): array
71 {
72 return $this->order;
73 }
74
75 public function join($init, callable $fn)
76 {
77 $ret = $init;
78 foreach ($this->order as $key => $value) {
79 $ret = $fn($ret, $key, $value);
80 }
81 return $ret;
82 }
83}
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
array $order
Definition: Order.php:36
append(string $subject, string $direction)
Definition: Order.php:58
join($init, callable $fn)
Definition: Order.php:75
__construct(string $subject, string $direction)
Definition: Order.php:38
checkDirection(string $direction)
Definition: Order.php:51
checkSubject(string $subject)
Definition: Order.php:44
const DESC
Definition: Order.php:31