ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
OrderedJobEntities.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24{
25 final public const int ORDER_BY_NONE = 0;
26 final public const int ORDER_BY_NAME = 1;
27 final public const int ORDER_BY_STATUS = 2;
28
29 final public const int ORDER_AS_PROVIDED = 1;
30 final public const int ORDER_REVERSE = -1;
31
33 private mixed $sort;
37 private ?array $sorted_jobs = null;
38
42 public function __construct(
43 private readonly \ILIAS\Cron\Job\JobCollection $origin,
44 mixed $sort,
45 bool $reverse_order = false
46 ) {
47 $order = $reverse_order ? self::ORDER_REVERSE : self::ORDER_AS_PROVIDED;
48
49 if ($sort === self::ORDER_BY_NAME) {
50 $this->sort = static function (\ILIAS\Cron\Job\JobEntity $left, \ILIAS\Cron\Job\JobEntity $right) use (
51 $order
52 ): int {
53 return $order * \ilStr::strCmp($left->getEffectiveTitle(), $right->getEffectiveTitle());
54 };
55 } elseif ($sort === self::ORDER_BY_STATUS) {
56 $this->sort = static function (\ILIAS\Cron\Job\JobEntity $left, \ILIAS\Cron\Job\JobEntity $right) use (
57 $order
58 ): int {
59 return $order * ($right->getJobStatus() <=> $left->getJobStatus());
60 };
61 } elseif ($sort === self::ORDER_BY_NONE) {
62 $this->sort = $order;
63 } elseif (\is_callable($sort)) {
64 $this->sort = $sort;
65 if ($reverse_order) {
66 $this->sort = static fn(
67 \ILIAS\Cron\Job\JobEntity $left,
68 \ILIAS\Cron\Job\JobEntity $right
69 ): int => -$sort($left, $right);
70 }
71 } else {
72 throw new \InvalidArgumentException(
73 'The SortableIterator takes a PHP callable or a valid built-in sort algorithm as an argument.'
74 );
75 }
76 }
77
81 public function getIterator(): \ArrayIterator
82 {
83 return new \ArrayIterator($this->getSortedJobs());
84 }
85
86 public function count(): int
87 {
88 return $this->origin->count();
89 }
90
91 public function add(\ILIAS\Cron\Job\JobEntity $job): void
92 {
93 $this->origin->add($job);
94 $this->sorted_jobs = null;
95 }
96
97 public function filter(callable $callable): static
98 {
99 return new static(
100 $this->origin->filter($callable),
102 );
103 }
104
105 public function slice(int $offset, ?int $length = null): static
106 {
107 return new static(
108 new JobEntities(...\array_slice($this->getSortedJobs(), $offset, $length)),
110 );
111 }
112
113 public function toArray(): array
114 {
115 return $this->getSortedJobs();
116 }
117
121 private function getSortedJobs(): array
122 {
123 if ($this->sorted_jobs === null) {
124 $list = iterator_to_array($this->origin->toArray(), false);
125 if ($this->sort !== self::ORDER_AS_PROVIDED) {
126 if ($this->sort === self::ORDER_REVERSE) {
127 $list = array_reverse($list);
128 } else {
129 uasort($list, $this->sort);
130 }
131 }
132 $this->sorted_jobs = $list;
133 }
134
135 return $this->sorted_jobs;
136 }
137}
filter(callable $callable)
Returns all the elements of this collection that satisfy the predicate $callable.
__construct(private readonly \ILIAS\Cron\Job\JobCollection $origin, mixed $sort, bool $reverse_order=false)
slice(int $offset, ?int $length=null)
Extracts a slice of $length elements starting at position $offset from the Collection.
static strCmp(string $a, string $b)
Definition: class.ilStr.php:87
@template-extends \IteratorAggregate<\ILIAS\Cron\Job\JobEntity>
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.