|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Larapack\AttributePurging; |
| 4 | + |
| 5 | +use Exception; |
| 6 | + |
| 7 | +trait Purgeable |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @var array List of attribute names which should not be saved to the database. |
| 11 | + * |
| 12 | + * protected $purge = []; |
| 13 | + */ |
| 14 | + |
| 15 | + /** |
| 16 | + * @var array List of original attribute values before they were purged. |
| 17 | + */ |
| 18 | + protected $originalPurgeableValues = []; |
| 19 | + |
| 20 | + /** |
| 21 | + * Boot the purgeable trait for a model. |
| 22 | + * @return void |
| 23 | + */ |
| 24 | + public static function bootPurgeable() |
| 25 | + { |
| 26 | + /* |
| 27 | + * Remove any purge attributes from the data set |
| 28 | + */ |
| 29 | + static::creating(function($model){ |
| 30 | + $model->purgeAttributes(); |
| 31 | + }); |
| 32 | + |
| 33 | + static::updating(function($model){ |
| 34 | + $model->purgeAttributes(); |
| 35 | + }); |
| 36 | + |
| 37 | + static::created(function($model){ |
| 38 | + $model->restorePurgedValues(); |
| 39 | + }); |
| 40 | + |
| 41 | + static::updated(function($model){ |
| 42 | + $model->restorePurgedValues(); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Removes purged attributes from the dataset, used before saving. |
| 48 | + * @param $attributes mixed Attribute(s) to purge, if unspecified, $purgable property is used |
| 49 | + * @return array Current attribute set |
| 50 | + */ |
| 51 | + public function purgeAttributes($attributesToPurge = null) |
| 52 | + { |
| 53 | + if ($attributesToPurge !== null) |
| 54 | + $purgeable = is_array($attributesToPurge) ? $attributesToPurge : [$attributesToPurge]; |
| 55 | + else |
| 56 | + $purgeable = $this->getPurgeableAttributes(); |
| 57 | + |
| 58 | + $attributes = $this->getAttributes(); |
| 59 | + |
| 60 | + $cleanAttributes = array_diff_key($attributes, array_flip($purgeable)); |
| 61 | + |
| 62 | + $originalAttributes = array_diff_key($attributes, $cleanAttributes); |
| 63 | + |
| 64 | + if (is_array($this->originalPurgeableValues)) |
| 65 | + $this->originalPurgeableValues = array_merge($this->originalPurgeableValues, $originalAttributes); |
| 66 | + else |
| 67 | + $this->originalPurgeableValues = $originalAttributes; |
| 68 | + |
| 69 | + return $this->attributes = $cleanAttributes; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns a collection of fields that will be hashed. |
| 74 | + */ |
| 75 | + public function getPurgeableAttributes() |
| 76 | + { |
| 77 | + if (property_exists(get_called_class(), 'purge')) |
| 78 | + { |
| 79 | + return $this->purge; |
| 80 | + } |
| 81 | + |
| 82 | + return []; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Returns the original values of any purged attributes. |
| 87 | + */ |
| 88 | + public function getOriginalPurgeValues() |
| 89 | + { |
| 90 | + return $this->originalPurgeableValues; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns the original values of any purged attributes. |
| 95 | + */ |
| 96 | + public function getOriginalPurgeValue($attribute) |
| 97 | + { |
| 98 | + return isset($this->originalPurgeableValues[$attribute]) |
| 99 | + ? $this->originalPurgeableValues[$attribute] |
| 100 | + : null; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Restores the original values of any purged attributes. |
| 105 | + */ |
| 106 | + public function restorePurgedValues() |
| 107 | + { |
| 108 | + $this->attributes = array_merge($this->getAttributes(), $this->originalPurgeableValues); |
| 109 | + return $this; |
| 110 | + } |
| 111 | +} |
0 commit comments