承接 morebec/value-objects 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

morebec/value-objects

Composer 安装命令:

composer require morebec/value-objects

包简介

PHP ValueObjects library

README 文档

README

A PHP Value Object Library in use by Morebec Projects

Build Status Coverage Status

Value objects are small objects representing simple concepts, and whose equality is based on their internal property values rather than a specific identity.

Value objects must honour the following contract:

  • They are immutable (no setters)
  • They are Self Validating
  • They represent and describe concepts in a clear way

Installation

To install the library in a project, add these lines to your composer.json configuration file:

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

Usage

This library comes with a number of predesigned ValueObject classes, that you can use in your projects. The ValueObject either implement the ValueObjectInterface or extend the BasicEnum class.

Creating your own Value Object using ValueObjectInterface

To create, one needs to implement the ValueObjectInterface and implement the two following methods:

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

Here's a basic example:

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;
    }
}

Doing that, our class can be used as follows:

$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();

Creating your own Enum class extending BasicEnum

To create a new Enum, one needs to extend the BasicEnum class. As an example, lets pretend we want to create a CardinalPoint Class. Since there are strictly 4 cardinal points, we will create an enum based ValueObject:

<?php

use Morebec\ValueObjects\ValueObjectInterface;

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

Doing this, will allow us to use our class in the following way:

// 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!';
    }    
}

Running Tests

The tests are based on codeception. To run the tests simply run:

composer test

morebec/value-objects 适用场景与选型建议

morebec/value-objects 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 70 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 01 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 morebec/value-objects 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2020-01-20