- How do I install apie/serializer in a Laravel project?
- Run `composer require apie/serializer` in your project root. The package integrates seamlessly with Laravel’s autoloader and requires no additional configuration beyond basic Composer setup. Ensure your project’s PHP version matches the package’s requirements (check Packagist for details).
- What’s the difference between apie/serializer and Symfony’s Serializer?
- apie/serializer replaces Symfony’s context array with an `ApieSerializerContext`, which supports recursive serialization calls and shared settings. It retains the same core logic for normalization, denormalization, encoding, and decoding but is optimized for Apie’s ecosystem and complex data contracts.
- Can I use apie/serializer with Eloquent models in Laravel?
- Yes, the package works with Eloquent models by default, but you may need to customize normalizers/denormalizers to handle hidden/visible attributes or relationships. Extend `NormalizerInterface` or `DenormalizerInterface` to map model properties as needed, similar to how you’d configure Symfony’s Serializer.
- Does apie/serializer support polymorphic serialization (e.g., different types for the same field)?
- Yes, you can implement polymorphic serialization by creating custom normalizers/denormalizers. Use the `ApieSerializerContext` to pass type hints or conditional logic, then override the `normalize()` or `denormalize()` methods in your custom interfaces to handle dynamic field transformations.
- How do I handle circular references in nested objects?
- apie/serializer includes built-in support for recursive calls via `ApieSerializerContext`, which prevents infinite loops during normalization/denormalization. For circular references, configure the context to track visited objects or use the package’s default handling, which skips cycles by design.
- Is apie/serializer compatible with Laravel’s JsonResource?
- Yes, you can integrate it by extending Laravel’s `JsonResource` and wrapping its `toArray()` method with `Serializer::normalize()`. For example, override `toArray()` to return the serialized output, then use the package’s context to enforce consistent data contracts across your API responses.
- What alternatives exist for serialization in Laravel?
- For simple cases, Laravel’s `JsonResponse` or `Arrayable` interface suffice. For complex APIs, consider `league/fractal` (resource transformation) or `spatie/array-to-object` (array-to-object conversion). apie/serializer stands out for recursive contexts and Apie ecosystem integration but lacks community adoption.
- How do I test apie/serializer in a Laravel application?
- Test normalization/denormalization by mocking domain objects and comparing outputs with expected arrays/JSON. Use PHPUnit to verify edge cases like null values, nested resources, or circular references. Benchmark performance against Laravel’s native `json_encode()` for critical paths.
- What’s the failure mode if apie/serializer breaks in production?
- If the package fails, your app can fall back to Laravel’s native `json_encode()` or `JsonResponse`. Design a graceful degradation strategy by wrapping serializer calls in try-catch blocks and logging errors. Ensure critical data contracts are also handled by manual serialization logic.
- Does apie/serializer work with Laravel’s API resources (e.g., JsonResource)?
- Yes, you can pre-process data in `JsonResource::toArray()` using `Serializer::normalize()` before Laravel’s serialization. For example, extend `JsonResource` and replace `return $this->data` with `return $this->serializer->normalize($this->data)`. This maintains consistency while leveraging Laravel’s built-in API features.