phrity/util-accessor
Access nested data (arrays, objects, scalars) using simple slash-delimited paths. Provides get() with optional default return and type coercion, plus has() to check if a path exists. Lightweight utility for safe, convenient data retrieval.
Strengths:
get(), has(), set()) for nested data access (arrays, objects, scalars), reducing boilerplate for traversing complex structures (e.g., Eloquent models, API responses, or config arrays).Type::STRING, Type::OBJECT), useful for API responses or form data normalization./, .) and support for PathAccessor/DataAccessor for specialized use cases (e.g., reusable paths or in-memory data manipulation).AccessorTrait enables lightweight adoption without tight coupling, ideal for utility classes or services.Gaps:
assessment-laravel_dev.md suggests Laravel-specific extensions (e.g., HasAccessors trait, RepoConfig) that aren’t part of the core package. These would require custom implementation.set() method mutates the input data structure, which may conflict with immutable design patterns or functional programming paradigms.$user->address->city) with path-based access ($accessor->get($user, 'address/city')), reducing repetitive code.with() for relationships).$resource->response->get('data/user/name')) and enforce consistent data shaping.$request->accessor->get('user.address.city')) with minimal boilerplate.user.address exists before accessing city).$configAccessor->get('services.api.timeout')) with a single interface.phrity/util-transformer for type coercion), reducing risk of version conflicts.$model->relation->attribute).assessment-laravel_dev.md describes unsupported features (e.g., HasAccessors trait, RepoConfig), which would need to be built in-house.$accessor->get($request, 'user.address.city') preferable to $request->input('user.address.city')?HasAccessors trait, RepoConfig) from assessment-laravel_dev.md, or build alternatives?set() method’s mutating behavior may require wrappers or alternatives.AccessorException be caught and translated into Laravel’s ValidationException or HttpResponse for APIs?// Before
$user->address->city;
// After
$accessor->get($user, 'address/city');
$resource->response->get('data.user.name');
$request->accessor->has('user.address') || fail('Address required');
DataAccessor for in-memory data manipulation (e.g., caching, transformations).Accessor.Accessor for read operations (get(), has()) in services or repositories.
$userData = $accessor->get($request->all(), 'user.profile');
set() for write operations in bulk data processing (e.g., imports).DataAccessor for in-memory data manipulation (e.g., caching layers).HasAccessors trait or RepoConfig wrapper based on assessment-laravel_dev.md.trait LaravelAccessorTrait {
use \Phrity\Util\AccessorTrait;
public function getAccessor(): Accessor {
return new Accessor();
}
}
$model->relation->attribute) in favor of paths.HasAccessors) require Laravel 8+.__get()/__set() could interfere with path traversal.set() will fail on immutable data (e.g., Laravel’s Collection or Carbon).composer.json and publish a config file (if needed) for custom separators/transformers.composer require phrity/util-accessor
Accessor to the container (Laravel) or instantiate manually.// Laravel Service Provider
$this->app->singleton(Accessor::class, fn() => new Accessor());
public function test_path_access() {
$data = ['user' => ['name' => 'John']];
$accessor = new Accessor();
$this->assertEquals('John', $accessor->get($data, 'user/name'));
}
How can I help you explore Laravel packages today?