mqlo/nameable

我的第一个包

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().

只有名称字段的值将存储在表中,标签的值在类中。这可以用来定义状态名称、角色等。

示例 #1

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

使用

    $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

此包可应用的示例表(令牌类型和帖子状态)

  • 令牌
id 类型 过期
1 电子邮件确认 2020-10-01 19:00:00
2 密码重置 2020-10-01 19:30:00
... ... ...
  • 帖子
id 标题 状态
1 帖子 1 草稿
2 帖子 2 已发布
... ... ...