Skip to content

Commit

Permalink
consider getter priorities w/ dirty attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
nateiler committed Jan 11, 2019
1 parent 8332ba4 commit 43c6652
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/records/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function rules()
}

/*******************************************
* MAGIC
* ATTRIBUTES
*******************************************/

/**
Expand All @@ -214,13 +214,37 @@ public function rules()
*/
public function __get($name)
{
$getter = 'get' . $name;
if (in_array($name, $this->getterPriorityAttributes, true) &&
method_exists($this, $getter)
) {
return $this->$getter();
if ($this->hasGetterPriority($name)) {
$this->{'get' . $name}();
}

return parent::__get($name);
}

/**
* @inheritdoc
*/
public function getDirtyAttributes($names = null)
{
$attributes = $names ?: $this->getterPriorityAttributes;

// Call each attribute to see if the 'getter' has a value
foreach($attributes as $attribute) {
if ($this->hasGetterPriority($attribute)) {
$this->{'get' . $attribute}();
}
}

return parent::getDirtyAttributes($names);
}

/**
* @param $name
* @return bool
*/
protected function hasGetterPriority($name)
{
return in_array($name, $this->getterPriorityAttributes, true) &&
method_exists($this, 'get' . $name);
}
}

0 comments on commit 43c6652

Please sign in to comment.