Skip to content

Instantly share code, notes, and snippets.

@tzkmx
Created April 23, 2020 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzkmx/8791697d778a4a586483841d4bc76db3 to your computer and use it in GitHub Desktop.
Save tzkmx/8791697d778a4a586483841d4bc76db3 to your computer and use it in GitHub Desktop.
Value Objects
<?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);
}
}
<?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
);
});
}
}
@tzkmx
Copy link
Author

tzkmx commented Apr 23, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment