Skip to content

Commit 9482d0e

Browse files
[12.x] Introducing toArray to ComponentAttributeBag class (#55258)
* introducing toArray to ComponentAttributeBag class * using all inside the toArray * Update ComponentAttributeBag.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 8158e29 commit 9482d0e

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/Illuminate/View/ComponentAttributeBag.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use ArrayAccess;
66
use ArrayIterator;
7+
use Illuminate\Contracts\Support\Arrayable;
78
use Illuminate\Contracts\Support\Htmlable;
89
use Illuminate\Support\Arr;
910
use Illuminate\Support\Collection;
@@ -16,7 +17,7 @@
1617
use Stringable;
1718
use Traversable;
1819

19-
class ComponentAttributeBag implements ArrayAccess, IteratorAggregate, JsonSerializable, Htmlable, Stringable
20+
class ComponentAttributeBag implements Arrayable, ArrayAccess, IteratorAggregate, JsonSerializable, Htmlable, Stringable
2021
{
2122
use Conditionable, Macroable;
2223

@@ -38,7 +39,7 @@ public function __construct(array $attributes = [])
3839
}
3940

4041
/**
41-
* Get all of the attribute values.
42+
* Get all the attribute values.
4243
*
4344
* @return array
4445
*/
@@ -497,6 +498,16 @@ public function jsonSerialize(): mixed
497498
return $this->attributes;
498499
}
499500

501+
/**
502+
* Get all the attribute values.
503+
*
504+
* @return array
505+
*/
506+
public function toArray()
507+
{
508+
return $this->all();
509+
}
510+
500511
/**
501512
* Implode the attributes into a single HTML ready string.
502513
*

tests/View/ViewComponentAttributeBagTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,15 @@ public function testAttributeIsNotEmpty()
153153

154154
$this->assertTrue((bool) $bag->isNotEmpty());
155155
}
156+
157+
public function testAttributeIsArray()
158+
{
159+
$bag = new ComponentAttributeBag([
160+
'name' => 'test',
161+
'class' => 'font-bold',
162+
]);
163+
164+
$this->assertIsArray($bag->toArray());
165+
$this->assertEquals(['name' => 'test', 'class' => 'font-bold'], $bag->toArray());
166+
}
156167
}

0 commit comments

Comments
 (0)