ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
Order.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 namespace ILIAS\Data;
6 
12 class Order
13 {
14  public const ASC = 'ASC';
15  public const DESC = 'DESC';
16 
20  protected array $order = [];
21 
22  public function __construct(string $subject, string $direction)
23  {
24  $this->checkDirection($direction);
25  $this->order[$subject] = $direction;
26  }
27 
28  protected function checkSubject(string $subject): void
29  {
30  if (array_key_exists($subject, $this->order)) {
31  throw new \InvalidArgumentException("already sorted by subject '$subject'", 1);
32  }
33  }
34 
35  protected function checkDirection(string $direction): void
36  {
37  if ($direction !== self::ASC && $direction !== self::DESC) {
38  throw new \InvalidArgumentException("Direction bust be Order::ASC or Order::DESC.", 1);
39  }
40  }
41 
42  public function append(string $subject, string $direction): Order
43  {
44  $this->checkSubject($subject);
45  $this->checkDirection($direction);
46  $clone = clone $this;
47  $clone->order[$subject] = $direction;
48  return $clone;
49  }
50 
54  public function get(): array
55  {
56  return $this->order;
57  }
58 
59  public function join($init, callable $fn)
60  {
61  $ret = $init;
62  foreach ($this->order as $key => $value) {
63  $ret = $fn($ret, $key, $value);
64  }
65  return $ret;
66  }
67 }
array $order
Definition: Order.php:20
join($init, callable $fn)
Definition: Order.php:59
checkDirection(string $direction)
Definition: Order.php:35
checkSubject(string $subject)
Definition: Order.php:28
__construct(string $subject, string $direction)
Definition: Order.php:22
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:12
string $key
Consumer key/client ID value.
Definition: System.php:193
append(string $subject, string $direction)
Definition: Order.php:42
const DESC
Definition: Order.php:15