Skip to content

Commit 7c7884a

Browse files
committed
Component::fill
1 parent ef555ee commit 7c7884a

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/Discord/Builders/Components/Component.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ abstract class Component implements JsonSerializable
6565
self::TYPE_CONTAINER => Container::class
6666
];
6767

68+
69+
/**
70+
* Fills the properties of the current object with values from the provided associative array.
71+
*
72+
* Iterates over each key-value pair in the input array and assigns the value to the corresponding
73+
* property of the object if the property exists.
74+
*
75+
* @param array $data
76+
*
77+
* @return void
78+
*/
79+
public function fill(array $data): void
80+
{
81+
foreach ($data as $key => $value) {
82+
if (property_exists($this, $key)) {
83+
$this->{$key} = $value;
84+
}
85+
}
86+
}
87+
6888
/**
6989
* Generates a UUID which can be used for component custom IDs.
7090
*

src/Discord/Parts/Interactions/Request/Component.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class Component extends Part
7272
/**
7373
* Gets the sub-components of the component.
7474
*
75-
* @return ExCollectionInterface|Component[]|null $components
75+
* @return ExCollectionInterface|ComponentBuilder[]|null $components
7676
*/
7777
protected function getComponentsAttribute(): ?ExCollectionInterface
7878
{
@@ -91,17 +91,15 @@ protected function getComponentsAttribute(): ?ExCollectionInterface
9191
return null;
9292
}
9393

94-
$components = Collection::for(Component::class, null);
94+
$components = Collection::for(ComponentBuilder::class, null);
9595

9696
foreach ($this->attributes['components'] ?? [] as $component) {
9797
$componentType = (is_object($component)) ? (isset($component->type) ? $component->type : 0) : ($component['type'] ?? 0);
9898
$components->pushItem(
99-
$this->createOf(
100-
isset(ComponentBuilder::TYPE_CLASSES[$componentType])
101-
? ComponentBuilder::TYPE_CLASSES[$componentType]
102-
: ComponentBuilder::class,
103-
$component
104-
)
99+
isset(ComponentBuilder::TYPE_CLASSES[$componentType])
100+
? (new (ComponentBuilder::TYPE_CLASSES[$componentType]))->fill((array) $component)
101+
: (new ComponentBuilder())->fill((array) $component),
102+
$component
105103
);
106104
}
107105

0 commit comments

Comments
 (0)