ILIAS  trunk Revision v12.0_alpha-399-g579a087ced2
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 ?DateTimeImmutable $start_date = null,
32 private ?int $period = null,
33 private bool $only_public = false,
34 private ?int $min_priority = null,
35 private ?int $max_priority = null,
36 private ?int $limit = null,
37 private bool $prevent_nesting = false,
38 private bool $no_auto_generated = false,
39 private array $excluded_news_ids = [],
40 private ?bool $include_read_status = null,
41 private ?int $read_user_id = null,
42 ) {
43 // By default, include read status if only public is not set
44 $this->include_read_status = $this->include_read_status ?? !$this->only_public;
45 }
46
47 /*
48 Getters and Setters
49 */
50
51 public function getStartDate(): ?DateTimeImmutable
52 {
53 return $this->start_date;
54 }
55
56 public function getPeriod(): ?int
57 {
58 return $this->period;
59 }
60
61 public function isOnlyPublic(): bool
62 {
63 return $this->only_public;
64 }
65
66 public function getMinPriority(): ?int
67 {
68 return $this->min_priority;
69 }
70
71 public function getMaxPriority(): ?int
72 {
73 return $this->max_priority;
74 }
75
76 public function getLimit(): ?int
77 {
78 return $this->limit;
79 }
80
81 public function isPreventNesting(): bool
82 {
83 return $this->prevent_nesting;
84 }
85
86 public function isNoAutoGenerated(): bool
87 {
88 return $this->no_auto_generated;
89 }
90
91 public function getExcludedNewsIds(): array
92 {
93 return $this->excluded_news_ids;
94 }
95
96 public function isIncludeReadStatus(): bool
97 {
98 return $this->include_read_status ?? false;
99 }
100
101 public function getReadUserId(): ?int
102 {
103 return $this->read_user_id;
104 }
105
106 public function withStartDate(?DateTimeImmutable $start_date): self
107 {
108 $new = clone $this;
109 $new->start_date = $start_date;
110 return $new;
111 }
112
113 public function withPeriod(?int $period): self
114 {
115 $new = clone $this;
116 $new->period = $period;
117 return $new;
118 }
119
120 public function withOnlyPublic(bool $only_public): self
121 {
122 $new = clone $this;
123 $new->only_public = $only_public;
124 return $new;
125 }
126
127 public function withMinPriority(?int $min_priority): self
128 {
129 $new = clone $this;
130 $new->min_priority = $min_priority;
131 return $new;
132 }
133
134 public function withMaxPriority(?int $max_priority): self
135 {
136 $new = clone $this;
137 $new->max_priority = $max_priority;
138 return $new;
139 }
140
141 public function withLimit(?int $limit): self
142 {
143 $new = clone $this;
144 $new->limit = $limit;
145 return $new;
146 }
147
148 public function withPreventNesting(bool $stop_nesting): self
149 {
150 $new = clone $this;
151 $new->prevent_nesting = $stop_nesting;
152 return $new;
153 }
154
155 public function withNoAutoGenerated(bool $no_auto_generated): self
156 {
157 $new = clone $this;
158 $new->no_auto_generated = $no_auto_generated;
159 return $new;
160 }
161
162 public function withExcludedNewsIds(array $excluded_news_ids): self
163 {
164 $new = clone $this;
165 $new->excluded_news_ids = array_map('intval', $excluded_news_ids);
166 return $new;
167 }
168
169 public function withIncludeReadStatus(bool $include_read_status): self
170 {
171 $new = clone $this;
172 $new->include_read_status = $include_read_status;
173 return $new;
174 }
175
176 public function withReadUserId(?int $read_user_id): self
177 {
178 $new = clone $this;
179 $new->read_user_id = $read_user_id;
180 return $new;
181 }
182
183 /*
184 Public Methods
185 */
186
187 public function jsonSerialize(): array
188 {
189 return $this->toArray();
190 }
191
192 public function toArray(): array
193 {
194 return [
195 'start_date' => $this->start_date?->format('Y-m-d H:i:s'),
196 'period' => $this->period,
197 'only_public' => $this->only_public,
198 'min_priority' => $this->min_priority,
199 'max_priority' => $this->max_priority,
200 'limit' => $this->limit,
201 'stop_nesting' => $this->prevent_nesting,
202 'no_auto_generated' => $this->no_auto_generated,
203 'excluded_news_ids' => $this->excluded_news_ids,
204 'include_read_status' => $this->include_read_status,
205 'read_user_id' => $this->read_user_id,
206 ];
207 }
208
212 public function hasPriorityFilters(): bool
213 {
214 return $this->min_priority !== null || $this->max_priority !== null;
215 }
216
220 public function validate(): void
221 {
222 if ($this->min_priority !== null && $this->max_priority !== null && $this->min_priority > $this->max_priority) {
223 throw new \InvalidArgumentException('Min priority cannot be greater than max priority');
224 }
225
226 if ($this->limit !== null && $this->limit < 0) {
227 throw new \InvalidArgumentException('Limit cannot be negative');
228 }
229 }
230}
News Criteria DTO for querying news items supports caching, JSON serialization, and validation.
withMaxPriority(?int $max_priority)
withPreventNesting(bool $stop_nesting)
withStartDate(?DateTimeImmutable $start_date)
validate()
Validate criteria parameters.
__construct(private ?DateTimeImmutable $start_date=null, private ?int $period=null, private bool $only_public=false, private ?int $min_priority=null, private ?int $max_priority=null, private ?int $limit=null, private bool $prevent_nesting=false, private bool $no_auto_generated=false, private array $excluded_news_ids=[], private ?bool $include_read_status=null, private ?int $read_user_id=null,)
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)