Skip to content

Enhance Attribute and Property Handling for Parts #1320

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
79 changes: 77 additions & 2 deletions src/Discord/Parts/PartTrait.php
Original file line number Diff line number Diff line change
@@ -113,6 +113,24 @@ private function checkForGetMutator(string $key)
return false;
}

/**
* Checks if a 'get' method exists for the given key.
*
* @param string $key The property name to check.
*
* @return string|false Either a string if it is a method or false.
*/
private function checkForGetMethod(string $key): string|false
{
$str = 'get'.self::studly($key);

if (method_exists($this, $str)) {
return $str;
}

return false;
}

/**
* Checks if there is a set mutator present.
*
@@ -133,6 +151,24 @@ private function checkForSetMutator(string $key)
return false;
}

/**
* Checks if a 'set' method exists for the given key.
*
* @param string $key The property name to check.
*
* @return string|false Either a string if it is a method or false.
*/
private function checkForSetMethod(string $key): string|false
{
$str = 'set'.self::studly($key);

if (method_exists($this, $str)) {
return $str;
}

return false;
}

/**
* Gets an attribute on the part.
*
@@ -162,6 +198,21 @@ private function getAttribute(string $key)
return $this->attributes[$key];
}

/**
* Gets a property on the part.
*
* @param string $key The name of the property.
*
* @return mixed Either the attribute if it exists or void.
* @throws \Exception
*/
private function getProperty(string $key)
{
if ($str = $this->checkForGetMethod($key)) {
return $this->{$str}();
}
}

/**
* Sets an attribute on the part.
*
@@ -181,6 +232,30 @@ private function setAttribute(string $key, $value): void
}
}

/**
* Sets an attribute or property on the party.
*
* @param string $key The name to the attribute or property.
* @param mixed $value The value of the attribute or property.
*/
private function setAttributeOrProperty(string $key, $value): void
{
if ($str = $this->checkForSetMutator($key)) {
$this->{$str}($value);

return;
}

if (in_array($key, $this->fillable)) {
$this->attributes[$key] = $value;
return;
}

if ($str = $this->checkForSetMethod($key)) {
$this->{$str}($value);
}
}

/**
* Gets an attribute via key. Used for ArrayAccess.
*
@@ -476,7 +551,7 @@ public function __debugInfo(): array
*/
public function __get(string $key)
{
return $this->getAttribute($key);
return $this->getAttribute($key) ?? $this->getProperty($key);
}

/**
@@ -489,6 +564,6 @@ public function __get(string $key)
*/
public function __set(string $key, $value): void
{
$this->setAttribute($key, $value);
$this->setAttributeOrProperty($key, $value);
}
}