Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.78% |
44 / 45 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
| ProviderList | |
97.78% |
44 / 45 |
|
85.71% |
6 / 7 |
25 | |
0.00% |
0 / 1 |
| hasProvider | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| hasProviderStrict | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
| hasRequest | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| withMatchingByList | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| withUniqueProvider | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| withDataAsObject | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
6 | |||
| sortById | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BO\Zmsentities\Collection; |
| 4 | |
| 5 | /** |
| 6 | * @extends Base<\BO\Zmsentities\Provider> |
| 7 | */ |
| 8 | class ProviderList extends Base |
| 9 | { |
| 10 | public const string ENTITY_CLASS = '\BO\Zmsentities\Provider'; |
| 11 | |
| 12 | public function hasProvider(mixed $providerIdCsv): bool |
| 13 | { |
| 14 | $providerFound = false; |
| 15 | $providerIds = explode(',', $providerIdCsv); |
| 16 | foreach ($providerIds as $providerId) { |
| 17 | if (in_array($providerId, $this->getIds())) { |
| 18 | $providerFound = true; |
| 19 | } |
| 20 | } |
| 21 | return $providerFound; |
| 22 | } |
| 23 | |
| 24 | public function hasProviderStrict(mixed $providerIdCsv): bool |
| 25 | { |
| 26 | $providerIds = explode(',', $providerIdCsv); |
| 27 | foreach ($providerIds as $providerId) { |
| 28 | if (! in_array($providerId, $this->getIds())) { |
| 29 | return false; |
| 30 | } |
| 31 | } |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | public function hasRequest(mixed $requestIdCsv): bool |
| 36 | { |
| 37 | $requestIds = explode(',', $requestIdCsv); |
| 38 | foreach ($this as $entity) { |
| 39 | foreach ($requestIds as $requestId) { |
| 40 | if ($entity->hasRequest($requestId)) { |
| 41 | return true; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | public function withMatchingByList(mixed $providerIdCsv): self |
| 49 | { |
| 50 | $collection = new self(); |
| 51 | $providerIds = explode(',', $providerIdCsv); |
| 52 | foreach ($providerIds as $providerId) { |
| 53 | if (in_array($providerId, $this->getIds())) { |
| 54 | $entity = $this->getEntity($providerId); |
| 55 | if ($entity !== null) { |
| 56 | $collection->addEntity($entity); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | return $collection; |
| 61 | } |
| 62 | |
| 63 | public function withUniqueProvider(): self |
| 64 | { |
| 65 | $list = new self(); |
| 66 | foreach ($this as $provider) { |
| 67 | /** @psalm-suppress RedundantConditionGivenDocblockType Constructor storage can include null. */ |
| 68 | if ($provider instanceof \BO\Zmsentities\Provider && ! $list->hasEntity($provider->id)) { |
| 69 | $list->addEntity($provider); |
| 70 | } |
| 71 | } |
| 72 | return $list; |
| 73 | } |
| 74 | |
| 75 | public function withDataAsObject(): self |
| 76 | { |
| 77 | $list = new self(); |
| 78 | foreach ($this as $provider) { |
| 79 | if (isset($provider['data']) && '{}' != $provider->data) { |
| 80 | if (is_string($provider->data)) { |
| 81 | $provider->data = json_decode($provider->data); |
| 82 | } elseif (is_array($provider->data)) { |
| 83 | $provider->data = json_decode((string) json_encode($provider->data, JSON_FORCE_OBJECT)); |
| 84 | } |
| 85 | } else { |
| 86 | unset($provider['data']); |
| 87 | } |
| 88 | $list->addEntity($provider); |
| 89 | } |
| 90 | return $list; |
| 91 | } |
| 92 | |
| 93 | public function sortById(): \BO\Zmsentities\Collection\Base |
| 94 | { |
| 95 | $list = clone $this; |
| 96 | $list->uasort(function ($a, $b): mixed { |
| 97 | return ($a['id'] - $b['id']); |
| 98 | }); |
| 99 | return (new self())->addList($list); |
| 100 | } |
| 101 | } |