Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
62.07% |
18 / 29 |
|
50.00% |
4 / 8 |
CRAP | |
0.00% |
0 / 1 |
| SerializedProperties | |
62.07% |
18 / 29 |
|
50.00% |
4 / 8 |
19.86 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createFromPrimitive | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| createFromMap | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| createFromList | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| isRegistered | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getRoot | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| withRegisteredObject | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| jsonSerialize | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 | |||
| 1 | <?php |
| 2 | namespace Apie\Serializer\PropertySerializer; |
| 3 | |
| 4 | use JsonSerializable; |
| 5 | use WeakMap; |
| 6 | |
| 7 | final class SerializedProperties implements JsonSerializable |
| 8 | { |
| 9 | private ?Typehint $root = null; |
| 10 | |
| 11 | private array $registeredObjects = []; |
| 12 | |
| 13 | /** @var WeakMap<object, mixed> */ |
| 14 | private WeakMap $registeredObjectsMap; |
| 15 | |
| 16 | public function __construct() |
| 17 | { |
| 18 | $this->registeredObjectsMap = new WeakMap(); |
| 19 | } |
| 20 | |
| 21 | public static function createFromPrimitive(string|int|float|bool|null $value): self |
| 22 | { |
| 23 | $instance = new self(); |
| 24 | $instance->root = Typehint::createFromPrimitive($value); |
| 25 | return $instance; |
| 26 | } |
| 27 | |
| 28 | public static function createFromMap(TypehintMap $properties): self |
| 29 | { |
| 30 | $instance = new self(); |
| 31 | $instance->root = new Typehint(TypeDefinition::Map, $properties); |
| 32 | return $instance; |
| 33 | } |
| 34 | |
| 35 | public static function createFromList(TypehintMap $properties): self |
| 36 | { |
| 37 | $instance = new self(); |
| 38 | $instance->root = new Typehint(TypeDefinition::Array, $properties); |
| 39 | return $instance; |
| 40 | } |
| 41 | |
| 42 | public function isRegistered(object $object): bool |
| 43 | { |
| 44 | return isset($this->registeredObjectsMap[$object]); |
| 45 | } |
| 46 | public function getRoot(): Typehint |
| 47 | { |
| 48 | if (null === $this->root) { |
| 49 | throw new \LogicException('Root is not set'); |
| 50 | } |
| 51 | return $this->root; |
| 52 | } |
| 53 | |
| 54 | public function withRegisteredObject(object $object, TypehintMap $result): self |
| 55 | { |
| 56 | if (isset($this->registeredObjectsMap[$object])) { |
| 57 | return $this; |
| 58 | } |
| 59 | $clone = clone $this; |
| 60 | if (null === $clone->root) { |
| 61 | $clone->root = Typehint::createFromObject($object, $result); |
| 62 | } |
| 63 | $clone->registeredObjectsMap[$object] = $result; |
| 64 | $clone->registeredObjects[spl_object_hash($object)] = $object; |
| 65 | return $clone; |
| 66 | } |
| 67 | |
| 68 | public function jsonSerialize(): mixed |
| 69 | { |
| 70 | $objects = [ |
| 71 | 'objects' => [], |
| 72 | 'root' => $this->root, |
| 73 | ]; |
| 74 | foreach ($this->registeredObjects as $hash => $object) { |
| 75 | $objects['objects'][$hash] = $this->registeredObjectsMap[$object]; |
| 76 | } |
| 77 | |
| 78 | return $objects; |
| 79 | } |
| 80 | } |