Skip to content

Commit 7f5c15f

Browse files
tibbsataylorotwell
andauthored
[12.x] Typed getters for Arr helper (#55567)
* add typed array value getters to Arr support class * add tests for Arr typed array value getters * cleanup: remove nullable returns (not possible) * cleanup: style CI fixup * cleanup: style CI fixup * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 186debc commit 7f5c15f

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

src/Illuminate/Collections/Arr.php

+80
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,38 @@ public static function add($array, $key, $value)
4040
return $array;
4141
}
4242

43+
/**
44+
* Get an array item from an array using "dot" notation.
45+
*/
46+
public static function array(ArrayAccess|array $array, string|int|null $key, ?array $default = null): array
47+
{
48+
$value = Arr::get($array, $key, $default);
49+
50+
if (! is_array($value)) {
51+
throw new InvalidArgumentException(
52+
sprintf('Array value for key [%s] must be an array, %s found.', $key, gettype($value))
53+
);
54+
}
55+
56+
return $value;
57+
}
58+
59+
/**
60+
* Get a boolean item from an array using "dot" notation.
61+
*/
62+
public static function boolean(ArrayAccess|array $array, string|int|null $key, ?bool $default = null): bool
63+
{
64+
$value = Arr::get($array, $key, $default);
65+
66+
if (! is_bool($value)) {
67+
throw new InvalidArgumentException(
68+
sprintf('Array value for key [%s] must be a boolean, %s found.', $key, gettype($value))
69+
);
70+
}
71+
72+
return $value;
73+
}
74+
4375
/**
4476
* Collapse an array of arrays into a single array.
4577
*
@@ -286,6 +318,22 @@ public static function flatten($array, $depth = INF)
286318
return $result;
287319
}
288320

321+
/**
322+
* Get a float item from an array using "dot" notation.
323+
*/
324+
public static function float(ArrayAccess|array $array, string|int|null $key, ?float $default = null): float
325+
{
326+
$value = Arr::get($array, $key, $default);
327+
328+
if (! is_float($value)) {
329+
throw new InvalidArgumentException(
330+
sprintf('Array value for key [%s] must be a float, %s found.', $key, gettype($value))
331+
);
332+
}
333+
334+
return $value;
335+
}
336+
289337
/**
290338
* Remove one or many array items from a given array using "dot" notation.
291339
*
@@ -433,6 +481,22 @@ public static function hasAny($array, $keys)
433481
return false;
434482
}
435483

484+
/**
485+
* Get an integer item from an array using "dot" notation.
486+
*/
487+
public static function integer(ArrayAccess|array $array, string|int|null $key, ?int $default = null): int
488+
{
489+
$value = Arr::get($array, $key, $default);
490+
491+
if (! is_integer($value)) {
492+
throw new InvalidArgumentException(
493+
sprintf('Array value for key [%s] must be an integer, %s found.', $key, gettype($value))
494+
);
495+
}
496+
497+
return $value;
498+
}
499+
436500
/**
437501
* Determines if an array is associative.
438502
*
@@ -906,6 +970,22 @@ public static function sortRecursiveDesc($array, $options = SORT_REGULAR)
906970
return static::sortRecursive($array, $options, true);
907971
}
908972

973+
/**
974+
* Get a string item from an array using "dot" notation.
975+
*/
976+
public static function string(ArrayAccess|array $array, string|int|null $key, ?string $default = null): string
977+
{
978+
$value = Arr::get($array, $key, $default);
979+
980+
if (! is_string($value)) {
981+
throw new InvalidArgumentException(
982+
sprintf('Array value for key [%s] must be a string, %s found.', $key, gettype($value))
983+
);
984+
}
985+
986+
return $value;
987+
}
988+
909989
/**
910990
* Conditionally compile classes from an array into a CSS class list.
911991
*

tests/Support/SupportArrTest.php

+100
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,106 @@ public function testGet()
516516
$this->assertSame('bar', Arr::get(['' => ['' => 'bar']], '.'));
517517
}
518518

519+
public function testItGetsAString()
520+
{
521+
$test_array = ['string' => 'foo bar', 'integer' => 1234];
522+
523+
// Test string values are returned as strings
524+
$this->assertSame(
525+
'foo bar', Arr::string($test_array, 'string')
526+
);
527+
528+
// Test that default string values are returned for missing keys
529+
$this->assertSame(
530+
'default', Arr::string($test_array, 'missing_key', 'default')
531+
);
532+
533+
// Test that an exception is raised if the value is not a string
534+
$this->expectException(InvalidArgumentException::class);
535+
$this->expectExceptionMessageMatches('#^Array value for key \[integer\] must be a string, (.*) found.#');
536+
Arr::string($test_array, 'integer');
537+
}
538+
539+
public function testItGetsAnInteger()
540+
{
541+
$test_array = ['string' => 'foo bar', 'integer' => 1234];
542+
543+
// Test integer values are returned as integers
544+
$this->assertSame(
545+
1234, Arr::integer($test_array, 'integer')
546+
);
547+
548+
// Test that default integer values are returned for missing keys
549+
$this->assertSame(
550+
999, Arr::integer($test_array, 'missing_key', 999)
551+
);
552+
553+
// Test that an exception is raised if the value is not an integer
554+
$this->expectException(InvalidArgumentException::class);
555+
$this->expectExceptionMessageMatches('#^Array value for key \[string\] must be an integer, (.*) found.#');
556+
Arr::integer($test_array, 'string');
557+
}
558+
559+
public function testItGetsAFloat()
560+
{
561+
$test_array = ['string' => 'foo bar', 'float' => 12.34];
562+
563+
// Test float values are returned as floats
564+
$this->assertSame(
565+
12.34, Arr::float($test_array, 'float')
566+
);
567+
568+
// Test that default float values are returned for missing keys
569+
$this->assertSame(
570+
56.78, Arr::float($test_array, 'missing_key', 56.78)
571+
);
572+
573+
// Test that an exception is raised if the value is not a float
574+
$this->expectException(InvalidArgumentException::class);
575+
$this->expectExceptionMessageMatches('#^Array value for key \[string\] must be a float, (.*) found.#');
576+
Arr::float($test_array, 'string');
577+
}
578+
579+
public function testItGetsABoolean()
580+
{
581+
$test_array = ['string' => 'foo bar', 'boolean' => true];
582+
583+
// Test boolean values are returned as booleans
584+
$this->assertSame(
585+
true, Arr::boolean($test_array, 'boolean')
586+
);
587+
588+
// Test that default boolean values are returned for missing keys
589+
$this->assertSame(
590+
true, Arr::boolean($test_array, 'missing_key', true)
591+
);
592+
593+
// Test that an exception is raised if the value is not a boolean
594+
$this->expectException(InvalidArgumentException::class);
595+
$this->expectExceptionMessageMatches('#^Array value for key \[string\] must be a boolean, (.*) found.#');
596+
Arr::boolean($test_array, 'string');
597+
}
598+
599+
public function testItGetsAnArray()
600+
{
601+
$test_array = ['string' => 'foo bar', 'array' => ['foo', 'bar']];
602+
603+
// Test array values are returned as arrays
604+
$this->assertSame(
605+
['foo', 'bar'], Arr::array($test_array, 'array')
606+
);
607+
608+
// Test that default array values are returned for missing keys
609+
$this->assertSame(
610+
[1, 'two'], Arr::array($test_array, 'missing_key', [1, 'two'])
611+
);
612+
613+
// Test that an exception is raised if the value is not an array
614+
$this->expectException(InvalidArgumentException::class);
615+
$this->expectExceptionMessageMatches('#^Array value for key \[string\] must be an array, (.*) found.#');
616+
Arr::array($test_array, 'string');
617+
}
618+
519619
public function testHas()
520620
{
521621
$array = ['products.desk' => ['price' => 100]];

0 commit comments

Comments
 (0)