Installation:
composer require baks-dev/reference-peels
Ensure your composer.json has PHP 8.4+ constraints.
First Use Case:
Peel a reference (e.g., a database record or API response) by wrapping it in a Peel class:
use BaksDev\ReferencePeels\Peel;
$data = ['id' => 1, 'name' => 'Test'];
$peeled = Peel::make($data);
The package abstracts "peeling" (stripping metadata or transforming data) via a fluent interface.
Key Classes:
Peel: Core class for peeling operations.PeelCollection: For peeling arrays/collections.Peelable: Trait for Eloquent models or custom classes to enable peeling.Where to Look First:
config/reference-peels.php for default behaviors (e.g., allowed peel methods).tests/ for usage examples (if tests exist).Peeling Eloquent Models:
use BaksDev\ReferencePeels\Peelable;
class User extends Model implements Peelable {
// Model logic...
}
$user = User::find(1);
$peeled = $user->peel(['id', 'name']); // Returns stripped data.
Custom Peel Methods:
Extend the Peel class to add domain-specific peeling logic:
class CustomPeel extends Peel {
public function peelUserData(array $data): array {
return [
'id' => $data['id'],
'email' => $data['email'] ?? null,
];
}
}
API Response Handling: Peel API responses before returning them to clients:
$response = $apiClient->get('/users/1');
$peeledResponse = Peel::make($response->data)->peel(['id', 'name', 'created_at']);
return response()->json($peeledResponse);
Batch Peeling:
Use PeelCollection for arrays or collections:
$users = User::all();
$peeledUsers = PeelCollection::make($users)
->peel(['id', 'name'])
->toArray();
Middleware for Peeling: Create middleware to peel responses globally:
namespace App\Http\Middleware;
use BaksDev\ReferencePeels\Peel;
use Closure;
class PeelResponseMiddleware {
public function handle($request, Closure $next) {
$response = $next($request);
if ($response->isJson()) {
$data = $response->getData();
$peeled = Peel::make($data)->peel(config('reference-peels.default_fields'));
$response->setData($peeled);
}
return $response;
}
}
Service Provider Integration:
Bind the Peel class to the container in AppServiceProvider:
public function register() {
$this->app->singleton(Peel::class, function () {
return new Peel(config('reference-peels'));
});
}
Over-Peeling:
id for relationships).peel(['id', 'name'])) instead of blacklists.Performance with Large Data:
Peel::shallow() for top-level peeling.Type Safety:
null) may throw errors.if (!is_array($data) && !method_exists($data, 'toArray')) {
return $data; // or throw custom exception
}
Configuration Overrides:
config/reference-peels.php may conflict with custom logic.$peel = new Peel(array_merge(config('reference-peels'), ['custom_method' => true]));
Eloquent Relationships:
$user = User::with('posts')->find(1);
$peeled = $user->peel(['id', 'posts.title']);
Enable Logging:
Add debug output to the Peel class to trace peeled fields:
Peel::debug(true); // Logs peeled fields to Laravel logs.
Test Edge Cases:
Peel::make([])->peel(['id']).Peel::make(new stdClass())->peel(['prop']).Peel::make($data)->peel(['id'])->withoutCircular().Check for Deprecations:
Monitor the 7.x branch for breaking changes (e.g., method signatures).
Custom Peel Strategies:
Implement BaksDev\ReferencePeels\Contracts\PeelStrategy for domain-specific logic:
class ApiPeelStrategy implements PeelStrategy {
public function peel(array $data, array $fields): array {
return array_intersect_key($data, array_flip($fields));
}
}
Event Listeners: Trigger events before/after peeling:
Peel::peeling(function ($data, $fields) {
// Pre-peel logic (e.g., sanitize fields).
});
Peel::peeled(function ($peeledData, $originalData, $fields) {
// Post-peel logic (e.g., log changes).
});
Macros:
Extend Peel with macros for reusable logic:
Peel::macro('audit', function ($fields) {
$peeled = $this->peel($fields);
Log::info("Peeled fields: " . implode(', ', $fields));
return $peeled;
});
Testing:
Mock the Peel class in tests:
$mockPeel = Mockery::mock(Peel::class);
$mockPeel->shouldReceive('peel')->andReturn(['id' => 1]);
$this->app->instance(Peel::class, $mockPeel);
Performance Optimization: Cache peeled results for immutable data:
$peeled = Peel::make($data)->peel(['id'])->remember(60); // Cache for 60s.
How can I help you explore Laravel packages today?