triun/value-object

v1.0.0 2017-03-09 06:10 UTC

This package is auto-updated.

Last update: 2024-09-17 21:49:08 UTC


README

Latest Version on Packagist Pre Release Version on Packagist Latest Unstable Version Build Status Total Downloads Software License

值对象定义。

关于

PHP 类接口,用于定义和标准化值对象格式。

在计算机科学中,值对象是一个小对象,表示一个简单的实体,其等价性不基于身份:即当两个值对象具有相同的值时,它们是相等的,不一定是指同一个对象(维基百科)。

安装

使用以下命令通过 composer 需求此包

composer require triun/value-object

描述

该包包含一个 ValueObject 接口

interface ValueObject
{
    /**
     * Returns a object taking PHP native value(s) as argument(s).
     *
     * @return \Triun\ValueObject\ValueObject
     */
    public static function fromNative();

    /**
     * Returns PHP native value(s)
     *
     * @return mixed
     */
    public function toNative();

    /**
     * Compare two ValueObjectInterface and tells whether they can be considered equal.
     *
     * @param \Triun\ValueObject\ValueObject $object
     *
     * @return mixed
     */
    public function equals(ValueObject $object);

    /**
     * Returns a string representation of the object.
     *
     * @return string
     */
    public function __toString();
}

以及一个 Invalid Argument Exception 异常

class InvalidNativeArgumentException extends \InvalidArgumentException
{
    /**
     * InvalidNativeArgumentException constructor.
     *
     * @param string $value
     * @param array  $allowed_types
     */
    public function __construct($value, array $allowed_types)
    {
        $this->message = sprintf(
            'Argument "%s" is invalid. Allowed types for argument are "%s".',
            $value,
            implode(', ', $allowed_types)
        );
    }
}

用法

单个字段值对象的用法示例

use Triun\ValueObject\ValueObject;
use Triun\ValueObject\Exceptions\InvalidNativeArgumentException;

class StringLiteral implements ValueObject
{
    /**
     * Native string
     *
     * @var string
     */
    protected $value;
    
    /**
     * Returns a StringLiteral object given a PHP native string as parameter.
     *
     * @internal param string $value
     * 
     * @return StringLiteral
     */
    public static function fromNative()
    {
        $value = func_get_arg(0);
        
        return new static($value);
    }
    
    /**
     * Returns a StringLiteral object given a PHP native string as parameter.
     *
     * @param string $value
     */
    public function __construct($value)
    {
        if (false === \is_string($value)) {
            throw new InvalidNativeArgumentException($value, array('string'));
        }
        
        $this->value = $value;
    }
    
    /**
     * Returns the value of the string
     *
     * @return string
     */
    public function toNative()
    {
        return $this->value;
    }
    
    /**
     * Tells whether two string literals are equal by comparing their values
     *
     * @param  ValueObject $stringLiteral
     * 
     * @return bool
     */
    public function equals(ValueObject $stringLiteral)
    {
        if (static::class !== get_class($stringLiteral)) {
            return false;
        }
        
        return $this->toNative() === $stringLiteral->toNative();
    }
    
    /**
     * Tells whether the StringLiteral is empty
     *
     * @return bool
     */
    public function isEmpty()
    {
        return \strlen($this->toNative()) == 0;
    }
    
    /**
     * Returns the string value itself
     *
     * @return string
     */
    public function __toString()
    {
        return $this->toNative();
    }
}

链接

问题

可以在 Github 问题跟踪器 上提交错误报告和功能请求。

贡献

有关信息,请参阅 CONTRIBUTING.md

许可证

Laravel 模型基是开源软件,根据 MIT 许可证 许可。