mqlo/nameable-cast

This package works best with the mqlo/nameable package for Laravel, Lumen

This package's canonical repository appears to be gone and the package has been frozen as a result.

v2.5 2021-02-19 17:04 UTC

This package is auto-updated.

Last update: 2021-10-06 18:33:51 UTC


README

  • $nameable->name(), $nameable->value();
  • 'name' => $this->name(), 'value' => $this->value().

This package works best with the mqlo/nameable package for Laravel, Lumen.

Example #1

Post

    /**
     * @property PostStatus $status
    */
    class Post extends Model
    {
        protected $casts = [
            'status' => PostStatusCast::class,
        ];
    ...
    }

PostStatus

    use Mqlo\Nameable\Nameable;

    class PostStatus extends Nameable
    {
        public const DRAFT = 'draft';
        public const PUBLISHED = 'published';

        protected static array $all = [
            self::DRAFT => 'Draft',
            self::PUBLISHED => 'Published',
        ];

        public static function draft(): self
        {
            return new self(self::DRAFT);
        }

        public static function published(): self
        {
            return new self(self::PUBLISHED);
        }

        public function isDraft(): bool
        {
            return $this->name === self::DRAFT;
        }
        
        public function isPublished(): bool
        {
            return $this->name === self::PUBLISHED;
        }
    }

PostStatusCast

    use Mqlo\NameableCast\NameableCast;

    class PostStatusCast extends NameableCast
    {
        protected function nameableClass(): string
        {
            return PostStatus::class;
        }
    }

Post create

    $post = new Post();
    $post->status = PostStatus::draft();
    $post->save();

    //or
    
    $post = Post::create(['status' => PostStatus::DRAFT]);

    //validation
    $this->validate($request, [
        'title' => 'required|string|max:255',
        'status' => 'required|in:' . implode(',', PostStatus::all(false))
    ]);

Using

    echo $post->status->isDraft() ? 'This post draft.' : '...'; 

    PostStatus::all(true); // with descriptions
    PostStatus::all(false); // without descriptions

    return $post;
        
    [
        ...,
        'status' => [
            'name' => 'draft',
            'label' => 'Draft'
        ]
    ]

Example tables

  • tokens
id type expire
1 email_confirmation 2020-10-01 19:00:00
2 password_reset 2020-10-01 19:30:00
... ... ...
  • posts
id title status
1 Post 1 draft
2 Post 2 published
... ... ...