Lines
86.63%
162 / 187
Methods
85.00%
34 / 40
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| getShortestBookableStart | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | ||
| getGreatestBookableEnd | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | ||
| getShortestBookableStartOnOpenedScope | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 4 | ||
| getGreatestBookableEndOnOpenedScope | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 4 | ||
| withoutDublicates | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 4 | ||
| withUniqueScopes | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 4 | ||
| addScopeList | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | ||
| withLessData | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| sortByName | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 3 | ||
| withProviderID | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 4 | ||
| addRequiredSlots | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 3 | ||
| getRequiredSlotsByScope | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | ||
| hasOpenedScope | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | ||
| getProviderList | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsentities\Collection\Base] __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Collection\Base] getFirst | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Collection\Base] getLast | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Collection\Base] sortByContactName | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Collection\Base] sortByCustomKey | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Collection\Base] sortByCustomStringKey | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Collection\Base] __clone | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsentities\Collection\Base] hasEntity | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 4 | ||
| [BO\Zmsentities\Collection\Base] getEntity | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 4 | ||
| [BO\Zmsentities\Collection\Base] addEntity | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Collection\Base] offsetSet | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 3 | ||
| [BO\Zmsentities\Collection\Base] addData | 91.66% | 11 / 12 | 0.00% | 0 / 1 | 5.01 | ||
| [BO\Zmsentities\Collection\Base] addList | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsentities\Collection\Base] getIds | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsentities\Collection\Base] getIdsCsv | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Collection\Base] getCsvForProperty | 0.00% | 0 / 5 | 0.00% | 0 / 1 | 12 | ||
| [BO\Zmsentities\Collection\Base] getCsvForPropertyList | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 6 | ||
| [BO\Zmsentities\Collection\Base] withValueFor | 0.00% | 0 / 4 | 0.00% | 0 / 1 | 6 | ||
| [BO\Zmsentities\Collection\Base] setJsonCompressLevel | 0.00% | 0 / 2 | 0.00% | 0 / 1 | 6 | ||
| [BO\Zmsentities\Collection\Base] getAsArray | 0.00% | 0 / 4 | 0.00% | 0 / 1 | 6 | ||
| [BO\Zmsentities\Collection\Base] jsonSerialize | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Collection\Base] __toString | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsentities\Collection\Base] getResolveLevel | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Collection\Base] setResolveLevel | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsentities\Collection\Base] withResolveLevel | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsentities\Collection\Base] chunk | 0.00% | 0 / 9 | 0.00% | 0 / 1 | 12 | ||
| 11 | class ScopeList extends Base | |
| 12 | { | |
| 13 | public const string ENTITY_CLASS = '\BO\Zmsentities\Scope'; | |
| 14 | ||
| 15 | protected array $slotsByID = []; | |
| 16 | ||
| 17 | /** | |
| 18 | * Get shortest bookable start date of a scope in scopelist | |
| 19 | * | |
| 20 | * @return \DateTimeImmutable $date | |
| 21 | */ | |
| 22 | public function getShortestBookableStart(mixed $now) | |
| 23 | { | |
| 24 | $date = $now; | |
| 25 | foreach ($this as $scope) { | |
| 26 | $startDate = $scope->getBookableStartDate($now); | |
| 27 | $date = ($startDate > $date) ? $startDate : $date; | |
| 28 | } | |
| 29 | return $date; | |
| 30 | } | |
| 31 | ||
| 32 | /** | |
| 33 | * Get longest bookable end date of a scope in scopelist | |
| 34 | * | |
| 35 | * @return \DateTimeImmutable $date | |
| 36 | */ | |
| 37 | public function getGreatestBookableEnd(mixed $now) | |
| 38 | { | |
| 39 | $date = $now; | |
| 40 | foreach ($this as $scope) { | |
| 41 | $endDate = $scope->getBookableEndDate($now); | |
| 42 | $date = ($endDate > $date) ? $endDate : $date; | |
| 43 | } | |
| 44 | return $date; | |
| 45 | } | |
| 46 | ||
| 47 | /** | |
| 48 | * Get shortest bookable start date of a opened scope in scopelist | |
| 49 | * | |
| 50 | * @return \DateTimeInterface|null | |
| 51 | */ | |
| 52 | public function getShortestBookableStartOnOpenedScope(mixed $now) | |
| 53 | { | |
| 54 | $date = null; | |
| 55 | foreach ($this as $scope) { | |
| 56 | if ($scope->getStatus('availability', 'isOpened')) { | |
| 57 | $date = $now; | |
| 58 | $startDate = $scope->getBookableStartDate($date); | |
| 59 | $date = ($startDate > $date) ? $startDate : $date; | |
| 60 | } | |
| 61 | } | |
| 62 | return $date; | |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * Get longest bookable end date of a opened scope in scopelist | |
| 67 | * | |
| 68 | * @return \DateTimeInterface|null | |
| 69 | */ | |
| 70 | public function getGreatestBookableEndOnOpenedScope(mixed $now) | |
| 71 | { | |
| 72 | $date = null; | |
| 73 | foreach ($this as $scope) { | |
| 74 | if ($scope->getStatus('availability', 'isOpened')) { | |
| 75 | $date = $now; | |
| 76 | $endDate = $scope->getBookableEndDate($date); | |
| 77 | $date = ($endDate > $date) ? $endDate : $date; | |
| 78 | } | |
| 79 | } | |
| 80 | return $date; | |
| 81 | } | |
| 82 | ||
| 83 | public function withoutDublicates(mixed $scopeList = null): self | |
| 84 | { | |
| 85 | $collection = new self(); | |
| 86 | foreach ($this as $scope) { | |
| 87 | if (! $scopeList || ! $scopeList->hasEntity($scope->getId())) { | |
| 88 | $collection->addEntity(clone $scope); | |
| 89 | } | |
| 90 | } | |
| 91 | return $collection; | |
| 92 | } | |
| 93 | ||
| 94 | public function withUniqueScopes(): self | |
| 95 | { | |
| 96 | $scopeList = new self(); | |
| 97 | foreach ($this as $scope) { | |
| 98 | if ($scope->getId() && ! $scopeList->hasEntity($scope->getId())) { | |
| 99 | $scopeList->addEntity(clone $scope); | |
| 100 | } | |
| 101 | } | |
| 102 | return $scopeList; | |
| 103 | } | |
| 104 | ||
| 105 | public function addScopeList(mixed $scopeList): static | |
| 106 | { | |
| 107 | foreach ($scopeList as $scope) { | |
| 108 | $this->addEntity($scope); | |
| 109 | } | |
| 110 | return $this; | |
| 111 | } | |
| 112 | ||
| 113 | /** | |
| 114 | * @return static | |
| 115 | */ | |
| 116 | #[\Override] | |
| 117 | public function withLessData(array $keepArray = []) | |
| 118 | { | |
| 119 | /** @psalm-suppress UnsafeGenericInstantiation */ | |
| 120 | $scopeList = new static(); | |
| 121 | foreach ($this as $scope) { | |
| 122 | $scopeList->addEntity(clone $scope->withLessData($keepArray)); | |
| 123 | } | |
| 124 | return $scopeList; | |
| 125 | } | |
| 126 | ||
| 127 | /** | |
| 128 | * @return static | |
| 129 | */ | |
| 130 | #[\Override] | |
| 131 | public function sortByName() | |
| 132 | { | |
| 133 | $this->uasort(function ($a, $b) { | |
| 134 | $nameA = (isset($a->provider['name'])) ? $a->provider['name'] : ($a->shortName ?? ''); | |
| 135 | $nameB = (isset($b->provider['name'])) ? $b->provider['name'] : ($b->shortName ?? ''); | |
| 136 | return strcmp( | |
| 137 | Sorter::toSortableString(ucfirst($nameA ?? '')), | |
| 138 | Sorter::toSortableString(ucfirst($nameB ?? '')) | |
| 139 | ); | |
| 140 | }); | |
| 141 | return $this; | |
| 142 | } | |
| 143 | ||
| 144 | public function withProviderID(mixed $source, mixed $providerID): self | |
| 145 | { | |
| 146 | $list = new ScopeList(); | |
| 147 | foreach ($this as $scope) { | |
| 148 | if ($scope->provider['source'] == $source && $scope->provider['id'] == $providerID) { | |
| 149 | $list->addEntity(clone $scope); | |
| 150 | } | |
| 151 | } | |
| 152 | return $list; | |
| 153 | } | |
| 154 | ||
| 155 | public function addRequiredSlots(mixed $source, mixed $providerID, mixed $slotsRequired): static | |
| 156 | { | |
| 157 | $scopeList = $this->withProviderID($source, $providerID); | |
| 158 | foreach ($scopeList as $scope) { | |
| 159 | if (!isset($this->slotsByID[$scope->id])) { | |
| 160 | $this->slotsByID[$scope->id] = 0; | |
| 161 | } | |
| 162 | $this->slotsByID[$scope->id] += $slotsRequired; | |
| 163 | } | |
| 164 | return $this; | |
| 165 | } | |
| 166 | ||
| 167 | public function getRequiredSlotsByScope(Scope $scope): mixed | |
| 168 | { | |
| 169 | if (isset($this->slotsByID[$scope->id])) { | |
| 170 | return $this->slotsByID[$scope->id]; | |
| 171 | } | |
| 172 | return 0; | |
| 173 | } | |
| 174 | ||
| 175 | public function hasOpenedScope(): bool | |
| 176 | { | |
| 177 | $isOpened = false; | |
| 178 | foreach ($this as $entity) { | |
| 179 | if ($entity->getStatus('availability', 'isOpened')) { | |
| 180 | $isOpened = true; | |
| 181 | } | |
| 182 | } | |
| 183 | return $isOpened; | |
| 184 | } | |
| 185 | ||
| 186 | public function getProviderList(): \BO\Zmsentities\Collection\ProviderList | |
| 187 | { | |
| 188 | $list = new ProviderList(); | |
| 189 | foreach ($this as $scope) { | |
| 190 | $provider = $scope->getProvider(); | |
| 191 | $list->addEntity($provider); | |
| 192 | } | |
| 193 | return $list->withUniqueProvider(); | |
| 194 | } | |
| 195 | } |
Inherited from BO\Zmsentities\Collection\Base
| 32 | public function __construct(object|array $array = [], int $flags = 0, string $iteratorClass = "ArrayIterator") | |
| 33 | { | |
| 34 | /** @var class-string<\ArrayIterator> $iteratorClass */ | |
| 35 | parent::__construct($array, $flags, $iteratorClass); | |
| 36 | } |
| 41 | public function getFirst() | |
| 42 | { | |
| 43 | $item = $this->getIterator()->current(); | |
| 44 | return $item; | |
| 45 | } |
| 51 | public function getLast() | |
| 52 | { | |
| 53 | $copy = $this->getArrayCopy(); | |
| 54 | $item = end($copy); | |
| 55 | return $item; | |
| 56 | } |
| 73 | public function sortByContactName(): static | |
| 74 | { | |
| 75 | $this->uasort(function ($a, $b) { | |
| 76 | return strcmp( | |
| 77 | Sorter::toSortableString(ucfirst($a->contact['name'] ?? '')), | |
| 78 | Sorter::toSortableString(ucfirst($b->contact['name'] ?? '')) | |
| 79 | ); | |
| 80 | }); | |
| 81 | return $this; | |
| 82 | } |
| 84 | public function sortByCustomKey(string $key): static | |
| 85 | { | |
| 86 | $this->uasort(function ($a, $b) use ($key): mixed { | |
| 87 | return ($a[$key] - $b[$key]); | |
| 88 | }); | |
| 89 | return $this; | |
| 90 | } |
| 92 | public function sortByCustomStringKey(mixed $key): static | |
| 93 | { | |
| 94 | $this->uasort(function ($a, $b) use ($key) { | |
| 95 | return strcmp( | |
| 96 | Sorter::toSortableString(ucfirst($a[$key] ?? '')), | |
| 97 | Sorter::toSortableString(ucfirst($b[$key] ?? '')) | |
| 98 | ); | |
| 99 | }); | |
| 100 | return $this; | |
| 101 | } |
| 103 | public function __clone() | |
| 104 | { | |
| 105 | foreach ($this as $key => $property) { | |
| 106 | $this[$key] = clone $property; | |
| 107 | } | |
| 108 | } |
| 110 | public function hasEntity(mixed $primary): bool | |
| 111 | { | |
| 112 | foreach ($this as $entity) { | |
| 113 | if (isset($entity->{$entity::PRIMARY}) && $primary == $entity->{$entity::PRIMARY}) { | |
| 114 | return true; | |
| 115 | } | |
| 116 | } | |
| 117 | return false; | |
| 118 | } |
| 124 | public function getEntity(string|int $primary) | |
| 125 | { | |
| 126 | foreach ($this as $entity) { | |
| 127 | if (isset($entity->{$entity::PRIMARY}) && $primary == $entity->{$entity::PRIMARY}) { | |
| 128 | return $entity; | |
| 129 | } | |
| 130 | } | |
| 131 | return null; | |
| 132 | } |
| 134 | public function addEntity(\BO\Zmsentities\Schema\Entity $entity): static | |
| 135 | { | |
| 136 | /** @var T $entity */ | |
| 137 | $this->append($entity); | |
| 138 | return $this; | |
| 139 | } |
| 146 | public function offsetSet(mixed $index, mixed $value): void | |
| 147 | { | |
| 148 | $className = $this::ENTITY_CLASS; | |
| 149 | if (is_a($value, $className)) { | |
| 150 | /** @var T $entity */ | |
| 151 | $entity = $value; | |
| 152 | /** @psalm-suppress PossiblyNullArgument */ | |
| 153 | parent::offsetSet($index, $entity); | |
| 154 | } elseif (is_array($value)) { | |
| 155 | /** @var T $entity */ | |
| 156 | $entity = new $className($value); | |
| 157 | /** @psalm-suppress PossiblyNullArgument */ | |
| 158 | parent::offsetSet($index, $entity); | |
| 159 | } else { | |
| 160 | $given = get_debug_type($value); | |
| 161 | throw new \Exception('Invalid entity ' . $given . ' for collection ' . __CLASS__); | |
| 162 | } | |
| 163 | } |
| 165 | public function addData(mixed $mergeData): static | |
| 166 | { | |
| 167 | foreach ($mergeData as $item) { | |
| 168 | if ($item instanceof Entity) { | |
| 169 | if (null === $item->getResolveLevel()) { | |
| 170 | $item->setResolveLevel($this->getResolveLevel()); | |
| 171 | } | |
| 172 | $this->addEntity($item); | |
| 173 | } else { | |
| 174 | $className = $this::ENTITY_CLASS; | |
| 175 | $entity = new $className($item); | |
| 176 | if (!$entity instanceof Entity) { | |
| 177 | throw new \Exception('Invalid entity ' . get_debug_type($entity) . ' for collection ' . __CLASS__); | |
| 178 | } | |
| 179 | $entity->setResolveLevel($this->getResolveLevel()); | |
| 180 | $this->addEntity($entity); | |
| 181 | } | |
| 182 | } | |
| 183 | return $this; | |
| 184 | } |
| 189 | public function addList(Base $list): static | |
| 190 | { | |
| 191 | foreach ($list as $item) { | |
| 192 | $this->addEntity($item); | |
| 193 | } | |
| 194 | return $this; | |
| 195 | } |
| 201 | public function getIds() | |
| 202 | { | |
| 203 | $list = []; | |
| 204 | foreach ($this as $entity) { | |
| 205 | $list[] = $entity->id; | |
| 206 | } | |
| 207 | return $list; | |
| 208 | } |
| 210 | public function getIdsCsv(): string | |
| 211 | { | |
| 212 | return implode(',', $this->getIds()); | |
| 213 | } |
| 215 | public function getCsvForProperty(mixed $propertyName, string $csvSeperator = ','): string | |
| 216 | { | |
| 217 | $list = []; | |
| 218 | foreach ($this as $entry) { | |
| 219 | if ($entry->hasProperty($propertyName)) { | |
| 220 | $list[] = $entry->getProperty($propertyName); | |
| 221 | } | |
| 222 | } | |
| 223 | return implode($csvSeperator, $list); | |
| 224 | } |
| 226 | public function getCsvForPropertyList(array $propertyList, string $propertySeperator = '', string $csvSeperator = ','): string | |
| 227 | { | |
| 228 | $list = []; | |
| 229 | foreach ($this as $entry) { | |
| 230 | $propertyConcat = ''; | |
| 231 | foreach ($propertyList as $propertyName) { | |
| 232 | if ($entry->hasProperty($propertyName)) { | |
| 233 | if ($propertyConcat) { | |
| 234 | $propertyConcat .= $propertySeperator; | |
| 235 | } | |
| 236 | $propertyConcat .= $entry->getProperty($propertyName); | |
| 237 | } | |
| 238 | } | |
| 239 | if ($propertyConcat) { | |
| 240 | $list[] = $propertyConcat; | |
| 241 | } | |
| 242 | } | |
| 243 | return implode($csvSeperator, $list); | |
| 244 | } |
| 250 | public function withValueFor(mixed $param, mixed $newValue): static | |
| 251 | { | |
| 252 | /** @psalm-suppress UnsafeGenericInstantiation */ | |
| 253 | $list = new static(); | |
| 254 | foreach ($this as $entry) { | |
| 255 | $list[] = $entry->withProperty($param, $newValue); | |
| 256 | } | |
| 257 | return $list; | |
| 258 | } |
| 277 | public function setJsonCompressLevel(mixed $jsonCompressLevel): void | |
| 278 | { | |
| 279 | foreach ($this as $item) { | |
| 280 | $item->setJsonCompressLevel($jsonCompressLevel); | |
| 281 | } | |
| 282 | } |
| 288 | public function getAsArray(): array | |
| 289 | { | |
| 290 | $array = []; | |
| 291 | foreach ($this as $item) { | |
| 292 | $array[] = $item; | |
| 293 | } | |
| 294 | ||
| 295 | return $array; | |
| 296 | } |
| 299 | public function jsonSerialize(): mixed | |
| 300 | { | |
| 301 | return $this->getArrayCopy(); | |
| 302 | } |
| 304 | public function __toString() | |
| 305 | { | |
| 306 | $list = []; | |
| 307 | foreach ($this as $item) { | |
| 308 | $list[] = $item->__toString(); | |
| 309 | } | |
| 310 | return "[" . implode(',', $list) . "]"; | |
| 311 | } |
| 316 | public function getResolveLevel() | |
| 317 | { | |
| 318 | return $this->resolveLevel; | |
| 319 | } |
| 325 | public function setResolveLevel($resolveLevel) | |
| 326 | { | |
| 327 | $this->resolveLevel = $resolveLevel; | |
| 328 | return $this; | |
| 329 | } |
| 335 | public function withResolveLevel($resolveLevel) | |
| 336 | { | |
| 337 | /** @psalm-suppress UnsafeGenericInstantiation */ | |
| 338 | $collection = new static(); | |
| 339 | foreach ($this as $entity) { | |
| 340 | $reduced = $entity->withResolveLevel($resolveLevel); | |
| 341 | $collection[] = $reduced; | |
| 342 | } | |
| 343 | return $collection; | |
| 344 | } |
| 350 | public function chunk(mixed $length): array | |
| 351 | { | |
| 352 | /** @psalm-suppress UnsafeGenericInstantiation */ | |
| 353 | $chunks = [new static()]; | |
| 354 | $id = 0; | |
| 355 | foreach ($this as $entry) { | |
| 356 | $chunkIndex = (int) floor($id / $length); | |
| 357 | if (! isset($chunks[$chunkIndex])) { | |
| 358 | /** @psalm-suppress UnsafeGenericInstantiation */ | |
| 359 | $chunks[$chunkIndex] = new static(); | |
| 360 | } | |
| 361 | ||
| 362 | $chunks[$chunkIndex][] = $entry; | |
| 363 | ||
| 364 | $id++; | |
| 365 | } | |
| 366 | ||
| 367 | return $chunks; | |
| 368 | } |