tzkmx
4/23/2020 - 1:44 AM

Value Objects

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
            );
        });
    }
}