ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
NewsCriteria.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\News\Data;
22
23use DateTimeImmutable;
24
28final class NewsCriteria implements \JsonSerializable
29{
30 public function __construct(
31 private ?int $period = null,
32 private bool $only_public = false,
33 private ?int $min_priority = null,
34 private ?int $max_priority = null,
35 private ?int $limit = null,
36 private bool $prevent_nesting = false,
37 private bool $no_auto_generated = false,
38 private array $excluded_news_ids = [],
39 private ?bool $include_read_status = null,
40 private ?int $read_user_id = null,
42 private array $start_dates = [],
43 ) {
44 // By default, include read status if only public is not set
45 $this->include_read_status = $this->include_read_status ?? !$this->only_public;
46 }
47
48 /*
49 Getters and Setters
50 */
51
52 public function getPeriod(): ?int
53 {
54 return $this->period;
55 }
56
57 public function isOnlyPublic(): bool
58 {
59 return $this->only_public;
60 }
61
62 public function getMinPriority(): ?int
63 {
64 return $this->min_priority;
65 }
66
67 public function getMaxPriority(): ?int
68 {
69 return $this->max_priority;
70 }
71
72 public function getLimit(): ?int
73 {
74 return $this->limit;
75 }
76
77 public function isPreventNesting(): bool
78 {
79 return $this->prevent_nesting;
80 }
81
82 public function isNoAutoGenerated(): bool
83 {
84 return $this->no_auto_generated;
85 }
86
87 public function getExcludedNewsIds(): array
88 {
89 return $this->excluded_news_ids;
90 }
91
92 public function isIncludeReadStatus(): bool
93 {
94 return $this->include_read_status ?? false;
95 }
96
97 public function getReadUserId(): ?int
98 {
99 return $this->read_user_id;
100 }
101
105 public function getStartDates(): array
106 {
107 return $this->start_dates;
108 }
109
110 public function withPeriod(?int $period): self
111 {
112 $new = clone $this;
113 $new->period = $period;
114 return $new;
115 }
116
117 public function withOnlyPublic(bool $only_public): self
118 {
119 $new = clone $this;
120 $new->only_public = $only_public;
121 return $new;
122 }
123
124 public function withMinPriority(?int $min_priority): self
125 {
126 $new = clone $this;
127 $new->min_priority = $min_priority;
128 return $new;
129 }
130
131 public function withMaxPriority(?int $max_priority): self
132 {
133 $new = clone $this;
134 $new->max_priority = $max_priority;
135 return $new;
136 }
137
138 public function withLimit(?int $limit): self
139 {
140 $new = clone $this;
141 $new->limit = $limit;
142 return $new;
143 }
144
145 public function withPreventNesting(bool $stop_nesting): self
146 {
147 $new = clone $this;
148 $new->prevent_nesting = $stop_nesting;
149 return $new;
150 }
151
152 public function withNoAutoGenerated(bool $no_auto_generated): self
153 {
154 $new = clone $this;
155 $new->no_auto_generated = $no_auto_generated;
156 return $new;
157 }
158
159 public function withExcludedNewsIds(array $excluded_news_ids): self
160 {
161 $new = clone $this;
162 $new->excluded_news_ids = array_map('intval', $excluded_news_ids);
163 return $new;
164 }
165
166 public function withIncludeReadStatus(bool $include_read_status): self
167 {
168 $new = clone $this;
169 $new->include_read_status = $include_read_status;
170 return $new;
171 }
172
173 public function withReadUserId(?int $read_user_id): self
174 {
175 $new = clone $this;
176 $new->read_user_id = $read_user_id;
177 return $new;
178 }
179
183 public function withStartDates(array $start_dates): self
184 {
185 $new = clone $this;
186 $new->start_dates = $start_dates;
187 return $new;
188 }
189
190 /*
191 Public Methods
192 */
193
194 public function jsonSerialize(): array
195 {
196 return $this->toArray();
197 }
198
199 public function toArray(): array
200 {
201 return [
202 'period' => $this->period,
203 'only_public' => $this->only_public,
204 'min_priority' => $this->min_priority,
205 'max_priority' => $this->max_priority,
206 'limit' => $this->limit,
207 'stop_nesting' => $this->prevent_nesting,
208 'no_auto_generated' => $this->no_auto_generated,
209 'excluded_news_ids' => $this->excluded_news_ids,
210 'include_read_status' => $this->include_read_status,
211 'read_user_id' => $this->read_user_id,
212 'start_dates' => $this->start_dates
213 ];
214 }
215
219 public function hasPriorityFilters(): bool
220 {
221 return $this->min_priority !== null || $this->max_priority !== null;
222 }
223
227 public function validate(): void
228 {
229 if ($this->min_priority !== null && $this->max_priority !== null && $this->min_priority > $this->max_priority) {
230 throw new \InvalidArgumentException('Min priority cannot be greater than max priority');
231 }
232
233 if ($this->limit !== null && $this->limit < 0) {
234 throw new \InvalidArgumentException('Limit cannot be negative');
235 }
236 }
237}
News Criteria DTO for querying news items supports caching, JSON serialization, and validation.
withMaxPriority(?int $max_priority)
withPreventNesting(bool $stop_nesting)
validate()
Validate criteria parameters.
withExcludedNewsIds(array $excluded_news_ids)
withNoAutoGenerated(bool $no_auto_generated)
withIncludeReadStatus(bool $include_read_status)
hasPriorityFilters()
Check if criteria has priority filters.
withMinPriority(?int $min_priority)
withReadUserId(?int $read_user_id)
withOnlyPublic(bool $only_public)
withStartDates(array $start_dates)
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76