Created
April 23, 2020 01:44
-
-
Save tzkmx/8791697d778a4a586483841d4bc76db3 to your computer and use it in GitHub Desktop.
Value Objects
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
<?php | |
namespace App\Generics; | |
class NamedValue | |
{ | |
protected $id; | |
protected $name; | |
public function __construct(int $id, string $name) | |
{ | |
$this->id = $id; | |
$this->name = $name; | |
} | |
public function getId(): int | |
{ | |
return $this->id; | |
} | |
public function getName(): string | |
{ | |
return $this->name; | |
} | |
public function fmap(callable $fn): self | |
{ | |
return call_user_func($fn, $this); | |
} | |
} |
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
<?php | |
namespace App\Model\Observer; | |
trait LegacyStaBajaFlag | |
{ | |
protected static $staBajaFieldName = 'sta_baja'; | |
protected static $staBajaNoBaja = 'N'; | |
protected static $staBajaSiBaja = 'S'; | |
/** @var \Closure */ | |
protected static $bajaAttributeSetter; | |
public static function bootLegacyStaBajaFlag() | |
{ | |
self::$bajaAttributeSetter = function ($flag) { | |
$query = $this->setKeysForSaveQuery($this->newModelQuery()); | |
$columns = [self::$staBajaFieldName => $flag]; | |
$this->timestamps = false; | |
$query->update($columns); | |
$this->timestamps = true; | |
$this->syncOriginalAttribute(self::$staBajaFieldName); | |
}; | |
static::deleting(function ($model) { | |
call_user_func( | |
self::$bajaAttributeSetter->bindTo($model, static::class), | |
static::$staBajaSiBaja | |
); | |
}); | |
static::restoring(function ($model) { | |
call_user_func( | |
self::$bajaAttributeSetter->bindTo($model, static::class), | |
static::$staBajaNoBaja | |
); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sería interesante hacer un trait que más bien pueda agregar diferentes atributos a la actualización previa a finalizar un evento de Eloquent, que cerrarlo a uno específico