Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.19% |
23 / 27 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| AbstractFieldData | |
85.19% |
23 / 27 |
|
33.33% |
1 / 3 |
12.47 | |
0.00% |
0 / 1 |
| createFromInput | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
6.40 | |||
| isSame | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
3.58 | |||
| getChanges | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Apie\ApieBundle\DataCollector\FieldData; |
| 4 | |
| 5 | use Apie\ApieBundle\DataCollector\ContextChange; |
| 6 | |
| 7 | abstract 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[$key]; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return new ContextChange( |
| 56 | $description, |
| 57 | $added, |
| 58 | $removed, |
| 59 | $modified, |
| 60 | ); |
| 61 | } |
| 62 | } |