rsmike/label

一种简单的组织常量集合和常量→字符串字典的方法。

1.1.2 2020-02-18 12:29 UTC

This package is auto-updated.

Last update: 2024-09-18 22:38:02 UTC


README

一种简单的组织常量集合和常量→字符串字典的方法。

示例标签类

/**
 * An example label class.
 *
 * @method static string|array status($item = [])
 * @method static string|array abbr($item = [])
 */
 
abstract class TestLabels extends rsmike\label\Label
 {
    const INACTIVE = 0;
    const ACTIVE = 1;
    const OTHER = 3;
    
    protected static $status = [
        self::ACTIVE => 'Active',
        self::INACTIVE => 'Inactive',
        self::OTHER => 'Other',
    ];

    protected static $abbr = [
        self::ACTIVE => 'A',
        self::INACTIVE => 'I'
    ];
 }

备注

  • 确保声明 '@method static' PHPDoc 以使自动完成功能正常工作
  • 类应该声明为抽象的,以避免实例化

用法

TestLabels::status(TestLabels::ACTIVE); 返回: 'Active'

TestLabels::abbr(0); 返回: 'I'

TestLabels::status(2); 返回: 2 (直接传递)

TestLabels::status(3); 返回: 'Other'

TestLabels::status(null); 返回: null (直接传递)

TestLabels::status(); 返回: [1=>'Active', 0=>'Inactive'] (完整的选项集。对下拉菜单等很有用)

TestLabels::status([]); 返回: [1=>'Active', 0=>'Inactive'] (与上面相同)

TestLabels::status([0]); 返回: [1, 0] (所有可用键)

TestLabels::status([[0, 3]]); 返回: [0=>'Inactive', 3=>'Other'] (子集)

默认值

可以传递一个额外的参数作为默认值,而不是直接传递

TestLabels::status(2, 'N/A'); 返回: 'N/A' (键未找到)

TestLabels::status(null, 'N/A'); 返回: 'N/A' (总是返回 null 时的默认值)

是/否

YN 方法是内置示例,可以在任何子类中使用 TestLabels::YN(1) 返回: 'Yes'

$YN 是受保护的,可以在子类中重写。

安装

运行以下命令之一:

$ composer require rsmike/label:~1.1

或者将以下内容添加到你的 composer.json 文件的 require 部分:

"rsmike/label": "~1.1"

变更日志

v1.1
  • 子集功能
v1.0
  • YN 现在是内置的
  • label() 方法已删除
  • 回退值功能
v0.3.4
  • 将默认快捷键更改为 [][0]
v0.3.4
  • 数组键快捷方式
v0.3
  • 开发版本

待办事项

  • 测试