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 Apie\Core\Attributes\StoreOptions; |
| 5 | use JsonSerializable; |
| 6 | use WeakMap; |
| 7 | |
| 8 | final class SerializedProperties implements JsonSerializable |
| 9 | { |
| 10 | private ?Typehint $root = null; |
| 11 | |
| 12 | private array $registeredObjects = []; |
| 13 | |
| 14 | /** @var WeakMap<object, mixed> */ |
| 15 | #[StoreOptions(alwaysMixedData: true)] |
| 16 | private WeakMap $registeredObjectsMap; |
| 17 | |
| 18 | public function __construct() |
| 19 | { |
| 20 | $this->registeredObjectsMap = new WeakMap(); |
| 21 | } |
| 22 | |
| 23 | public static function createFromPrimitive(string|int|float|bool|null $value): self |
| 24 | { |
| 25 | $instance = new self(); |
| 26 | $instance->root = Typehint::createFromPrimitive($value); |
| 27 | return $instance; |
| 28 | } |
| 29 | |
| 30 | public static function createFromMap(TypehintMap $properties): self |
| 31 | { |
| 32 | $instance = new self(); |
| 33 | $instance->root = new Typehint(TypeDefinition::Map, $properties); |
| 34 | return $instance; |
| 35 | } |
| 36 | |
| 37 | public static function createFromList(TypehintMap $properties): self |
| 38 | { |
| 39 | $instance = new self(); |
| 40 | $instance->root = new Typehint(TypeDefinition::Array, $properties); |
| 41 | return $instance; |
| 42 | } |
| 43 | |
| 44 | public function isRegistered(object $object): bool |
| 45 | { |
| 46 | return isset($this->registeredObjectsMap[$object]); |
| 47 | } |
| 48 | public function getRoot(): Typehint |
| 49 | { |
| 50 | if (null === $this->root) { |
| 51 | throw new \LogicException('Root is not set'); |
| 52 | } |
| 53 | return $this->root; |
| 54 | } |
| 55 | |
| 56 | public function withRegisteredObject(object $object, TypehintMap $result): self |
| 57 | { |
| 58 | if (isset($this->registeredObjectsMap[$object])) { |
| 59 | return $this; |
| 60 | } |
| 61 | $clone = clone $this; |
| 62 | if (null === $clone->root) { |
| 63 | $clone->root = Typehint::createFromObject($object, $result); |
| 64 | } |
| 65 | $clone->registeredObjectsMap[$object] = $result; |
| 66 | $clone->registeredObjects[spl_object_hash($object)] = $object; |
| 67 | return $clone; |
| 68 | } |
| 69 | |
| 70 | public function jsonSerialize(): mixed |
| 71 | { |
| 72 | $objects = [ |
| 73 | 'objects' => [], |
| 74 | 'root' => $this->root, |
| 75 | ]; |
| 76 | foreach ($this->registeredObjects as $hash => $object) { |
| 77 | $objects['objects'][$hash] = $this->registeredObjectsMap[$object]; |
| 78 | } |
| 79 | |
| 80 | return $objects; |
| 81 | } |
| 82 | } |