\n"; // 2. Create and store state $cartState = ShoppingCartState::fromArray([ 'items' => [ ['id' => '1', 'name' => 'Product A', 'price' => 29.99, 'quantity' => 2], ['id' => '2', 'name' => 'Product B', 'price' => 15.50, 'quantity' => 1] ], 'discount_code' => 'SAVE10', 'discount_percentage' => 10 ]); $stateManager->setState('user-123', $cartState, Duration::fromHours(1)); echo "✅ Stored ShoppingCartState for user-123\n"; // 3. Retrieve state - TYPE PRESERVED! $retrieved = $stateManager->getState('user-123'); echo "✅ Retrieved state type: " . get_class($retrieved) . "\n"; echo "✅ Discount code: " . $retrieved->discountCode . "\n"; echo "✅ Item count: " . count($retrieved->items) . "\n"; echo "✅ Discount percentage: " . $retrieved->discountPercentage . "%\n"; // 4. Verify type safety if ($retrieved instanceof ShoppingCartState) { echo "\n✅ TYPE SAFETY VERIFIED: Retrieved concrete ShoppingCartState (not mixed)!\n"; echo "✅ IDE auto-completion will work for: \$retrieved->items, \$retrieved->discountCode, etc.\n"; } else { echo "\n❌ Type safety failed\n"; exit(1); } // 5. Test atomic update echo "\n=== Testing Atomic State Update ===\n"; $updated = $stateManager->updateState( 'user-123', function($state) { return $state ? $state->withDiscountCode('SAVE20', 20) : ShoppingCartState::fromArray([]); }, Duration::fromHours(1) ); echo "✅ Updated discount code to: " . $updated->discountCode . " (" . $updated->discountPercentage . "%)\n"; // 6. Verify statistics $stats = $stateManager->getStatistics(); echo "\n=== StateManager Statistics ===\n"; echo "Total keys: " . $stats->totalKeys . "\n"; echo "Hit count: " . $stats->hitCount . "\n"; echo "Miss count: " . $stats->missCount . "\n"; echo "Set count: " . $stats->setCount . "\n"; echo "Update count: " . $stats->updateCount . "\n"; echo "\n🎉 COMPLETE INTEGRATION SUCCESS!\n"; echo "✅ LiveComponentState works seamlessly with StateManager\n"; echo "✅ Type safety fully preserved throughout\n"; echo "✅ All domain-specific methods accessible\n";