Total Complexity | 6 |
Total Lines | 64 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
30 | class SerializedObject extends Type implements Contract |
||
31 | { |
||
32 | /** |
||
33 | * Allowed classes for serialization of object type properties. |
||
34 | * |
||
35 | * <code> |
||
36 | * [ |
||
37 | * // An array of allowed classes for serialization for object types |
||
38 | * ClassName::class, |
||
39 | * ] |
||
40 | * </code> |
||
41 | * |
||
42 | * @return class-string[] |
||
43 | */ |
||
44 | protected const array ALLOWED_CLASSES = []; |
||
|
|||
45 | |||
46 | public function __construct(object $subject) |
||
47 | { |
||
48 | $this->subject = $subject; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @inheritDoc |
||
53 | * |
||
54 | * @throws JsonException |
||
55 | */ |
||
56 | public static function fromValue(mixed $value): static |
||
57 | { |
||
58 | if (is_string($value)) { |
||
59 | /** @var class-string[] $allowedClasses */ |
||
60 | $allowedClasses = static::ALLOWED_CLASSES; |
||
61 | |||
62 | return new static(Helper::fromSerializedString($value, $allowedClasses)); |
||
63 | } |
||
64 | |||
65 | return new static((object) $value); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @inheritDoc |
||
70 | */ |
||
71 | public function asValue(): object |
||
72 | { |
||
73 | return $this->subject; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @inheritDoc |
||
78 | * |
||
79 | * @throws JsonException |
||
80 | */ |
||
81 | public function asFlatValue(): string |
||
82 | { |
||
83 | return Helper::toSerializedString($this->subject); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @inheritDoc |
||
88 | * |
||
89 | * @throws JsonException |
||
90 | */ |
||
91 | public function modify(callable $closure): static |
||
92 | { |
||
93 | return static::fromValue($closure(clone $this->subject)); |
||
94 | } |
||
96 |