Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
12 / 12 |
|
60.00% |
6 / 10 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| ListArray | |
100.00% |
10 / 10 |
|
100.00% |
12 / 12 |
|
60.00% |
6 / 10 |
|
100.00% |
2 / 2 |
10.14 | |
100.00% |
1 / 1 |
| coerce | |
100.00% |
9 / 9 |
|
100.00% |
7 / 7 |
|
66.67% |
4 / 6 |
|
100.00% |
1 / 1 |
4.59 | |||
| isEmpty | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
|
50.00% |
2 / 4 |
|
100.00% |
1 / 1 |
4.12 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Celemas\Sire\Coercer; |
| 6 | |
| 7 | use Celemas\Sire\Coercion; |
| 8 | use Celemas\Sire\CoercionMode; |
| 9 | use Celemas\Sire\Contract; |
| 10 | use Celemas\Sire\Failure; |
| 11 | use Override; |
| 12 | |
| 13 | /** @api */ |
| 14 | final class ListArray implements Contract\Coercer |
| 15 | { |
| 16 | public string $message { |
| 17 | get => '{label} must be a list'; |
| 18 | } |
| 19 | |
| 20 | #[Override] |
| 21 | public function coerce(mixed $pristine, CoercionMode $mode): Contract\Coercion |
| 22 | { |
| 23 | if ( |
| 24 | is_array($pristine) |
| 25 | && ($pristine === [] || array_keys($pristine) === range(0, count($pristine) - 1)) |
| 26 | ) { |
| 27 | return new Coercion($pristine, $pristine, empty: $pristine === []); |
| 28 | } |
| 29 | |
| 30 | return new Coercion( |
| 31 | $pristine, |
| 32 | $pristine, |
| 33 | Failure::invalid(), |
| 34 | empty: self::isEmpty($pristine), |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | private static function isEmpty(mixed $value): bool |
| 39 | { |
| 40 | return $value === null || $value === '' || $value === []; |
| 41 | } |
| 42 | } |