estvoyage/value 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

estvoyage/value

Composer 安装命令:

composer require estvoyage/value

包简介

A set of whole value to avoid using native PHP type, see http://c2.com/cgi/wiki?ValueObject for more information

README 文档

README

A set of whole value for PHP

Beside using the handful of primitive type offered by PHP (floats, integers, strings, boolean, datetime), you will make and use new objects that represent the meaningful quantities of your business.
These values (like currency, credit card numbers, zip code, firstname, lastname, calendar period or telephone number) will carry whole, useful chunks of information from the user-interface to the domain model.
But often in early stages of development you make decisions about representing simple facts using primitive type…
And as development proceeds you realize that those simple items aren't so simple anymore: A telephone number may be represented as a string for a while, but later you realize that the telephone needs special behavior for formatting, extracting the area code, and the like.
So it must be interesting to use object to quantify your domain model and use these values as the arguments of your methods very early.

This set provide currently base classes for:

  • string
  • integer
  • unsigned integer
  • float
  • unsigned float

All instances of these classes are immutable, so if you try to set or unset one of their properties, a logicException will be throwed.
To access the underlying value, use property asString for string, asInteger for integer and asFloat for float.
All of these classes are abstract, so you should extends them to quantify your domain:

<?php

use estvoyage\value\string;

final class phone extends string
{
	function __construct($value)
	{
		parent::__construct($value);

		if (! static::validateFormat($value))
		{
			throw new \domainException('Phone number format is invalid');
		}
	}

	static function validate($value)
	{
		return parent::validate($value) && self::validateFormat($value);
	}

	private static function validateFormat($value)
	{
		return preg_match('/^\+\d{2} \d(?: \d{2}){4}$/', $value);
	}
}

$phone = new phone('+ 33 1 23 45 67 89');
echo $phone->asString; // display '+ 33 1 23 45 67 89'
echo $phone->{uniqid()}; // throw a \logicException with message 'Undefined property in phone: …'
$phone->{uniqid()} = uniqid(); // throw a \logicException with message 'phone is immutable'
$phone->asString = uniqid(); // throw a \logicException with message 'phone is immutable'
unset($phone->{uniqid()}); // throw a \logicException with message 'phone is immutable'
unset($phone->asString); // throw a \logicException with message 'phone is immutable'

$badFormat = new phone('01 23 45 67 89'); // throw a domainException!

Note that the phone class is final, because it contains a public property, so extend it will be a violation of the Liskov substitution principle.
All of these classes implements __toString() so you can use one of their instance as argument of any string related PHP function.

<?php

use estvoyage\value\string;

final class phone extends string {}

$phone = new phone('+ 33 1 23 45 67 89');
echo substr($phone, 0, 4); // display '+ 33'

estvoyage/value 适用场景与选型建议

estvoyage/value 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 426 次下载、GitHub Stars 达 3, 最近一次更新时间为 2014 年 11 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 426
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 14
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2014-11-21