Skip to content

Commit 14244e5

Browse files
committed
Cache inner iso in XsiTypeEncoder to avoid redundant iso() calls
Captures $this->encoder->iso($context) once in iso() and passes the result to the private to() and from() methods. This avoids rebuilding the inner iso on every encode/decode invocation. In from(), when xsi:type detection resolves to the same encoder, the cached iso is reused instead of calling iso() again.
1 parent a38af3e commit 14244e5

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

src/Encoder/XsiTypeEncoder.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,37 @@ 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+
private function to(Iso $innerIso, mixed $value): string
4245
{
43-
// There is no way to know what xsi:type to use when encoding any type.
44-
// The type defined in the wsdl will always be used to encode the value.
45-
// 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);
46+
return $innerIso->to($value);
4747
}
4848

49-
private function from(Context $context, Element $value): mixed
49+
private function from(Context $context, Iso $innerIso, Element $value): mixed
5050
{
5151
/** @var XmlEncoder<string, mixed> $encoder */
5252
$encoder = match (true) {
5353
$this->encoder instanceof Feature\DisregardXsiInformation => $this->encoder,
5454
default => XsiTypeDetector::detectEncoderFromXmlElement($context, $value->element())->unwrapOr($this->encoder)
5555
};
5656

57-
return $encoder->iso($context)->from($value);
57+
$iso = $encoder === $this->encoder ? $innerIso : $encoder->iso($context);
58+
59+
return $iso->from($value);
5860
}
5961
}

0 commit comments

Comments
 (0)