ILIAS  release_7 Revision v7.30-3-g800a261c036
Order.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 
3 namespace ILIAS\Data;
4 
10 class Order
11 {
12  const ASC = 'ASC';
13  const DESC = 'DESC';
14 
18  protected $order = [];
19 
20  public function __construct(string $subject, $direction)
21  {
22  $this->checkDirection($direction);
23  $this->order[$subject] = $direction;
24  }
25 
26  protected function checkSubject(string $subject)
27  {
28  if (array_key_exists($subject, $this->order)) {
29  throw new \InvalidArgumentException("already sorted by subject '$subject'", 1);
30  }
31  }
32 
33  protected function checkDirection($direction)
34  {
35  if ($direction !== self::ASC && $direction !== self::DESC) {
36  throw new \InvalidArgumentException("Direction bust be Order::ASC or Order::DESC.", 1);
37  }
38  }
39 
40  public function append(string $subject, $direction) : Order
41  {
42  $this->checkSubject($subject);
43  $this->checkDirection($direction);
44  $clone = clone $this;
45  $clone->order[$subject] = $direction;
46  return $clone;
47  }
48 
49  public function get() : array
50  {
51  return $this->order;
52  }
53 
54  public function join($init, callable $fn)
55  {
56  $ret = $init;
57  foreach ($this->order as $key => $value) {
58  $ret = $fn($ret, $key, $value);
59  }
60  return $ret;
61  }
62 }
checkDirection($direction)
Definition: Order.php:33
join($init, callable $fn)
Definition: Order.php:54
checkSubject(string $subject)
Definition: Order.php:26
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:10
__construct(string $subject, $direction)
Definition: Order.php:20
append(string $subject, $direction)
Definition: Order.php:40
$ret
Definition: parser.php:6
const DESC
Definition: Order.php:13