Skip to content

Commit

Permalink
Fix Property Acessors Trait
Browse files Browse the repository at this point in the history
  • Loading branch information
gpupo committed Apr 10, 2018
1 parent 20475bb commit 665fb3d
Showing 1 changed file with 6 additions and 22 deletions.
28 changes: 6 additions & 22 deletions src/Traits/PropertyAccessorsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,23 @@

trait PropertyAccessorsTrait
{
private function __accessorPropertyExists($property)
{
return property_exists(self::class, $property);
}

private function __accessorPropertyGetter($property, callable $exception)
{
if ($this->__accessorPropertyExists($property)) {
return $this->{$property};
}

$exception();
}

private function __accessorPropertyException($method, $property = null)
{
if (empty($property) || !$this->__accessorPropertyExists($property)) {
throw new \BadMethodCallException('There is no [magic] method '.$method.'()');
if (empty($property) || !property_exists(get_called_class(), $property)) {
throw new \BadMethodCallException('There is no [magic] method '.$method.'() ['.$property.']');
}
}

public function __get($property)
{
if ($this->__accessorPropertyExists($property)) {
return $this->{$property};
}
$this->__accessorPropertyException('__get', $property);

return $this->{$property};
}

public function __set($property, $value)
{
$this->__accessorPropertyException('__set', $property);
$this->{$property} = $value;

return true;
Expand All @@ -71,18 +58,15 @@ public function __call($method, $args)
$property[0] = strtolower($property[0]);

if ('set' === $command) {
$this->__accessorPropertyException($method, $property);

return $this->__set($property, current($args));
}
if ('has' === $command) {
$this->__accessorPropertyException($method, $property);
$value = $this->__get($property);

return !empty($value);
}
if ('get' === $command) {
$this->__accessorPropertyException($method, $property);

return $this->__get($property);
}
Expand Down

0 comments on commit 665fb3d

Please sign in to comment.