ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCollection.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Export\ImportStatus\I\Content\ilHandlerInterface as ilImportStatusContentHandlerInterface;
26use ILIAS\Export\ImportStatus\I\ilFactoryInterface as ilImportStatusFactoryInterface;
27
29{
30 protected ilImportStatusFactoryInterface $status;
34 protected array $elements;
35 protected int $index;
36 protected int $minIndex;
37 protected bool $is_numbering_enabled;
38
39 public function __construct(
40 ilImportStatusFactoryInterface $status,
41 ) {
42 $this->elements = [];
43 $this->minIndex = 0;
44 $this->index = $this->minIndex;
45 $this->is_numbering_enabled = false;
46 $this->status = $status;
47 }
48
52 protected function getArrayOfElementsWithType(StatusType $type): array
53 {
54 return array_filter(
55 $this->toArray(),
56 function (ilHandler $s) use ($type) {
57 return $s->getType() === $type;
58 }
59 );
60 }
61
62 public function hasStatusType(StatusType $type): bool
63 {
64 return count($this->getArrayOfElementsWithType($type)) > 0;
65 }
66
67 public function withAddedStatus(ilHandlerInterface $import_status): ilCollection
68 {
69 $clone = clone $this;
70 $clone->elements[] = $import_status;
71 return $clone;
72 }
73
75 {
76 $clone = clone $this;
77 $clone->elements = $this->getArrayOfElementsWithType($type);
78 return $clone;
79 }
80
82 {
83 $clone = clone $this;
84 $clone->elements = array_merge($this->toArray(), $other->toArray());
85 return $clone;
86 }
87
88 public function current(): ilHandlerInterface
89 {
90 return $this->elements[$this->index];
91 }
92
93 public function next(): void
94 {
95 $this->index++;
96 }
97
98 public function key(): int
99 {
100 return $this->index;
101 }
102
103 public function valid(): bool
104 {
105 return $this->minIndex <= $this->index && $this->index < $this->count();
106 }
107
108 public function rewind(): void
109 {
110 $this->index = $this->minIndex;
111 }
112
113 public function count(): int
114 {
115 return count($this->elements);
116 }
117
121 public function toArray(): array
122 {
123 return $this->elements;
124 }
125
126 public function withNumberingEnabled(bool $enabled): ilCollectionInterface
127 {
128 $clone = clone $this;
129 $clone->is_numbering_enabled = $enabled;
130 return $clone;
131 }
132
133 public function toString(StatusType ...$types): string
134 {
135 $collection = $this->status->collection();
136 $msg = "<br>Listing status messages (of type(s)";
137 foreach ($types as $type) {
138 $msg .= " " . $type->name;
139 $collection = $collection->getMergedCollectionWith($this->getCollectionOfAllByType($type));
140 }
141 $msg .= "):<br>";
142 $elements = $collection->toArray();
143 for($i = 0; $i < count($elements); $i++) {
144 $faied_status = $elements[$i];
145 $msg .= "<br>";
146 $msg .= $this->is_numbering_enabled
147 ? '[' . $i . ']: '
148 : '';
149 $msg .= $faied_status->getContent()->toString();
150 }
151 return $msg;
152 }
153
154 public function mergeContentToElements(
155 ilImportStatusContentHandlerInterface $content,
156 bool $at_front = true
158 $clone = clone $this;
159 $new_elements = [];
160 foreach ($clone->toArray() as $element) {
161 $new_elements[] = $at_front
162 ? $this->status->handler()
163 ->withType($element->getType())
164 ->withContent($content->mergeWith($element->getContent()))
165 : $this->status->handler()
166 ->withType($element->getType())
167 ->withContent($element->getContent()->mergeWith($content));
168 }
169 $clone->elements = $new_elements;
170 return $clone;
171 }
172}
withAddedStatus(ilHandlerInterface $import_status)
getMergedCollectionWith(ilCollectionInterface $other)
ilImportStatusFactoryInterface $status
__construct(ilImportStatusFactoryInterface $status,)
mergeContentToElements(ilImportStatusContentHandlerInterface $content, bool $at_front=true)