Skip to content

Instantly share code, notes, and snippets.

@pionl
Last active May 4, 2016 17:19
Show Gist options
  • Save pionl/32beaaa9331a7f95698c to your computer and use it in GitHub Desktop.
Save pionl/32beaaa9331a7f95698c to your computer and use it in GitHub Desktop.

Revisions

  1. Martin Kluska revised this gist May 4, 2016. 1 changed file with 22 additions and 7 deletions.
    29 changes: 22 additions & 7 deletions NullEmptyStringAttributeTrait.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    <?php
    namespace Pion\Traits\Models;
    use Illuminate\Support\Arr;

    /**
    * Class NullEmptyStringAttributeTrait
    @@ -18,22 +17,31 @@
    * ];
    * }
    */
    trait NullEmptyStringAttributeTrait {
    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
    * @param string $key
    * @param mixed $value
    *
    * @return void
    */
    public function setAttribute($key, $value) {
    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
    * @param mixed $value
    *
    * @return bool
    */
    public function canNullAttributeValue($key, $value) {
    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) === "");
  2. Martin Kluska revised this gist Dec 23, 2015. No changes.
  3. Martin Kluska revised this gist Oct 21, 2015. No changes.
  4. Martin Kluska created this gist Oct 21, 2015.
    65 changes: 65 additions & 0 deletions NullEmptyStringAttributeTrait.php
    Original 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) === "");
    }
    }