Skip to content

Commit

Permalink
Allow adding purgeable attributes on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed Oct 17, 2016
1 parent eaa29fe commit c420ed0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/Database/Traits/Hashable.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ public static function bootHashable()

/**
* Adds an attribute to the hashable attributes list
* @param string $attribute Attribute
* @param array|string|null $attributes
* @return $this
*/
public function addHashableAttribute($attribute)
public function addHashableAttribute($attributes = null)
{
if (in_array($attribute, $this->hashable)) return;
$attributes = is_array($attributes) ? $attributes : func_get_args();

$this->hashable = array_merge($this->hashable, $attributes);

$this->hashable[] = $attribute;
return $this;
}

Expand Down
28 changes: 23 additions & 5 deletions src/Database/Traits/Purgeable.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,44 @@ public static function bootPurgeable()
});
}

/**
* Adds an attribute to the purgeable attributes list
* @param array|string|null $attributes
* @return $this
*/
public function addPurgeableAttribute($attributes = null)
{
$attributes = is_array($attributes) ? $attributes : func_get_args();

$this->purgeable = array_merge($this->purgeable, $attributes);

return $this;
}

/**
* Removes purged attributes from the dataset, used before saving.
* @param $attributes mixed Attribute(s) to purge, if unspecified, $purgable property is used
* @return array Current attribute set
*/
public function purgeAttributes($attributesToPurge = null)
{
if ($attributesToPurge !== null)
if ($attributesToPurge !== null) {
$purgeable = is_array($attributesToPurge) ? $attributesToPurge : [$attributesToPurge];
else
}
else {
$purgeable = $this->getPurgeableAttributes();
}

$attributes = $this->getAttributes();
$cleanAttributes = array_diff_key($attributes, array_flip($purgeable));
$originalAttributes = array_diff_key($attributes, $cleanAttributes);

if (is_array($this->originalPurgeableValues))
if (is_array($this->originalPurgeableValues)) {
$this->originalPurgeableValues = array_merge($this->originalPurgeableValues, $originalAttributes);
else
}
else {
$this->originalPurgeableValues = $originalAttributes;
}

return $this->attributes = $cleanAttributes;
}
Expand Down Expand Up @@ -94,4 +112,4 @@ public function restorePurgedValues()
$this->attributes = array_merge($this->getAttributes(), $this->originalPurgeableValues);
return $this;
}
}
}

0 comments on commit c420ed0

Please sign in to comment.