-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
136 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php namespace RainLab\Translate\Behaviors; | ||
|
||
/** | ||
* Purgeable | ||
*/ | ||
class Purgeable extends \October\Rain\Extension\ExtensionBase | ||
{ | ||
/** | ||
* @var array List of attribute names which should not be saved to the database. | ||
* | ||
* public $purgeable = []; | ||
*/ | ||
|
||
protected $model; | ||
|
||
protected static $purgableShownDeprecation = false; | ||
|
||
public function __construct($parent) | ||
{ | ||
$this->model = $parent; | ||
$this->bootPurgeable(); | ||
} | ||
|
||
/** | ||
* @var array List of original attribute values before they were purged. | ||
*/ | ||
protected $originalPurgeableValues = []; | ||
|
||
/** | ||
* Boot the purgeable trait for a model. | ||
* @return void | ||
*/ | ||
public function bootPurgeable() | ||
{ | ||
if (!$this->model->propertyExists('purgeable')) { | ||
$this->model->addDynamicProperty('purgeable', []); | ||
} | ||
|
||
$this->model->purgeable = array_merge($this->model->purgeable, ['purgeable']); | ||
$dynPropNames = array_keys(array_diff_key($this->model->getDynamicProperties(), ['purgeable' => 0])); | ||
$this->model->purgeable = array_merge($this->model->purgeable, $dynPropNames); | ||
|
||
/* | ||
* Remove any purge attributes from the data set | ||
*/ | ||
$model = $this->model; | ||
$model->bindEvent('model.saveInternal', function () use ($model) { | ||
$model->purgeAttributes(); | ||
}); | ||
|
||
if (!static::$purgableShownDeprecation) { | ||
traceLog('Class October\Rain\Database\Behaviors\Purgeable is deprecated. If you require this class, please copy it to your codebase locally.'); | ||
static::$purgableShownDeprecation = true; | ||
} | ||
} | ||
|
||
/** | ||
* Adds an attribute to the purgeable attributes list | ||
* @param array|string|null $attributes | ||
* @return $this | ||
*/ | ||
public function addPurgeable($attributes = null) | ||
{ | ||
$attributes = is_array($attributes) ? $attributes : func_get_args(); | ||
|
||
$this->model->purgeable = array_merge($this->model->purgeable, $attributes); | ||
|
||
return $this->model; | ||
} | ||
|
||
/** | ||
* 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) { | ||
$purgeable = is_array($attributesToPurge) ? $attributesToPurge : [$attributesToPurge]; | ||
} | ||
else { | ||
$purgeable = $this->getPurgeableAttributes(); | ||
} | ||
|
||
$attributes = $this->model->getAttributes(); | ||
$cleanAttributes = array_diff_key($attributes, array_flip($purgeable)); | ||
$originalAttributes = array_diff_key($attributes, $cleanAttributes); | ||
|
||
if (is_array($this->originalPurgeableValues)) { | ||
$this->originalPurgeableValues = array_merge($this->originalPurgeableValues, $originalAttributes); | ||
} | ||
else { | ||
$this->originalPurgeableValues = $originalAttributes; | ||
} | ||
|
||
return $this->model->attributes = $cleanAttributes; | ||
} | ||
|
||
/** | ||
* Returns a collection of fields that will be hashed. | ||
*/ | ||
public function getPurgeableAttributes() | ||
{ | ||
return $this->model->purgeable; | ||
} | ||
|
||
/** | ||
* Returns the original values of any purged attributes. | ||
*/ | ||
public function getOriginalPurgeValues() | ||
{ | ||
return $this->originalPurgeableValues; | ||
} | ||
|
||
/** | ||
* Returns the original values of any purged attributes. | ||
*/ | ||
public function getOriginalPurgeValue($attribute) | ||
{ | ||
return $this->originalPurgeableValues[$attribute] ?? null; | ||
} | ||
|
||
/** | ||
* Restores the original values of any purged attributes. | ||
*/ | ||
public function restorePurgedValues() | ||
{ | ||
$this->model->attributes = array_merge($this->model->getAttributes(), $this->originalPurgeableValues); | ||
return $this->model; | ||
} | ||
} |