定制 phpgears/value-object 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

phpgears/value-object

Composer 安装命令:

composer require phpgears/value-object

包简介

Value object for PHP

README 文档

README

PHP version Latest Version License

Build Status Style Check Code Quality Code Coverage

Total Downloads Monthly Downloads

value-object

Immutable Value Objects for PHP

Installation

Composer

composer require phpgears/value-object

Usage

Require composer autoload file

require './vendor/autoload.php';

Extend from Gears\ValueObject\AbstractValueObject. Make your class final, value objects should always be final

Be aware of the protected constructor, you should create a "named constructor" for your value object

use Gears\ValueObject\AbstractValueObject;

final class CustomStringValueObject extends AbstractValueObject
{
    private $value;

    public static function fromString(string $value)
    {
        $stringObject = new self();
        $stringObject->value = $value;

        return $stringObject;
    }

    public function getValue(): string
    {
        return $this->value;
    }

    public function isEqualTo($valueObject): bool
    {
        return \get_class($valueObject) === self::class 
            && $valueObject->getValue() === $this->value;
    }
}

Serialization

Extending AbstractValueObject does not automatically define serialization mechanisms in your value objects because value objects can be composed of several values, other value objects and even other objects such as enums

For this consider adding serialization methods in your value objects to control how serialization takes place

use Gears\ValueObject\AbstractValueObject;

final class Money extends AbstractValueObject implements \Serializable
{
    private const CURRENCY_EUR = 'eur';

    private $value;
    private $precision;
    private $currency;

    public static function fromEuro(int $value, int $precision)
    {
        $money = new self();
        $money->value = $value;
        $money->precision = $precision;
        $money->currency = static::CURRENCY_EUR; // Should be an enum

        return $money;
    }

    // [...]

    final public function __serialize(): array
    {
        return [
            'value' => $this->value,
            'precision' => $this->precision,
            'currency' => $this->currency,
        ];
    }

    final public function __unserialize(array $data): void
    {
        $this->assertImmutable();

        $this->value = $data['value'];
        $this->precision = $data['precision'];
        $this->currency = $data['currency'];
    }

    final public function serialize(): string
    {
        return serialize([
            $this->value,
            $this->precision,
            $this->currency,
        ]);
    }

    public function unserialize($serialized): void 
    {
        $this->assertImmutable();

        list(
            $this->value,
            $this->precision,
            $this->currency
        ) = \unserialize($serialized, ['allowed_classes' => false]);
    }
}

Enums and Value Objects get along perfectly, consider using phpgears/enum for enumerations

Contributing

Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before.

See file CONTRIBUTING.md

License

See file LICENSE included with the source code for a copy of the license terms.

统计信息

  • 总下载量: 1.35k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-09-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固