ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Sorting.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Badge;
22
23use Closure;
24use ilBadge;
26
28{
29 private readonly string $method;
31 private readonly Closure $compare;
32 private readonly string $direction;
33 private readonly string $key;
34 private readonly string $badgeOrAssignment;
35
36 public function __construct(string $sort_by = '')
37 {
38 $parts = explode('_', $sort_by);
39 $what = $parts[0] ?? '';
40 $direction = $parts[1] ?? '';
41
42 $map = [
43 'title' => ['badge', 'getTitle', 'strcasecmp'],
44 'date' => ['assignment', 'getTimestamp', $this->minus()],
45 ];
46 $directions = ['asc' => 'asc', 'desc' => 'desc'];
47
48 $key = isset($map[$what]) ? $what : key($map);
49 $direction = $directions[$direction] ?? 'asc';
50
51 $this->badgeOrAssignment = $map[$key][0];
52 $this->method = $map[$key][1];
53 $this->compare = Closure::fromCallable($map[$key][2]);
54 $this->direction = $direction;
55 $this->key = $key . '_' . $direction;
56 }
57
63 public function compare(array $badge_and_assignment, array $other): int
64 {
66 $value = $badge_and_assignment[$this->badgeOrAssignment]->$method();
67 $other = $other[$this->badgeOrAssignment]->$method();
68
69 return $this->reverse() * ($this->compare)($value, $other);
70 }
71
72 public function key(): string
73 {
74 return $this->key;
75 }
76
77 public function label(): string
78 {
79 return $this->options()[$this->key];
80 }
81
85 public function options(): array
86 {
87 return [
88 'title_asc' => 'sort_by_title_asc',
89 'title_desc' => 'sort_by_title_desc',
90 'date_asc' => 'sort_by_date_asc',
91 'date_desc' => 'sort_by_date_desc',
92 ];
93 }
94
95 private function reverse(): int
96 {
97 return $this->direction === 'asc' ? 1 : -1;
98 }
99
100 private function minus(): Closure
101 {
102 return static fn(int $x, int $y): int => $x - $y;
103 }
104}
readonly Closure $compare
Definition: Sorting.php:31
__construct(string $sort_by='')
Definition: Sorting.php:36
readonly string $key
Definition: Sorting.php:33
readonly string $badgeOrAssignment
Definition: Sorting.php:34
readonly string $method
Definition: Sorting.php:29
compare(array $badge_and_assignment, array $other)
Definition: Sorting.php:63
readonly string $direction
Definition: Sorting.php:32
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61