Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
25.93% covered (danger)
25.93%
7 / 27
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractFieldData
25.93% covered (danger)
25.93%
7 / 27
0.00% covered (danger)
0.00%
0 / 3
70.53
0.00% covered (danger)
0.00%
0 / 1
 createFromInput
77.78% covered (warning)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
6.40
 isSame
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 getChanges
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace Apie\ApieBundle\DataCollector\FieldData;
4
5use Apie\ApieBundle\DataCollector\ContextChange;
6
7abstract class AbstractFieldData implements \Stringable
8{
9    protected string $typehint;
10
11    protected mixed $data;
12
13    final public static function createFromInput(mixed $input): AbstractFieldData
14    {
15        if (is_array($input)) {
16            return ArrayType::createFromArray($input);
17        }
18        if (is_object($input)) {
19            return new ObjectType($input);
20        }
21        if (is_resource($input)) {
22            return new ResourceType($input);
23        }
24        if (is_scalar($input) || is_null($input)) {
25            return new ScalarType($input);
26        }
27
28        return new UnknownType($input);
29    }
30
31    public function isSame(AbstractFieldData $fieldData): bool
32    {
33        if (get_class($this) !== get_class($fieldData)) {
34            return false;
35        }
36        if ($this->typehint !== $fieldData->typehint) {
37            return false;
38        }
39        return $this->data === $fieldData->data;
40    }
41
42    final public function getChanges(string $description, ArrayType $next): ContextChange
43    {
44        assert($this instanceof ArrayType);
45        $removed = array_diff_key($this->data, $next->data);
46        $added = array_diff_key($next->data, $this->data);
47
48        $modified = [];
49        foreach (array_intersect_key($this->data, $next->data) as $key => $value) {
50            if (!$this->data[$key]->isSame($next->data[$key])) {
51                $modified[$key] = $next->data;
52            }
53        }
54
55        return new ContextChange(
56            $description,
57            $added,
58            $removed,
59            $modified,
60        );
61    }
62}