morebec/value-objects

此包最新版本(1.4.0)没有可用的许可证信息。

PHP 值对象库

1.4.0 2020-11-09 04:47 UTC

This package is auto-updated.

Last update: 2024-09-09 12:47:58 UTC


README

Morebec 项目使用的 PHP 值对象库

Build Status Coverage Status

值对象是表示简单概念的小对象,其等价性基于其内部属性值,而不是特定的身份。

值对象必须遵守以下契约

  • 它们是不可变的(没有设置器)
  • 它们是自我验证的
  • 它们以清晰的方式表示和描述概念

安装

要将库安装到项目中,请将这些行添加到您的 composer.json 配置文件中

{
    "repositories": [
        {
            "url": "https://github.com/Morebec/ValueObjects.git",
            "type": "git"
        }
    ],
    "require": {
        "morebec/value-objects": "^1.0"
    }
}

使用方法

此库附带了一些预定义的 ValueObject 类,您可以在项目中使用。ValueObject 要么实现 ValueObjectInterface,要么扩展 BasicEnum 类。

使用 ValueObjectInterface 创建自己的值对象

要创建一个,需要实现 ValueObjectInterface 并实现以下两个方法

  • __toString()
  • isEqualTo(ValueObjectInterface $valueObject): bool

以下是一个基本示例

use Assert\Assertion;
use Morebec\ValueObjects\ValueObjectInterface;

/**
 * Age Value Object
 */
final class Age implements ValueObjectInterface
{
    /** @var int age */
    private $age;

    public function __construct(int $age)
    {
        Assertion::min($age, 1);
        $this->age = $age;
    }

    public function __toString()
    {
        return strval($this->age);
    }

    /**
     * Returns the value of this age object
     * @return int
     */
    public function toInt(): int
    {
        return $this->age;
    }

    /**
     * Indicates if this value object is equal to abother value object
     * @param  ValueObjectInterface $valueObject othervalue object to compare to
     * @return boolean                           true if equal otherwise false
     */
    public function isEqualTo(ValueObjectInterface $vo): bool
    {
        return (string)$this === (string)$vo;
    }
}

这样做,我们的类可以按以下方式使用

$age = new Age(24);

// Test Equality
$maturity = new Age(18);
$age->isEqualTo($maturity); // false  
$age == $maturity; // false
$age === '18'; // false

// Test Greater than
$age->toInt() >= 18; // true
$age->toInt() >= $maturity->toInt();

通过扩展 BasicEnum 创建自己的枚举类

要创建一个新枚举,需要扩展 BasicEnum 类。例如,假设我们想要创建一个 CardinalPoint 类。由于有严格的四个方向,我们将创建一个基于枚举的值对象

<?php

use Morebec\ValueObjects\ValueObjectInterface;

/**
 * CardinalPoint
 */
class CardinalPoint implements ValueObjectInterface
{
    const NORTH = 'NORTH';    
    const EAST = 'EAST';    
    const WEST = 'WEST';  
    const SOUTH = 'SOUTH';
}

这样做,将允许我们按以下方式使用我们的类

// Instatiate a new CardinalPoint instance
$direction = new CardinalPoint(CardinalPoint::NORTH);

// Since Enums have builtin validation,
// the following line would throw an InvalidArgumentException:
$direction = new CardinalPoint('North');
// However the following would work:
$direction = new CardinalPoint('NORTH');

// Using in functions or class methods 
public function changeDirection(CardinalPoint $direction)
{
    // Testing equlity with string
    if(!$direction->isEqualTo(new CardinalPoint(CardinalPoint::EAST))) {
        echo 'Not going East!';
    }

    // Since the constants are strings, it is also possible to compare
    // using string comparison
    if($direction == CardinalPoint::NORTH) {
        echo 'Definitely going North!';
    }    
}

运行测试

测试基于 codeception。要运行测试,只需运行

composer test