Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/Encoder/XsiTypeEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,46 @@ public function __construct(
*/
public function iso(Context $context): Iso
{
$innerIso = $this->encoder->iso($context);

return new Iso(
function (mixed $value) use ($context) : string {
return $this->to($context, $value);
function (mixed $value) use ($innerIso) : string {
return $this->to($innerIso, $value);
},
function (string|Element $value) use ($context) : mixed {
function (string|Element $value) use ($context, $innerIso) : mixed {
return $this->from(
$context,
$innerIso,
($value instanceof Element ? $value : Element::fromString(non_empty_string()->assert($value)))
);
}
);
}

private function to(Context $context, mixed $value): string
/**
* @param Iso<mixed, string> $innerIso
*/
private function to(Iso $innerIso, mixed $value): string
{
// There is no way to know what xsi:type to use when encoding any type.
// The type defined in the wsdl will always be used to encode the value.
// If you want more control over the encoded type, please control how to encode by using the MatchingValueEncoder.
return $this->encoder->iso($context)->to($value);
return $innerIso->to($value);
}

private function from(Context $context, Element $value): mixed
/**
* @param Iso<mixed, string> $innerIso
*/
private function from(Context $context, Iso $innerIso, Element $value): mixed
{
/** @var XmlEncoder<string, mixed> $encoder */
$encoder = match (true) {
$this->encoder instanceof Feature\DisregardXsiInformation => $this->encoder,
default => XsiTypeDetector::detectEncoderFromXmlElement($context, $value->element())->unwrapOr($this->encoder)
$iso = match (true) {
$this->encoder instanceof Feature\DisregardXsiInformation => $innerIso,
default => XsiTypeDetector::detectEncoderFromXmlElement($context, $value->element())
->map(static fn (XmlEncoder $encoder): Iso => $encoder->iso($context))
->unwrapOr($innerIso),
};

return $encoder->iso($context)->from($value);
/** @psalm-suppress ImplicitToStringCast - Encoders accept string|Element in from() */
return $iso->from($value);
}
}
Loading