定制 yomy/valueobject 二次开发

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

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

yomy/valueobject

Composer 安装命令:

composer require yomy/valueobject

包简介

Value objects

README 文档

README

Value Objects and Enums

Build Status

Installation and documentation

  • Available as [Composer] package [yomy/valueobject].

What is this library about

This library adds a ValueObject and EnumValueObject classes.

Examples of value object

Creating an object:

use YomY\ValueObject\ValueObject;
$object = ValueObject::instance(1);

Getting value from the object

$value = $object->getValue();

Objects with the same value are going to be the same object

$object1 = ValueObject::instance(1);
$object2 = ValueObject::instance(1);
//These two are the same objects ($object1 === $object2)

You can use type hinting in methods

public function doSomething(ValueObject $valueObject) {
    $value = $valueObject->getValue();
    ...
}

You can extend the object for more detailed type hinting

class UserId extends ValueObject {}
class DataId extends ValueObject {}
...
public function doSomething(UserId $userId, DataId $dataId) {
    ...
}

Objects of different class or variable type are different

$object1 = ValueObject::instance('');
$object2 = ValueObject::instance(null);
$object3 = ValueObject::instance(false);
$object4 = ExtendedValueObject::instance('');
$object5 = ExtendedValueObject::instance(null);
$object6 = ExtendedValueObject::instance(false);
//All of the above are different

Instead of a strong (===) comparison operator, you could also use the equals() method

$object1 = ValueObject::instance(1);
$object2 = ValueObject::instance(1);
$same = $object1->equals($object2); //true

Generally, unserialize of a value object is prohibited, as this would break the ability to compare objects by reference. However, you might have a case you don't care about strict comparison, and need to unserialize the object. You can add a WeakValueObjectTrait usage to your custom object, which will allow unserializing it, and also compare objects by the values instead of reference when using equals() method

class MyWeakObject extends ValueObject {
    use WeakValueObjectTrait;
}
...
$weakObject1 = MyWeakObject::instance(1);
$serializedWeakObject = serialize($weakObject1);
$weakObject2 = unserialize($serializedWeakObject);
//These two objects are "equal" but not the same
$weakObject1->equals($weakObject2); //true
//On regular value objects this would be false

Examples of Enum value object

Creating an enum object

use YomY\ValueObject\EnumValueObject;
class Category extends EnumValueObject {
    const FIRST = 1;
    const SECOND = 2;
    const THIRD = 3;
}

Creating enum objects

$category = Category::instance(Category::FIRST);

or by referring referring to key

$category = Category::FIRST();

You will get an error if you try to instantiate invalid value

$category = Category::instance('missing_value');
$category = Category::MISSING();

Examples of Positive Integer value object

As value objects are commonly used as identifiers for database entities with an integer key, positive int value object ensures a valid key object for this purpose

Creating an object:

use YomY\ValueObject\PositiveIntValueObject;
$object1 = PositiveIntValueObject::instance(1);
$object2 = PositiveIntValueObject::instance('1');
//These two are the same objects ($object1 === $object2)

Usually, the id key in the db cannot be a 0, so these objects are invalid:

$object = PositiveIntValueObject::instance(0);
$object = PositiveIntValueObject::instance('0');

Of course, as with a basic value object, it is intended to use these in extended classes.

class UserId extends PositiveIntValueObject {}
class DataId extends PositiveIntValueObject {}
$user = UserId::instance(42);
$data = DataId::instance(42);
//these two are not the same

yomy/valueobject 适用场景与选型建议

yomy/valueobject 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.47k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 09 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「enum」 「ddd」 「ValueObject」 「value-object」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 yomy/valueobject 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yomy/valueobject 我们能提供哪些服务?
定制开发 / 二次开发

基于 yomy/valueobject 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2017-09-20