Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
63.16% |
12 / 19 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ArrayType | |
63.16% |
12 / 19 |
|
50.00% |
2 / 4 |
15.00 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| createFromArray | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| isSame | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
3.58 | |||
| __toString | |
37.50% |
3 / 8 |
|
0.00% |
0 / 1 |
7.91 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Apie\ApieBundle\DataCollector\FieldData; |
| 4 | |
| 5 | final class ArrayType extends AbstractFieldData |
| 6 | { |
| 7 | /** |
| 8 | * @param array<string, AbstractFieldData> $fields |
| 9 | */ |
| 10 | private function __construct( |
| 11 | array $fields |
| 12 | ) { |
| 13 | $this->typehint = 'array'; |
| 14 | $this->data = $fields; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * @param array<string, mixed> $input |
| 19 | */ |
| 20 | public static function createFromArray(array $input): self |
| 21 | { |
| 22 | $fields = []; |
| 23 | foreach ($input as $key => $value) { |
| 24 | $fields[$key] = AbstractFieldData::createFromInput($value); |
| 25 | } |
| 26 | |
| 27 | return new self($fields); |
| 28 | } |
| 29 | |
| 30 | public function isSame(AbstractFieldData $fieldData): bool |
| 31 | { |
| 32 | if (get_class($this) !== get_class($fieldData)) { |
| 33 | return false; |
| 34 | } |
| 35 | if ($this->typehint !== $fieldData->typehint) { |
| 36 | return false; |
| 37 | } |
| 38 | return $this->__toString() === $fieldData->__toString(); |
| 39 | } |
| 40 | |
| 41 | public function __toString(): string |
| 42 | { |
| 43 | if (array_is_list($this->data)) { |
| 44 | if (empty($this->data)) { |
| 45 | return 'empty list'; |
| 46 | } |
| 47 | return 'list(' . implode(', ', array_keys($this->data)) . ')'; |
| 48 | } |
| 49 | $list = []; |
| 50 | foreach ($this->data as $key => $value) { |
| 51 | $list[] = ' ' . $key . ' => ' . $value . ','; |
| 52 | } |
| 53 | return 'array(' . PHP_EOL . rtrim(implode(PHP_EOL, $list), ',') . PHP_EOL . ')'; |
| 54 | } |
| 55 | } |