ILIAS  trunk Revision v11.0_alpha-1846-g895b5f47236
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
trait.TableRecords.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ILIAS\Data;
25 
30 trait TableRecords
31 {
32  protected function orderRecords(array $records, Data\Order $order): array
33  {
34  [$aspect, $direction] = $order->join("", function ($i, $k, $v) {
35  return [$k, $v];
36  });
37  usort($records, static function (array $a, array $b) use ($aspect): int {
38  if (!isset($a[$aspect]) && !isset($b[$aspect])) {
39  return 0;
40  }
41  if (!isset($a[$aspect])) {
42  return -1;
43  }
44  if (!isset($b[$aspect])) {
45  return 1;
46  }
47  if (is_numeric($a[$aspect]) || is_bool($a[$aspect])) {
48  return $a[$aspect] <=> $b[$aspect];
49  }
50  if (is_array($a[$aspect])) {
51  return $a[$aspect] <=> $b[$aspect];
52  }
53  if ($a[$aspect] instanceof Link) {
54  return $a[$aspect]->getLabel() <=> $b[$aspect]->getLabel();
55  }
56  if ($a[$aspect] instanceof \DateTimeImmutable) {
57  return $a[$aspect]->getTimestamp() <=> $b[$aspect]->getTimestamp();
58  }
59 
60  return strcmp($a[$aspect], $b[$aspect]);
61  });
62 
63  if ($direction === $order::DESC) {
64  $records = array_reverse($records);
65  }
66  return $records;
67  }
68 
69  protected function limitRecords(array $records, Data\Range $range): array
70  {
71  $records = array_slice($records, $range->getStart(), $range->getLength());
72 
73  return $records;
74  }
75 }
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples