From 646ec20a2281499ff9fff2a6e481cffebd1e52fb Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Thu, 26 Mar 2026 11:01:05 +0100 Subject: [PATCH] Cache type iso in AttributeValueEncoder Captures $this->typeEncoder->iso($context) once in iso() and passes it to the private to() and from() methods. Previously called up to 4 times per encode/decode (fixed value check, encode, decode, default). --- .../SimpleType/AttributeValueEncoder.php | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Encoder/SimpleType/AttributeValueEncoder.php b/src/Encoder/SimpleType/AttributeValueEncoder.php index 5bc702f..fd70687 100644 --- a/src/Encoder/SimpleType/AttributeValueEncoder.php +++ b/src/Encoder/SimpleType/AttributeValueEncoder.php @@ -27,17 +27,22 @@ public function __construct( */ public function iso(Context $context): Iso { + $typeIso = $this->typeEncoder->iso($context); + return (new Iso( - fn (mixed $value): ?string => $this->to($context, $value), - fn (?string $value): mixed => $this->from($context, $value), + fn (mixed $value): ?string => $this->to($context, $typeIso, $value), + fn (?string $value): mixed => $this->from($context, $typeIso, $value), )); } - public function to(Context $context, mixed $value): ?string + /** + * @param Iso $typeIso + */ + private function to(Context $context, Iso $typeIso, mixed $value): ?string { $meta = $context->type->getMeta(); $fixed = $meta->fixed() - ->map(fn (string $fixed): mixed => $this->typeEncoder->iso($context)->from($fixed)) + ->map(static fn (string $fixed): mixed => $typeIso->from($fixed)) ->unwrapOr(null); if ($fixed !== null && $value !== $fixed) { @@ -47,18 +52,21 @@ public function to(Context $context, mixed $value): ?string ); } - return $value !== null ? $this->typeEncoder->iso($context)->to($value) : null; + return $value !== null ? $typeIso->to($value) : null; } - public function from(Context $context, ?string $value): mixed + /** + * @param Iso $typeIso + */ + private function from(Context $context, Iso $typeIso, ?string $value): mixed { if ($value !== null) { - return $this->typeEncoder->iso($context)->from($value); + return $typeIso->from($value); } $meta = $context->type->getMeta(); $default = $meta->fixed()->or($meta->default())->unwrapOr(null); - return $default !== null ? $this->typeEncoder->iso($context)->from($default) : null; + return $default !== null ? $typeIso->from($default) : null; } }