Skip to content

Commit

Permalink
Move guild common repository fill() to set<Attribute>Attribute methods (
Browse files Browse the repository at this point in the history
#996)

* Make Guild fillables roles, emojis, stickers override the attribute mutator

* move repo fillable to visible

* move todo note
  • Loading branch information
SQKo authored Nov 28, 2022
1 parent d27c126 commit 5764bd6
Showing 1 changed file with 51 additions and 16 deletions.
67 changes: 51 additions & 16 deletions src/Discord/Parts/Guild/Guild.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ class Guild extends Part
'splash',
'discovery_splash',
'features',
'emojis',
'banner',
'owner_id',
'application_id',
Expand All @@ -203,7 +202,6 @@ class Guild extends Part
'widget_enabled',
'widget_channel_id',
'verification_level',
'roles',
'default_message_notifications',
'hub_type',
'mfa_level',
Expand All @@ -223,7 +221,6 @@ class Guild extends Part
'approximate_presence_count',
'welcome_screen',
'nsfw_level',
'stickers',
'premium_progress_bar_enabled',

// events
Expand Down Expand Up @@ -262,6 +259,9 @@ class Guild extends Part
'feature_verified',
'feature_vip_regions',
'feature_welcome_screen_enabled',
'emojis',
'roles',
'stickers',
];

/**
Expand Down Expand Up @@ -293,14 +293,40 @@ class Guild extends Part

/**
* @inheritDoc
*
* @todo move each repository fill to the set<Attribute>Attributes methods
*/
public function fill(array $attributes): void
{
parent::fill($attributes);

foreach ($attributes['roles'] ?? [] as $role) {
if (isset($attributes['roles'])) {
$this->setRolesAttribute($attributes['roles']);
}

if (isset($attributes['emojis'])) {
$this->setEmojisAttribute($attributes['emojis']);
}

if (isset($attributes['stickers'])) {
$this->setStickersAttribute($attributes['stickers']);
}

// @todo move each repository fill to the setChannelAttributes methods?
foreach ($attributes['channels'] ?? [] as $channel) {
$channel = (array) $channel;
/** @var Channel */
if ($channelPart = $this->channels->offsetGet($channel['id'])) {
$channelPart->fill($channel);
}
$this->channels->pushItem($channelPart ?: $this->channels->create($channel, true));
}
}

/**
* @inheritDoc
*/
protected function setRolesAttribute(?array $roles): void
{
foreach ($roles ?? [] as $role) {
$role = (array) $role;
/** @var Role */
if ($rolePart = $this->roles->offsetGet($role['id'])) {
Expand All @@ -309,7 +335,15 @@ public function fill(array $attributes): void
$this->roles->pushItem($rolePart ?: $this->roles->create($role, $this->created));
}

foreach ($attributes['emojis'] ?? [] as $emoji) {
$this->attributes['roles'] = $roles;
}

/**
* @inheritDoc
*/
protected function setEmojisAttribute(?array $emojis): void
{
foreach ($emojis ?? [] as $emoji) {
$emoji = (array) $emoji;
/** @var Emoji */
if ($emojiPart = $this->emojis->offsetGet($emoji['id'])) {
Expand All @@ -318,7 +352,15 @@ public function fill(array $attributes): void
$this->emojis->pushItem($emojiPart ?: $this->emojis->create($emoji, $this->created));
}

foreach ($attributes['stickers'] ?? [] as $sticker) {
$this->attributes['emojis'] = $emojis;
}

/**
* @inheritDoc
*/
protected function setStickersAttribute(?array $stickers): void
{
foreach ($stickers ?? [] as $sticker) {
$sticker = (array) $sticker;
/** @var Sticker */
if ($stickerPart = $this->stickers->offsetGet($sticker['id'])) {
Expand All @@ -327,14 +369,7 @@ public function fill(array $attributes): void
$this->stickers->pushItem($stickerPart ?: $this->stickers->create($sticker, $this->created));
}

foreach ($attributes['channels'] ?? [] as $channel) {
$channel = (array) $channel;
/** @var Channel */
if ($channelPart = $this->channels->offsetGet($channel['id'])) {
$channelPart->fill($channel);
}
$this->channels->pushItem($channelPart ?: $this->channels->create($channel, true));
}
$this->attributes['stickers'] = $stickers;
}

/**
Expand Down

0 comments on commit 5764bd6

Please sign in to comment.