Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
57.89% |
11 / 19 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ParserConfig | |
57.89% |
11 / 19 |
|
60.00% |
3 / 5 |
17.46 | |
0.00% |
0 / 1 |
| api | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| form | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| update | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| extend | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Computator\FrameworkUtils\InputParser; |
| 4 | |
| 5 | use function |
| 6 | array_diff_key, |
| 7 | array_flip, |
| 8 | array_intersect_key, |
| 9 | get_defined_vars, |
| 10 | trim; |
| 11 | |
| 12 | class ParserConfig { |
| 13 | // default configs |
| 14 | public static function api(): self { static $c; $c ??= new self( |
| 15 | ); return $c; } |
| 16 | |
| 17 | public static function form(): self { static $c; $c ??= new self( |
| 18 | unset_empty: true, |
| 19 | filters: [ |
| 20 | trim(...), |
| 21 | ], |
| 22 | ); return $c; } |
| 23 | |
| 24 | public bool $unset_empty = false; |
| 25 | public bool $invalid_empty = false; |
| 26 | public string $default_field_type = Field\StringField::class; |
| 27 | |
| 28 | protected const LIST_FIELDS = ['filters']; |
| 29 | public array $filters = []; |
| 30 | |
| 31 | public function __construct(... $opts) { $this->update(...$opts); } |
| 32 | |
| 33 | protected function update( |
| 34 | ?bool $unset_empty = null, |
| 35 | ?bool $invalid_empty = null, |
| 36 | ?string $default_field_type = null, |
| 37 | ?array $filters = null, |
| 38 | ): void { |
| 39 | foreach (get_defined_vars() as $k => $v) { |
| 40 | if ($v !== null) |
| 41 | $this->{$k} = $v; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | public function extend(... $opts): self { |
| 46 | $n = clone $this; |
| 47 | $n->update(...(array_diff_key($opts, array_flip(self::LIST_FIELDS)))); |
| 48 | if ($lfields = array_intersect_key($opts, array_flip(self::LIST_FIELDS))) { |
| 49 | foreach ($lfields as $k => $v) |
| 50 | // merge iterables and pass on non-iterables to let update throw error |
| 51 | $lfields[$k] = is_iterable($v) ? [...$n->{$k}, ...$v] : $v; |
| 52 | $n->update(...$lfields); |
| 53 | } |
| 54 | return $n; |
| 55 | } |
| 56 | } |