Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
5.56% |
1 / 18 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Base | |
5.56% |
1 / 18 |
|
33.33% |
1 / 3 |
17.48 | |
0.00% |
0 / 1 |
| offsetSet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| sortByName | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| sortWithCollator | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @package Zmsdldb |
| 5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
| 6 | **/ |
| 7 | |
| 8 | namespace BO\Zmsdldb\Collection; |
| 9 | |
| 10 | use BO\Zmsdldb\Helper\Sorter; |
| 11 | |
| 12 | /** |
| 13 | * @template T of \BO\Zmsdldb\Entity\Base |
| 14 | * @extends \ArrayObject<int|string, T> |
| 15 | */ |
| 16 | class Base extends \ArrayObject |
| 17 | { |
| 18 | public function offsetSet(mixed $key, mixed $value): void |
| 19 | { |
| 20 | parent::offsetSet($key, $value); |
| 21 | } |
| 22 | |
| 23 | public function sortByName(): static |
| 24 | { |
| 25 | $itemList = clone $this; |
| 26 | $itemList->uasort(function ($a, $b) { |
| 27 | return strcmp(Sorter::toSortableString($a->getName()), Sorter::toSortableString($b->getName())); |
| 28 | }); |
| 29 | return $itemList; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @psalm-api |
| 34 | */ |
| 35 | public function sortWithCollator(string $field = 'name', string $locale = 'de'): static |
| 36 | { |
| 37 | $collator = collator_create($locale); |
| 38 | if ($collator === null) { |
| 39 | throw new \RuntimeException('Could not create collator for locale ' . $locale); |
| 40 | } |
| 41 | $collator->setStrength(\Collator::QUATERNARY); |
| 42 | $collator->setAttribute(\Collator::QUATERNARY, \Collator::ON); |
| 43 | $collator->setAttribute(\Collator::CASE_FIRST, \Collator::ON); |
| 44 | $collator->setAttribute(\Collator::NUMERIC_COLLATION, \Collator::ON); |
| 45 | |
| 46 | $itemList = clone $this; |
| 47 | $itemList->uasort(function ($itemA, $itemB) use ($collator, $field) { |
| 48 | return collator_compare($collator, $itemA[$field], $itemB[$field]); |
| 49 | }); |
| 50 | return $itemList; |
| 51 | } |
| 52 | } |