Download the PHP package mellivora/laravel-api-caster without Composer
On this page you can find all versions of the php package mellivora/laravel-api-caster. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mellivora/laravel-api-caster
More information about mellivora/laravel-api-caster
Files in mellivora/laravel-api-caster
Package laravel-api-caster
Short Description Convert API response results to Entity objects like Laravel Eloquent. Enhanced with type-safe collections and meta support. Supports PHP 8.3+ and Laravel 10+.
License MIT
Homepage https://github.com/zhouyl/laravel-api-caster
Informations about the package laravel-api-caster
Laravel API Caster
Convert API response results to Entity objects like Laravel Eloquent. This package provides a powerful and flexible way to transform API responses into structured, type-safe entities with support for casting, mapping, data transformation, and enhanced collection handling with metadata support.
Features
- ๐ Laravel Eloquent-like API - Familiar syntax for Laravel developers
- ๐ Automatic Type Casting - Built-in support for common data types
- ๐ฏ Custom Casters - Create your own casting logic
- ๐ฆ Entity Mapping - Map nested data to Entity objects
- ๐ง Flexible Configuration - Includes, excludes, renames, and more
- ๐ Collection with Meta - Enhanced collections with metadata support
- ๐ก๏ธ Type-Safe Collections - Complete type validation for all collection operations
- ๐งช Fully Tested - Comprehensive test suite with 84.79% coverage
- โก High Performance - Optimized for speed and memory efficiency
- ๐ Type Safe - Full PHP 8.3+ type declarations
Requirements
- PHP 8.3 or 8.4
- Laravel 10.x, 11.x, or 12.x
Installation
You can install the package via composer:
Quick Start
Basic Usage
Collections
Advanced Usage
Type Casting
Entity Mapping
Field Configuration
Custom Casters
Available Cast Types
| Type | Description | Example |
|---|---|---|
int, integer |
Cast to integer | '123' โ 123 |
float, double, real |
Cast to float | '12.34' โ 12.34 |
string |
Cast to string | 123 โ '123' |
bool, boolean |
Cast to boolean | 1 โ true |
array |
Cast JSON to array | '[1,2,3]' โ [1,2,3] |
json |
Alias for array | Same as array |
object |
Cast JSON to object | '{"a":1}' โ stdClass |
collection |
Cast to Collection | [1,2,3] โ Collection |
date |
Cast to Carbon date | '2023-01-01' โ Carbon |
datetime |
Cast to Carbon datetime | '2023-01-01 12:00:00' โ Carbon |
timestamp |
Cast timestamp to Carbon | 1672574400 โ Carbon |
decimal:2 |
Cast to decimal string | 12.345 โ '12.35' |
date:Y-m-d |
Custom date format | Custom format |
datetime:Y-m-d H:i |
Custom datetime format | Custom format |
Testing
Code Quality
API Documentation
Entity Class
Methods
__construct(iterable $attributes = [], array $meta = [])- Create new entityfrom(Response $response): static- Create from Response objectcollection(iterable $items, array $meta = []): Collection- Create collectioncollectionResponse(Response $response): Collection- Create collection from ResponsetoArray(): array- Convert to arraytoJson(int $options = 0): string- Convert to JSONkeys(): array- Get all keysvalues(): array- Get all valuesisEmpty(): bool- Check if emptycopy(): static- Create a copymeta(string $key = null, mixed $default = null): mixed- Get meta dataorigin(string $key = null, mixed $default = null): mixed- Get original data
Response Class
Methods
__construct(HttpResponse|MessageInterface $response)- Create new responsecode(): int- Get response codemessage(): string- Get response messagedata(string $key = null, mixed $default = null): mixed- Get response datameta(string $key = null, mixed $default = null): mixed- Get response metatoArray(): array- Convert to array
Caster Class
Methods
cast(string $cast, mixed $value): mixed- Cast valuevalue(string $cast, mixed $value): mixed- Get original value
Best Practices
- Use Type Hints: Always define proper types in your entity classes
- Leverage Caching: Cache frequently used entities to improve performance
- Validate Data: Use Laravel's validation before creating entities
- Handle Nulls: Always consider null values in your casting logic
- Test Thoroughly: Write tests for your custom entities and casters
Performance Tips
- Use
includesto limit processed fields - Avoid deep nesting when possible
- Cache entity instances for repeated use
- Use collections for bulk operations
Troubleshooting
Common Issues
Issue: "Class not found" error when using custom casters
Solution: Ensure your caster class implements the Castable interface
Issue: Infinite recursion with circular references
Solution: Use excludes to break circular references
Issue: Memory issues with large datasets Solution: Process data in chunks using collections
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-api-caster with dependencies
brick/math Version ^0.10|^0.11|^0.12
nesbot/carbon Version ^2.0|^3.0
illuminate/contracts Version ^10.0|^11.0|^12.0
illuminate/http Version ^10.0|^11.0|^12.0
illuminate/support Version ^10.0|^11.0|^12.0
psr/http-message Version ^2.0