Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/selective-php/array-reader
A strictly typed array reader for PHP
https://github.com/selective-php/array-reader
array array-reader php strictly-typed
Last synced: 4 days ago
JSON representation
A strictly typed array reader for PHP
- Host: GitHub
- URL: https://github.com/selective-php/array-reader
- Owner: selective-php
- License: mit
- Created: 2019-10-19T13:31:56.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-09-09T12:39:11.000Z (about 1 year ago)
- Last Synced: 2024-09-19T11:19:01.363Z (2 months ago)
- Topics: array, array-reader, php, strictly-typed
- Language: PHP
- Size: 34.2 KB
- Stars: 11
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# selective/array-reader
A strictly typed array reader for PHP.
[![Latest Version on Packagist](https://img.shields.io/github/release/selective-php/array-reader.svg)](https://packagist.org/packages/selective/array-reader)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE)
[![Build Status](https://github.com/selective-php/array-reader/workflows/build/badge.svg)](https://github.com/selective-php/array-reader/actions)
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/selective-php/array-reader.svg)](https://scrutinizer-ci.com/g/selective-php/array-reader/code-structure)
[![Quality Score](https://img.shields.io/scrutinizer/quality/g/selective-php/array-reader.svg)](https://scrutinizer-ci.com/g/selective-php/array-reader/?branch=master)
[![Total Downloads](https://img.shields.io/packagist/dt/selective/array-reader.svg)](https://packagist.org/packages/selective/array-reader/stats)## Requirements
* PHP 8.1+
## Installation
```bash
composer require selective/array-reader
```## Usage
You can use the `ArrayReader` to read single values from a multidimensional
array by passing the path to one of the `get{type}()` and `find{type}()` methods.Each `get*() / find*()` method takes a default value as second argument.
If the path cannot be found in the original array, the default is used as return value.A `get*()` method returns only the declared return type.
If the default value is not given and the element cannot be found, an exception is thrown.A `find*()` method returns only the declared return type or `null`.
No exception is thrown if the element cannot be found.```php
[
'key2' => [
'key3' => 'value1',
]
]
]);// Output: value1
echo $arrayReader->getString('key1.key2.key3');
```## Better Code Quality
Converting complex data with simple PHP works by using a lot of type casting and `if` conditions etc.
This leads to very high cyclomatic complexity and nesting depth, and thus poor "code rating".**Before**: Conditions: 10, Paths: 512, CRAP Score: 10
Click to expand!
**After**: Conditions: 1, Paths: 1, CRAP Score: 1
Click to expand!
## Similar libraries
* https://github.com/michaelpetri/typed-input
* https://github.com/codeliner/array-reader
* https://github.com/adbario/php-dot-notation
* https://symfony.com/doc/current/components/property_access.html## License
The MIT License (MIT). Please see [License File](LICENSE) for more information.