codeliner/array-reader
Read values from multidimensional PHP arrays using dot-paths with escaping for dotted keys. Typed getters like stringValue() accept defaults when paths are missing, and pathExists() lets you distinguish null values from non-existent paths.
codeliner/array-reader package provides a clean abstraction for safely accessing nested array values, which aligns well with Laravel’s common patterns for configuration, request payloads, or Eloquent model attribute access. It mitigates risks of UndefinedIndex or UndefinedOffset errors by offering type-safe retrieval with defaults.Arr::get() (via Illuminate\Support\Arr) but adds explicit type safety (e.g., stringValue(), intValue()) and path existence checks (pathExists()). Useful for:
config('services.stripe.api_key')).Arr helper covers basic use cases, this package’s strict typing and path existence checks justify its adoption for projects requiring explicit control over array access.AppServiceProvider for global access:
$this->app->singleton('arrayReader', function ($app) {
return new \Codeliner\ArrayReader\ArrayReader();
});
Then inject via constructor or resolve via $app->make('arrayReader').Arr?
intValue() vs. Arr::get() returning mixed)?pathExists()) a critical feature for validation?Arr::get() (likely negligible, but worth validating).Validator)?Request objects or config)?$reader->stringValue('data.user.address.city')).Arr::get() chains with type-safe alternatives.stringValue(), intValue()) improve PHPDoc/PSR-12 compliance.$value = Arr::get($array, 'path.to.value', 'default');
With:
$reader = new ArrayReader($array);
$value = $reader->stringValue('path.to.value', 'default');
$reader->pathExists('required.field')).ArrayReader into services handling complex data structures.Arr for simple cases, migrate critical paths to ArrayReader.Arr helper. Package is additive.Request objects, config files).public function test_request_array_access() {
$request = new Request([...]);
$reader = new ArrayReader($request->all());
$this->assertEquals('value', $reader->stringValue('data.nested.key'));
}
composer.json and bootstrap as a singleton.Arr::get($user->toArray(), 'profile.address')).ArrayReader vs. Arr.pathExists() returns false).intValue() won’t return a string).NULL values and missing paths (use pathExists()).Arr::get().| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Invalid path access | Returns default value (safe). | Use pathExists() for critical paths. |
| Circular references | Infinite loop (unlikely in Laravel). | Avoid passing recursive arrays. |
| PHP version incompatibility | Fails to install. | Pin to ~2.0 in composer.json. |
| Key escaping errors | Silent failure (e.g., with\.dot). |
Document escaping rules in team docs. |
Request, Config, and Eloquent.Arr to ArrayReader.NULL vs. missing paths).UndefinedIndex exceptions).How can I help you explore Laravel packages today?