mqlo/nameable

My first package

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:59:13 UTC


README

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

Only the value of the name field will be stored in the table, and the value of the label will be in the class. This can be used to define the name of the status, role, etc.

Example #1

PostStatus class

    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->value === self::DRAFT;
        }
        
        public function isPublished(): bool
        {
            return $this->value === self::PUBLISHED;
        }
    }

Used

    $postStatus = new PostStatus(PostStatus::draft);
    // or
    $postStatus = PostStatus::draft();

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

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

Example tables when this package can be applied (token type and post status)

  • 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
... ... ...