|
8 | 8 | use Chubbyphp\Parsing\Errors; |
9 | 9 | use Chubbyphp\Parsing\ErrorsException; |
10 | 10 |
|
11 | | -abstract class AbstractObjectSchema extends AbstractSchema implements ObjectSchemaInterface |
| 11 | +abstract class AbstractObjectSchema extends AbstractSchemaInnerParse implements ObjectSchemaInterface |
12 | 12 | { |
13 | 13 | public const string ERROR_TYPE_CODE = 'abstract_object.type'; |
14 | 14 | public const string ERROR_TYPE_TEMPLATE = 'Type should be "array|\stdClass|\Traversable", {{given}} given'; |
@@ -64,54 +64,18 @@ public function __construct(array $fieldToSchema) |
64 | 64 | } |
65 | 65 |
|
66 | 66 | $this->fieldToSchema = $typeCheckedFieldToSchema; |
67 | | - } |
68 | | - |
69 | | - final public function parse(mixed $input): mixed |
70 | | - { |
71 | | - if ($input instanceof \stdClass || $input instanceof \Traversable) { |
72 | | - $input = (array) $input; |
73 | | - } |
74 | | - |
75 | | - if ($input instanceof \JsonSerializable) { |
76 | | - $input = $input->jsonSerialize(); |
77 | | - } |
78 | | - |
79 | | - try { |
80 | | - $input = $this->dispatchPreParses($input); |
81 | 67 |
|
82 | | - if (null === $input && $this->nullable) { |
83 | | - return null; |
| 68 | + $this->preParses[] = static function (mixed $input) { |
| 69 | + if ($input instanceof \stdClass || $input instanceof \Traversable) { |
| 70 | + return (array) $input; |
84 | 71 | } |
85 | 72 |
|
86 | | - if (!\is_array($input)) { |
87 | | - throw new ErrorsException( |
88 | | - new Error( |
89 | | - static::ERROR_TYPE_CODE, |
90 | | - static::ERROR_TYPE_TEMPLATE, |
91 | | - ['given' => $this->getDataType($input)] |
92 | | - ) |
93 | | - ); |
94 | | - } |
95 | | - |
96 | | - /** @var array<string, mixed> $input */ |
97 | | - $childrenErrors = new Errors(); |
98 | | - |
99 | | - $this->unknownFields($input, $childrenErrors); |
100 | | - |
101 | | - $output = $this->parseFields($input, $childrenErrors); |
102 | | - |
103 | | - if ($childrenErrors->has()) { |
104 | | - throw new ErrorsException($childrenErrors); |
| 73 | + if ($input instanceof \JsonSerializable) { |
| 74 | + return $input->jsonSerialize(); |
105 | 75 | } |
106 | 76 |
|
107 | | - return $this->dispatchPostParses($output); |
108 | | - } catch (ErrorsException $e) { |
109 | | - if ($this->catch) { |
110 | | - return ($this->catch)($input, $e); |
111 | | - } |
112 | | - |
113 | | - throw $e; |
114 | | - } |
| 77 | + return $input; |
| 78 | + }; |
115 | 79 | } |
116 | 80 |
|
117 | 81 | /** |
@@ -146,6 +110,32 @@ final public function optional(array $optional = []): static |
146 | 110 | return $clone; |
147 | 111 | } |
148 | 112 |
|
| 113 | + protected function innerParse(mixed $input): mixed |
| 114 | + { |
| 115 | + if (!\is_array($input)) { |
| 116 | + throw new ErrorsException( |
| 117 | + new Error( |
| 118 | + static::ERROR_TYPE_CODE, |
| 119 | + static::ERROR_TYPE_TEMPLATE, |
| 120 | + ['given' => $this->getDataType($input)] |
| 121 | + ) |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + /** @var array<string, mixed> $input */ |
| 126 | + $childrenErrors = new Errors(); |
| 127 | + |
| 128 | + $this->unknownFields($input, $childrenErrors); |
| 129 | + |
| 130 | + $output = $this->parseFields($input, $childrenErrors); |
| 131 | + |
| 132 | + if ($childrenErrors->has()) { |
| 133 | + throw new ErrorsException($childrenErrors); |
| 134 | + } |
| 135 | + |
| 136 | + return $output; |
| 137 | + } |
| 138 | + |
149 | 139 | /** |
150 | 140 | * @param array<string, mixed> $input |
151 | 141 | */ |
|
0 commit comments