chippyash/attributes

单子属性映射

1.1.1 2019-02-22 10:45 UTC

This package is auto-updated.

Last update: 2024-09-22 22:53:46 UTC


README

质量保证

PHP 5.6 PHP 7 Build Status Test Coverage Code Climate

上述徽章代表当前的开发分支。通常情况下,除非测试、覆盖率和使用性都是可接受的,否则我不会向GitHub推送代码。在假期期间,需要为其他下游项目编写代码等情况下,这可能不成立。如果您需要稳定的代码,请使用标记版本。请阅读“进一步文档”和“安装”。

是什么?

提供基于 单子 原则的非常强类型但简单的通用属性容器。

为什么?

您有多少次编写了一个具有大量受保护参数(属性)的类,然后不得不编写所有获取器和设置器?这个库提供了一个用于这些属性的通用容器。此外,属性容器和属性都是单子且不可变的,这意味着您可以保证它们的状态。

如何

使用属性特征。

use Chippyash\Attributes\Attribution;

class MyClass
{
	use Attribution;
}

您的类现在有2个方法

public function getA(StringType $name): Attribute

public function hasA(StringType $name): bool

来检索和测试属性

您可以使用属性可设置特征来添加一个设置器。注意:您需要同时使用这两个特征,因为属性提供了受保护的$attributes变量给类;

use Chippyash\Attributes\Attribution;
use Chippyash\Attributes\AttributionSettable;

class MyClass
{
	use Attribution;
	use AttributionSettable;
}

这添加了

public function setA(StringType $name, Attribute $attribute): Attribution

	use Chippyash\Attributes\Attribute;
	use Chippyash\Type\String\StringType;
	
	$myClass = new MyClass();
	$attr = new Attribute('bar');
	$attrName = new StringType('foo);
	$test = $myClass->setA($attrName, $attr)->getA($attrName);
	
	echo ($myClass->hasA($attrName) ? 'true' : 'false');
	echo ($myClass->hasA(new StringType('bar)) ? 'true' : 'false');
	

属性,作为单子,有一个值可以检索,如下所示

	$attrValue = $myClass->getA($attrName)->value();
	//or
	$attr = $myClass->getA($attrName);
	$attrValue = $attr();  //using the invokable interface

如果需要,您可以在使用特征时定义类以实现Attributal接口,以便与其他对象通信,属性可以测试和检索。您可以使用AttributalSettable接口来表示类允许设置属性(当然,这将破坏不可变接口,因此您可能需要考虑在属性映射上保留状态历史);

use Chippyash\Attributes\Attribution;
use Chippyash\Attributes\AttributionSettable;
use Chippyash\Attributes\Attributal;
use Chippyash\Attributes\AttributalSettable;

class MyClass implements Attributal, AttributalSettable
{
	use Attribution;
	use AttributionSettable;
}

进一步文档

更多关于 单子这里

测试合同 在文档目录中。

查看 ZF4 包 获取更多包

修改库

  1. 进行分支
  2. 编写测试
  3. 修改
  4. 发起合并请求

找到了您无法解决的问题?

  1. 进行分支
  2. 编写测试
  3. 发起合并请求

注意:在发起合并请求之前,请确保您已经rebase到HEAD

或者 - 提交一个问题票。

在哪里?

该库托管在 Github 上。它在 Packagist.org 上可用。

安装

安装 Composer

对于生产环境

    "chippyash/attributes": ">=1,<2"

或者使用最新版本,可能是不稳定的版本

    "chippyash/attributes": "dev-master"

对于开发环境

克隆此存储库,然后在本地存储库根目录中运行Composer以拉取依赖项

    git clone git@github.com:chippyash/attributes.git Attributes
    cd Attributes
    composer install

来运行测试

    cd Attributes
    vendor/bin/phpunit -c test/phpunit.xml test/

许可证

此软件库在 BSD 3 Clause 许可证 下发布

此软件库的版权为(c)2017,Ashley Kitson,英国

历史记录

V1.0.0 初次发布

V1.0.1 更新依赖项

V1.1.0 许可证从GPL V3更改为BSD 3 Clause