Skip to content

[12.x] Introducing toArray to ComponentAttributeBag class #55258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Illuminate/View/ComponentAttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ArrayAccess;
use ArrayIterator;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand All @@ -16,7 +17,7 @@
use Stringable;
use Traversable;

class ComponentAttributeBag implements ArrayAccess, IteratorAggregate, JsonSerializable, Htmlable, Stringable
class ComponentAttributeBag implements Arrayable, ArrayAccess, IteratorAggregate, JsonSerializable, Htmlable, Stringable
{
use Conditionable, Macroable;

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

/**
* Get all of the attribute values.
* Get all the attribute values.
*
* @return array
*/
Expand Down Expand Up @@ -497,6 +498,16 @@ public function jsonSerialize(): mixed
return $this->attributes;
}

/**
* Get all the attribute values.
*
* @return array
*/
public function toArray()
{
return $this->all();
}

/**
* Implode the attributes into a single HTML ready string.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/View/ViewComponentAttributeBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,15 @@ public function testAttributeIsNotEmpty()

$this->assertTrue((bool) $bag->isNotEmpty());
}

public function testAttributeIsArray()
{
$bag = new ComponentAttributeBag([
'name' => 'test',
'class' => 'font-bold',
]);

$this->assertIsArray($bag->toArray());
$this->assertEquals(['name' => 'test', 'class' => 'font-bold'], $bag->toArray());
}
}
Loading