From f3e5e958b7a5c7e7263944cdce3797cb64c7db3f Mon Sep 17 00:00:00 2001 From: Joel Male Date: Wed, 27 May 2026 21:16:07 +1000 Subject: [PATCH] fix: resolve Cart.php regressions after driver refactor - Cast \$value to string in updateQuantityRelative() before passing to preg_match() and str_replace() to fix TypeError when integer quantities are passed - Reconstruct ItemAttributeCollection and CartCondition objects from serialized arrays in getContent() so attribute property access (->size) and condition methods (.getName()) work correctly after deserialization - Store CartCondition objects directly (not serialized to arrays) in saveConditions() to preserve object identity for parsedRawValue tracking via getCalculatedValue() - Remove ->values() re-indexing from serializeItems() conditions serialization to preserve original condition array indices after partial removal --- src/Cart.php | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index c465c50..d565e6f 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -464,7 +464,24 @@ public function getTotalQuantity(): int|float public function getContent(): CartCollection { return (new CartCollection($this->driver->getItems())) - ->transform(fn($item) => $item instanceof ItemCollection ? $item : new ItemCollection($item, $this->config)) + ->transform(function ($item) { + if ($item instanceof ItemCollection) { + return $item; + } + + if (is_array($item) && isset($item['attributes']) && is_array($item['attributes'])) { + $item['attributes'] = new ItemAttributeCollection($item['attributes']); + } + + if (is_array($item) && isset($item['conditions']) && is_array($item['conditions'])) { + $item['conditions'] = array_map( + fn($c) => is_array($c) ? new CartCondition($c) : $c, + $item['conditions'] + ); + } + + return new ItemCollection($item, $this->config); + }) ->reject(fn($item) => ! ($item instanceof ItemCollection)); } @@ -549,7 +566,6 @@ private function serializeItems(CartCollection $cart): array } elseif (is_array($conditions) || $conditions instanceof \Traversable) { $data['conditions'] = collect($conditions) ->map(fn($c) => $c instanceof CartCondition ? $c->toArray() : $c) - ->values() ->toArray(); } else { $data['conditions'] = []; @@ -565,7 +581,7 @@ private function serializeItems(CartCollection $cart): array private function serializeConditions(CartConditionCollection $conditions): array { - return $conditions->map(fn(CartCondition $c) => $c->toArray())->toArray(); + return $conditions->all(); } protected function itemHasConditions(ItemCollection $item): bool @@ -583,13 +599,13 @@ protected function itemHasConditions(ItemCollection $item): bool protected function updateQuantityRelative(ItemCollection $item, string $key, mixed $value): ItemCollection { - if (preg_match('/\-/', $value) === 1) { - $value = (float) str_replace('-', '', $value); + if (preg_match('/\-/', (string) $value) === 1) { + $value = (float) str_replace('-', '', (string) $value); if (($item[$key] - $value) > 0) { $item[$key] -= $value; } - } elseif (preg_match('/\+/', $value) === 1) { - $item[$key] += (float) str_replace('+', '', $value); + } elseif (preg_match('/\+/', (string) $value) === 1) { + $item[$key] += (float) str_replace('+', '', (string) $value); } else { $item[$key] += (float) $value; }