Skip to content

Commit 8fc46d5

Browse files
authored
Merge pull request #52 from veewee/feature/xsi-type-encoder-iso-cache
Cache inner iso in XsiTypeEncoder
2 parents 9e3519e + 0d7914d commit 8fc46d5

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/Encoder/XsiTypeEncoder.php

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,46 @@ public function __construct(
2525
*/
2626
public function iso(Context $context): Iso
2727
{
28+
$innerIso = $this->encoder->iso($context);
29+
2830
return new Iso(
29-
function (mixed $value) use ($context) : string {
30-
return $this->to($context, $value);
31+
function (mixed $value) use ($innerIso) : string {
32+
return $this->to($innerIso, $value);
3133
},
32-
function (string|Element $value) use ($context) : mixed {
34+
function (string|Element $value) use ($context, $innerIso) : mixed {
3335
return $this->from(
3436
$context,
37+
$innerIso,
3538
($value instanceof Element ? $value : Element::fromString(non_empty_string()->assert($value)))
3639
);
3740
}
3841
);
3942
}
4043

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

49-
private function from(Context $context, Element $value): mixed
55+
/**
56+
* @param Iso<mixed, string> $innerIso
57+
*/
58+
private function from(Context $context, Iso $innerIso, Element $value): mixed
5059
{
51-
/** @var XmlEncoder<string, mixed> $encoder */
52-
$encoder = match (true) {
53-
$this->encoder instanceof Feature\DisregardXsiInformation => $this->encoder,
54-
default => XsiTypeDetector::detectEncoderFromXmlElement($context, $value->element())->unwrapOr($this->encoder)
60+
$iso = match (true) {
61+
$this->encoder instanceof Feature\DisregardXsiInformation => $innerIso,
62+
default => XsiTypeDetector::detectEncoderFromXmlElement($context, $value->element())
63+
->map(static fn (XmlEncoder $encoder): Iso => $encoder->iso($context))
64+
->unwrapOr($innerIso),
5565
};
5666

57-
return $encoder->iso($context)->from($value);
67+
/** @psalm-suppress ImplicitToStringCast - Encoders accept string|Element in from() */
68+
return $iso->from($value);
5869
}
5970
}

0 commit comments

Comments
 (0)