Last active
May 4, 2016 17:19
-
-
Save pionl/32beaaa9331a7f95698c to your computer and use it in GitHub Desktop.
Revisions
-
Martin Kluska revised this gist
May 4, 2016 . 1 changed file with 22 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,5 @@ <?php namespace Pion\Traits\Models; /** * Class NullEmptyStringAttributeTrait @@ -18,22 +17,31 @@ * ]; * } */ trait NullEmptyStringAttributeTrait { /** * Enables settings specified collumns * @var array|null */ protected $nullEmptyCollumns = null; /** * Enables to set specified columns that should be ignored in seting null * @var array|null */ protected $nullIgnoreColumns = null; /** * Set a given attribute on the model. * * @param string $key * @param mixed $value * * @return void */ public function setAttribute($key, $value) { // should we null the attribute if ($this->canNullAttributeValue($key, $value)) { @@ -49,15 +57,22 @@ public function setAttribute($key, $value) { * be nulled on empty string * * @param string $key * @param mixed $value * * @return bool */ public function canNullAttributeValue($key, $value) { // filter collumns (optional setting) if (is_array($this->nullEmptyCollumns) && !in_array($key, $this->nullEmptyCollumns)) { return false; } // filter attributes that are in ignore if (is_array($this->nullIgnoreColumns) && in_array($key, $this->nullIgnoreColumns)) { return false; } // check if the value is string and the trimed value is empty // first check without trim for non needed trim call return is_string($value) && ($value === "" || trim($value) === ""); -
Martin Kluska revised this gist
Dec 23, 2015 . No changes.There are no files selected for viewing
-
Martin Kluska revised this gist
Oct 21, 2015 . No changes.There are no files selected for viewing
-
Martin Kluska created this gist
Oct 21, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,65 @@ <?php namespace Pion\Traits\Models; use Illuminate\Support\Arr; /** * Class NullEmptyStringAttributeTrait * * Enables the automatic nulling of empty string value. You can provide * list of collumns keys to allow only specified collumns to be nulled. * * Usage for setting nullEmptyCollumns: * * public function __construct(array $attributes = []) { * parent::__construct($attributes); * * $this->nullEmptyCollumns = [ * "name" * ]; * } */ trait NullEmptyStringAttributeTrait { /** * Enables settings specified collumns * @var array|null */ protected $nullEmptyCollumns = null; /** * Set a given attribute on the model. * * @param string $key * @param mixed $value * @return void */ public function setAttribute($key, $value) { // should we null the attribute if ($this->canNullAttributeValue($key, $value)) { $value = null; } // call the parent attribute seting parent::setAttribute($key, $value); } /** * Checks the given attribute with value, detects if the value should * be nulled on empty string * * @param string $key * @param mixed $value * @return bool */ public function canNullAttributeValue($key, $value) { // filter collumns (optional setting) if (is_array($this->nullEmptyCollumns) && !in_array($key, $this->nullEmptyCollumns)) { return false; } // check if the value is string and the trimed value is empty // first check without trim for non needed trim call return is_string($value) && ($value === "" || trim($value) === ""); } }