2025-07-25 20:06:31 [::1][-][-][error][application] Invalid cartdata JSON structure:…
- User
2025-07-25 20:06:31 [::1][-][-][error][application] Invalid cart_data JSON structure: [{"id":1,"quantity":1,"available_quantity":4,"total_price":400}]; Context: { "cart data": [ { "id": 1, "quantity": 1, "available_quantity": 4, "total_price": 400 } ], "method": "app\components\services\SessionUserService::writeSession", "file": "/components/services/SessionUserService.php", "line": 189 }
codeprivate function validateCartData(array $cartData): bool { // Configuration: max allowed items in cart $maxItems = 10; if (count($cartData) > $maxItems) { $this->logDebug("Cart data exceeds maximum {$maxItems} allowed items: " . count($cartData), [], 'validation'); return false; } foreach ($cartData as $index => $item) { if (!is_array($item) || !isset($item['id'], $item['quantity'])) { $this->logDebug("Cart item at index {$index} missing required keys or invalid format.", [ 'item' => $item, ], 'validation'); return false; } if (!is_int($item['id']) && !is_string($item['id'])) { $this->logDebug("Cart item 'id' at index {$index} must be int or string.", [ 'id' => $item['id'], ], 'validation'); return false; } $qty = $item['quantity']; // Validate quantity is positive integer or string of digits if (!((is_int($qty) && $qty > 0) || (is_string($qty) && ctype_digit($qty) && (int) $qty > 0)) ) { $this->logDebug("Cart item 'quantity' at index {$index} must be a positive integer.", [ 'quantity' => $qty, ], 'validation'); return false; } // Optional: reject unexpected extra keys in cart items to avoid abuse $allowedItemKeys = ['id', 'quantity']; foreach ($item as $key => $value) { if (!in_array($key, $allowedItemKeys, true)) { $this->logDebug("Unexpected key {$key} in cart item at index {$index}.", [ 'item' => $item, ], 'validation'); return false; } // Optionally reject nested arrays in item fields (no complex objects) if (is_array($value) || is_object($value)) { $this->logDebug("Cart item value for key {$key} at index {$index} is nested array/object.", [ 'value type' => gettype($value), 'item' => $item, ], 'validation'); return false; } }//*/ } /** * Optional: Check total serialized length of cart_data JSON to prevent too large carts */ $encoded = json_encode($cartData); if ($encoded === false || strlen($encoded) > 8192) { $this->logDebug('Encoded extra_data JSON too large or failed to encode: ' . ($encoded === false ? 0 : strlen($encoded)), [ 'length' => $encoded === false ? 0 : strlen($encoded), ], 'validation'); return false; }//*/ return true; } - Assistant
The error message:
codeInvalid cart_data JSON structure: [{"id":1,"quantity":1,"available_quantity":4,"total_price":400}] - User
what I was trying to get to work was this from the cartController:
private function ensureSessionReady(): void { this->__sessionUser->readSession();
codeif ($sessionData === null) { $existingExtraData = $sessionData['extra_data'] ?? []; $newData = ['cart_start_time' => time()]; $mergedExtraData = array_merge($existingExtraData, $newData); $this->__sessionUser->writeSession(['extra_data' => $mergedExtraData]); } }I dont understand why cart_start_time is not being added if its new data
- Assistant