chippyash / identity
类身份管理
1.1.0
2018-07-04 22:34 UTC
Requires
- php: >=5.6
- chippyash/strong-type: >=5,<6
Requires (Dev)
- phpunit/phpunit: >=5.7.9,<6
README
质量保证
查看测试合约
是什么?
提供简单的辅助功能,用于类身份管理
为什么?
这是那种你反复编写的代码之一。许多应用程序需要它们使用的类有一些形式的身份。通常在数据驱动应用程序中,这相当于主键字段 id
。这个库提供了对这个的支持。
路线图
如果你需要更多,可以建议,或者更好的是,分支它并提交一个pull request。
查看ZF4 Packages 获取更多包
如何
编码基础
代码以接口和特性提供。只需将你的类声明为实现 Identifiable
接口,然后在类中使用 Identifying
特性来提供功能。
身份基于 Chippyash\Type\Interfaces\TypeInterface。这允许强类型和执行身份规则。例如,这里是一个固定长度数字字符串的产品id的例子。
use Chippyash\Type\String\DigitType; /** * A unique identifier */ class Identifier extends DigitType { /** * Length of product id, it will be front padded with zeros to this length * or cut to this length * * @var int */ protected $length = 10; /** * @return int */ public function getLength() { return $this->length; } /** * @param mixed $value * * @return string */ protected function typeOf($value) { $v = parent::typeOf($value); if (strlen($v) > $this->length) { return substr($v, -$this->length); } return str_pad($v, $this->length, '0',STR_PAD_LEFT); } }
你的类然后可以简单地这样使用
use Chippyash\Identity\Identifiable; use Chippyash\Identity\Identifying; class Product implements Identifiable { use Identifying; public function __construct(Identifier $id) { $this->id = $id; //or maybe there is some other way of establishing the identity } } $product = new Product(new Identifier(53)); $id = $product->id(); // returns Identifier $vId = $product->vId(); //returns '0000000053' $vId = $product->id()->get(); //returns '0000000053'
更改库
- 分支它
- 编写测试
- 修改它
- 提交pull request
发现了无法解决的问题?
- 分支它
- 编写测试
- 提交pull request
注意。在pull request之前,确保你rebase到HEAD
或者 - 提交一个问题票证。
在哪里?
库托管在 Github。它在 Packagist.org 上可用
安装
安装 Composer
生产环境
"chippyash/identity": ">=1,<2"
开发环境
克隆此仓库,然后在本地仓库根目录中运行Composer以拉取依赖项
git clone git@github.com:chippyash/identity.git cd identity composer update
运行测试
cd identity vendor/bin/phpunit -c test/phpunit.xml test/
许可证
此软件库根据 BSD 3 Clause license 发布
此软件库版权所有 (c) 2015,Ashley Kitson,英国
历史
V1.0.0 原始发布
V1.0.1 构建运行更新
V1.0.2 文档更新
V1.0.3 composer.json 修复
V1.0.4 依赖更新
V1.1.0 许可证从GPL V3更改为BSD 3 Clause