Repository navigation
Decode objects without exception-driven control flow #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| use Soap\Encoding\Xml\Writer\XsdTypeXmlElementWriter; | ||
| use Soap\Encoding\Xml\Writer\XsiAttributeBuilder; | ||
| use Soap\Engine\Metadata\Model\Property; | ||
| use stdClass; | ||
| use VeeWee\Reflecta\Iso\Iso; | ||
| use VeeWee\Reflecta\Lens\LensInterface; | ||
| use function is_array; | ||
|
|
@@ -125,32 +126,39 @@ static function (string $normalizePropertyName, Property $property) use ($object | |
| private function from(Context $context, ObjectAccess $objectAccess, Element $data): object | ||
| { | ||
| $nodes = (new DocumentToLookupArrayReader())($data); | ||
| $values = map_with_key( | ||
| $objectAccess->properties, | ||
| static function (string $normalizePropertyName, Property $property) use ($objectAccess, $nodes): mixed { | ||
| $type = $property->getType(); | ||
| $meta = $type->getMeta(); | ||
|
|
||
| /** @var string|null $value */ | ||
| $value = self::runLens( | ||
| $objectAccess->decoderLenses[$normalizePropertyName], | ||
| $nodes, | ||
| ); | ||
| $iso = $objectAccess->isos[$normalizePropertyName]; | ||
| $defaultValue = $meta->isList()->unwrapOr(false) ? [] : null; | ||
|
|
||
| /** @psalm-suppress PossiblyNullArgument */ | ||
| return match(true) { | ||
| $meta->isAttribute()->unwrapOr(false) => $iso->from($value), | ||
| default => $value !== null ? $iso->from($value) : $defaultValue, | ||
| }; | ||
| }, | ||
| ); | ||
|
|
||
| // stdClass has no declared properties: reflection-based hydration would build and catch | ||
| // an exception for every single property before falling back to a dynamic one. | ||
| if ($this->className === stdClass::class) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will of course only add benifits for stdClass. Wouldn't it make sense to make set_properties smarter? https://github.com/veewee/reflecta/blob/main/src/Reflect/properties_set.php We could always load all properties before adding the predicate. Not sure what the timing impact would be for the both scenarios. |
||
| /** @var TObj */ | ||
| return (object) $values; | ||
| } | ||
|
|
||
| /** @var Iso<TObj, TObj, array<string, mixed>, array<string, mixed>> $objectData */ | ||
| $objectData = object_data($this->className); | ||
|
|
||
| return $objectData->from( | ||
| map_with_key( | ||
| $objectAccess->properties, | ||
| static function (string $normalizePropertyName, Property $property) use ($objectAccess, $nodes): mixed { | ||
| $type = $property->getType(); | ||
| $meta = $type->getMeta(); | ||
|
|
||
| /** @var string|null $value */ | ||
| $value = self::runLens( | ||
| $objectAccess->decoderLenses[$normalizePropertyName], | ||
| $nodes, | ||
| ); | ||
| $iso = $objectAccess->isos[$normalizePropertyName]; | ||
| $defaultValue = $meta->isList()->unwrapOr(false) ? [] : null; | ||
|
|
||
| /** @psalm-suppress PossiblyNullArgument */ | ||
| return match(true) { | ||
| $meta->isAttribute()->unwrapOr(false) => $iso->from($value), | ||
| default => $value !== null ? $iso->from($value) : $defaultValue, | ||
| }; | ||
| }, | ||
| ) | ||
| ); | ||
| return $objectData->from($values); | ||
| } | ||
|
|
||
| private static function runLens(LensInterface $lens, mixed $data, mixed $default = null): mixed | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want to have the speedup everywhere, we should somehow be able to get rid of this |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might move this to a Lens namespace where we provide some highly optimized lenses.