Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
30.00% covered (danger)
30.00%
6 / 20
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ArrayType
30.00% covered (danger)
30.00%
6 / 20
50.00% covered (danger)
50.00%
2 / 4
44.30
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 createFromArray
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 isSame
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 __toString
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace Apie\ApieBundle\DataCollector\FieldData;
4
5final 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        $intersect = array_intersect_key($this->data, $fieldData->data);
39        return count($intersect) === count($fieldData->data);
40    }
41
42    public function __toString(): string
43    {
44        if (array_is_list($this->data)) {
45            if (empty($this->data)) {
46                return 'empty list';
47            }
48            return 'list(' . implode(', ', array_keys($this->data)) . ')';
49        }
50        $list = [];
51        foreach ($this->data as $key => $value) {
52            $list[] = '    ' . $key . ' => ' . $value . ',';
53        }
54        return 'array(' . PHP_EOL . rtrim(implode(PHP_EOL, $list), ',') . PHP_EOL . ')';
55    }
56}