|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Soap\Encoding\Encoder; |
| 4 | + |
| 5 | +use RuntimeException; |
| 6 | +use Soap\Encoding\Xml\Node\Element; |
| 7 | +use Soap\Encoding\Xml\Node\ElementList; |
| 8 | +use Soap\Encoding\Xml\Reader\DocumentToLookupArrayReader; |
| 9 | +use Soap\Engine\Metadata\Model\Property; |
| 10 | +use Soap\Engine\Metadata\Model\Type; |
| 11 | +use VeeWee\Reflecta\Iso\Iso; |
| 12 | +use VeeWee\Reflecta\Lens\Lens; |
| 13 | +use function is_array; |
| 14 | +use function is_string; |
| 15 | +use function Psl\Dict\diff_by_key; |
| 16 | +use function Psl\Iter\first; |
| 17 | +use function Psl\Iter\reduce; |
| 18 | +use function Psl\Str\join; |
| 19 | +use function Psl\Type\string; |
| 20 | +use function Psl\Type\vec; |
| 21 | + |
| 22 | +/** |
| 23 | + * @implements XmlEncoder<array|string|null, string> |
| 24 | + * |
| 25 | + * @psalm-import-type LookupArray from DocumentToLookupArrayReader |
| 26 | + */ |
| 27 | +final class AnyElementEncoder implements Feature\ListAware, Feature\OptionalAware, XmlEncoder |
| 28 | +{ |
| 29 | + /** |
| 30 | + * This lens will be used to decode XML into an 'any' property. |
| 31 | + * It will contain all the XML tags available in the object that is surrounding the 'any' property. |
| 32 | + * Properties that are already known by the object, will be omitted. |
| 33 | + * |
| 34 | + * @return Lens<LookupArray, ElementList> |
| 35 | + */ |
| 36 | + public static function createDecoderLens(Type $type): Lens |
| 37 | + { |
| 38 | + $omittedKeys = reduce( |
| 39 | + $type->getProperties(), |
| 40 | + static fn (array $omit, Property $property): array => [ |
| 41 | + ...$omit, |
| 42 | + ...($property->getName() !== 'any' ? [$property->getName()] : []), |
| 43 | + ], |
| 44 | + [] |
| 45 | + ); |
| 46 | + |
| 47 | + /** |
| 48 | + * @param LookupArray $data |
| 49 | + * @return LookupArray |
| 50 | + */ |
| 51 | + $omit = static fn (array $data): array => diff_by_key($data, array_flip($omittedKeys)); |
| 52 | + |
| 53 | + /** @var Lens<LookupArray, ElementList> */ |
| 54 | + return new Lens( |
| 55 | + /** |
| 56 | + * @psalm-suppress MixedArgumentTypeCoercion - Psalm gets confused about the result of omit. |
| 57 | + * @param LookupArray $data |
| 58 | + */ |
| 59 | + static fn (array $data): ElementList => ElementList::fromLookupArray($omit($data)), |
| 60 | + static fn (array $_data, ElementList $_value): never => throw new RuntimeException('Readonly lens') |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @return Iso<array|string|null, string> |
| 66 | + */ |
| 67 | + public function iso(Context $context): Iso |
| 68 | + { |
| 69 | + $meta = $context->type->getMeta(); |
| 70 | + $isNullable = $meta->isNullable()->unwrapOr(false); |
| 71 | + $isList = $meta->isList()->unwrapOr(false); |
| 72 | + |
| 73 | + return new Iso( |
| 74 | + static fn (string|array|null $raw): string => match (true) { |
| 75 | + is_string($raw) => $raw, |
| 76 | + is_array($raw) => join(vec(string())->assert($raw), ''), |
| 77 | + default => '', |
| 78 | + }, |
| 79 | + /** |
| 80 | + * @psalm-suppress DocblockTypeContradiction - Psalm gets confused about the return type of first() in default case. |
| 81 | + * @psalm-return null|array<array-key, string>|string |
| 82 | + */ |
| 83 | + static fn (ElementList|string $xml): mixed => match(true) { |
| 84 | + is_string($xml) => $xml, |
| 85 | + $isList && !$xml->hasElements() => [], |
| 86 | + $isNullable && !$xml->hasElements() => null, |
| 87 | + $isList => $xml->traverse(static fn (Element $element) => $element->value()), |
| 88 | + default => first($xml->elements())?->value(), |
| 89 | + } |
| 90 | + ); |
| 91 | + } |
| 92 | +} |
0 commit comments