iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🌿

Retrieving Values with Type-Safe Getters Using Laravel's Arr Class

に公開

Introduction

Recently, the following feature was released.

https://laravel-news.com/laravel-12-11-0#content-typed-getters-for-arr-helpers

It seems that it's now possible to specify the type when retrieving values from an array using the Illuminate\Support\Arr class.

I decided to take a quick look at what it's like.

Types and Methods

The supported types are array, boolean, float, integer, and string—five in total.
I've summarized which method to use for each in the table below.

Type Method
array array
boolean boolean
float float
integer integer
string string

Having summarized them, it's easy to understand since the type and method names are identical.
I've also included links to the manual for reference.

Usage

Now that I know the methods, I'll try them out briefly.

I'll use the following test data. (It is just a slightly modified version of the array from the release notes.)

$array = ['name' => '太郎', 'languages' => ['PHP', 'Ruby']];

Here is how it looked when using the Arr::array method with the array above.

Note: I am using Tinker for execution.

> use Illuminate\Support\Arr;
> $array = ['name' => '太郎', 'languages' => ['PHP', 'Ruby']];
= [
    "name" => "太郎",
    "languages" => [
      "PHP",
      "Ruby",
    ],
  ]

> Arr::array($array, 'languages');
= [
    "PHP",
    "Ruby",
  ]

> Arr::array($array, 'name');

   InvalidArgumentException  Array value for key [name] must be an array, string found.

> 

It works exactly as described in the release notes. Arrays can be retrieved, but strings cause an exception.

Conversely, let's see what happens when using the Arr::string method.

> use Illuminate\Support\Arr;
> $array = ['name' => '太郎', 'languages' => ['PHP', 'Ruby']];
= [
    "name" => "太郎",
    "languages" => [
      "PHP",
      "Ruby",
    ],
  ]

> Arr::string($array, 'name');
= "太郎"

> Arr::string($array, 'languages');

   InvalidArgumentException  Array value for key [languages] must be a string, array found.

> 

In the case of string, the string was retrieved successfully, but the array resulted in an exception.

The results for other types will likely be the same.

Looking at the pull request that added these methods, it seems that if the value being retrieved matches the method's type, it is returned; otherwise, an exception is thrown.

https://github.com/laravel/framework/pull/55567

The intended use case is likely to eliminate the need for manual type checking of retrieved values.
On the other hand, since you need to account for exceptions, it's a matter of which approach you prefer.

This might come down to personal preference.

Summary

While the manual provides the answers, I've explored and summarized them in my own way.

I'm not sure if the day will come when I actually use it, but if it does, it will likely be useful. (Probably)

Additional Notes

It seems that similar functionality is available for the config helper as well.

https://github.com/laravel/framework/pull/50140

Laravel's methods for retrieving array contents may have evolved in various ways over time.

However, there was a pull request to add this to the Env helper as well, but unfortunately, it was closed.

https://github.com/laravel/framework/pull/55658

GitHubで編集を提案

Discussion