lemaur / laravel-typed-arr
Requires
- php: ^8.1
- illuminate/contracts: ^10.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.9
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
This package is auto-updated.
Last update: 2024-10-08 11:56:49 UTC
README
This package provides an object-oriented way to retrieve the Array data with the right type.
If you are familiar with static analysis tool like phpstan, you've probably encountered Cannot cast mixed to string
error (or similar).
This is usually happen when you try to cast a value returned from a method/function with a mixed
return type.
With this package you can get a string value (and not only string) directly from the Arr helper like:
use \Illuminate\Support\Arr; /** @var \Illuminate\Support\Stringable $myValue */ $myValue = Arr::string($data, 'my-value');
Support Me
Hey folks,
Do you like this package? Do you find it useful, and it fits well in your project?
I am glad to help you, and I would be so grateful if you considered supporting my work.
You can even choose 😃:
- You can sponsor me 😎 with a monthly subscription.
- You can buy me a coffee ☕ or a pizza 🍕 just for this package.
- You can plant trees 🌴. By using this link we will both receive 30 trees for free and the planet (and me) will thank you.
- You can "Star ⭐" this repository (it's free 😉).
Installation
You can install the package via composer:
composer require lemaur/laravel-typed-arr
Usage
Retrieving Stringable from Arr helper
You may use the string
method to retrieve the Arr helper item as an instance of Illuminate\Support\Stringable
:
use \Illuminate\Support\Arr; /** @var \Illuminate\Support\Stringable $name */ $name = Arr::string($data, 'name');
Retrieving Boolean from Arr helper
You may use the boolean
method to retrieve the Arr helper item as a boolean. The boolean
method returns true
for 1, "1", true, "true", "on", and "yes". All other values will return false
:
use \Illuminate\Support\Arr; /** @var bool $archived */ $archived = Arr::boolean($data, 'archived');
Retrieving Integer from Arr helper
You may use the integer
method to retrieve the Arr helper item as an integer:
use \Illuminate\Support\Arr; /** @var int $count */ $count = Arr::integer($data, 'count');
Retrieving Float from Arr helper
You may use the float
method to retrieve the Arr helper item as a float:
use \Illuminate\Support\Arr; /** @var float $amount */ $amount = Arr::float($data, 'amount');
Retrieving Date from Arr helper
You may use the date
method to retrieve the Arr helper item as a Carbon instance:
use \Illuminate\Support\Arr; /** @var \Carbon\Carbon $birthday */ $birthday = Arr::date($data, 'birthday');
The second and third arguments accepted by the date
method may be used to specify the date's format and timezone, respectively:
use \Illuminate\Support\Arr; /** @var \Carbon\Carbon $elapsed */ $elapsed = Arr::date($data, 'elapsed', '!H:i', 'Europe/Madrid');
In case of an invalid format, an InvalidArgumentException
will be thrown.
Retrieving Enum from Arr helper
You may use the enum
method to retrieve the Arr helper item as a PHP enum instance.
In case of an invalid value or the enum does not have a backing value that matches the input value, null
will be returned.
The enum
method accepts the name of the input value and the enum class as its first and second arguments:
use App\Enums\Status; use \Illuminate\Support\Arr; /** @var Status $status */ $status = Arr::enum($data, 'status', Status::class);
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.