19declare(strict_types=1);
39 protected ?\Closure $fetch_callback =
null
43 foreach ($news_ids as $news_id) {
44 $this->news_items[$news_id] =
null;
54 $clone->fetch_callback = $callback;
64 $this->news_items[
$id] =
null;
77 public function load(array $news_ids = []): static
79 if ($this->fetch_callback ===
null) {
80 throw new \RuntimeException(
'No fetch callback provided');
83 if (empty($news_ids)) {
84 $news_ids = array_keys(array_filter($this->news_items, fn($item) => $item ===
null));
86 $news_ids = array_intersect($news_ids, array_keys($this->news_items));
89 if (empty($news_ids)) {
95 if ($this->group_files ===
true) {
96 $context_types[] =
'file';
98 if ($this->group_forums > 0) {
99 $context_types[] =
'frm';
103 $items = call_user_func($this->fetch_callback, $news_ids, $context_types);
118 $this->group_files =
true;
124 $this->group_forums = $group_posting_sequence ? 2 : 1;
131 $needs_file_grouping =
false;
132 $needs_forum_grouping =
false;
133 foreach ($items as $item) {
134 if ($this->group_files ===
true && $item->getContextObjType() ===
'file') {
135 $needs_file_grouping =
true;
137 if ($this->group_forums > 0 && $item->getContextObjType() ===
'frm') {
138 $needs_forum_grouping =
true;
143 if ($needs_file_grouping) {
144 parent::groupFiles();
146 if ($needs_forum_grouping) {
147 parent::groupForums($this->group_forums === 2);
161 return array_key_exists($news_id, $this->news_items);
167 public function has(
int $news_id): bool
169 return isset($this->news_items[$news_id]);
181 if (!$this->
has($news_id)) {
182 $this->
load([$news_id]);
185 return $this->news_items[$news_id] ??
null;
197 $index = max(0, $offset);
198 $news_id = array_keys($this->news_items)[$index] ??
null;
200 return $news_id !==
null ? $this->
getById($news_id) :
null;
206 public function pluck(
string $key,
bool $wrap =
false): array
209 return $wrap ? array_map(fn($item) => [$item], array_keys($this->news_items)) : array_keys($this->news_items);
213 return parent::pluck($key, $wrap);
222 return parent::toArray();
229 bool $aggregate_files =
false,
230 bool $aggregate_forums =
false,
231 bool $group_posting_sequence =
false
234 return parent::getAggregatedNews($aggregate_files, $aggregate_forums, $group_posting_sequence);
239 return parent::merge($other)->withFetchCallback($this->fetch_callback);
242 public function exclude(array $news_ids): static
244 return parent::exclude($news_ids)->withFetchCallback($this->fetch_callback);
247 public function limit(?
int $limit): static
249 return parent::limit($limit)->withFetchCallback($this->fetch_callback);
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
This class is a special implementation of a NewsCollection that is designed to load the complete News...
load(array $news_ids=[])
This method loads the provided NewsItems from the database.
limit(?int $limit)
Limit the number of news items and returns it as a new collection.
contains(int $news_id)
This method returns true if the NewsItem exists in this collection.
addNewsItems(array $news_items)
Add multiple news items efficiently.
pick(int $offset)
This method returns the NewsItem of the given offset of the collection.
withFetchCallback(\Closure $callback)
groupForums(bool $group_posting_sequence)
exclude(array $news_ids)
Returns a new collection with only the news items that are not in the provided list.
has(int $news_id)
This method returns true if the NewsItem exists in this collection and has been loaded.
toArray()
INFO: This method will load all NewsItems into the collection.
__construct(array $news_ids=[], protected ?\Closure $fetch_callback=null)
merge(NewsCollection $other)
Merge with another collection and returns it as a new collection.
pluck(string $key, bool $wrap=false)
INFO: This method will load all NewsItems into the collection.
getAggregatedNews(bool $aggregate_files=false, bool $aggregate_forums=false, bool $group_posting_sequence=false)
INFO: This method will load all NewsItems into the collection.
getById(int $news_id)
This method returns the NewsItem with the given id or null if it does not exist.
Optimized News Collection with memory-efficient data structures to support large news feeds.
addNewsItem(NewsItem $item)
Add a single news item with indexing.
News Item DTO for transfer of news items.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc