1313 * Encodes and decodes cache payloads for storage.
1414 *
1515 * Strategy:
16- * - Scalars, arrays, and null are encoded as JSON (with exceptions on error) to keep
17- * values portable and human-inspectable across services.
18- * - Other values (e.g., objects) fall back to PHP's native serialization to retain
19- * fidelity for framework- or application-specific types.
16+ * - Arrays are encoded via PHP's native serialization to preserve complex nested
17+ * structures and prevent ambiguity with JSON objects vs arrays when decoding.
18+ * - Scalars and null are encoded as JSON (throwing on errors) to remain compact,
19+ * portable, and human-inspectable.
20+ * - Objects attempt JSON encoding first to support simple DTO-like objects; if
21+ * that fails, fall back to native serialization for full fidelity.
2022 *
2123 * Architectural notes:
2224 * - The class is immutable and stateless; marked as readonly for clarity.
23- * - Decoding first attempts JSON, then falls back to unserialize; if both fail,
24- * null is returned to indicate an un-decodable payload.
25+ * - Decoding tries JSON first, then unserialize(); if both fail, returns null.
2526 */
2627final readonly class Encoder
2728{
@@ -35,7 +36,6 @@ public function encode(mixed $data): string
3536 }
3637
3738 // For objects, prefer JSON to enable safe round-tripping of simple public data structures
38- // (e.g., anonymous classes with public properties). If JSON encoding fails, fall back to
3939 // native serialization for complex framework types.
4040 try {
4141 return json_encode ($ data , JSON_THROW_ON_ERROR );
0 commit comments