肉磨机/可相等

此包已被废弃,不再维护。未建议替代包。

对象自定义相等性判断接口。

1.0.0 2017-03-05 18:41 UTC

This package is not auto-updated.

Last update: 2020-01-24 15:51:46 UTC


README

Latest Stable Version License Total Downloads

可相等

可相等 库提供单个接口,定义一个实现类可以使用的方法来提供自定义相等性判断。这对于值对象和实体都很有用,因为前者可能想要允许对标量类型的相等性检查,而后者需要从相等性判断中排除所有不是其身份定义值的封装值。

安装

打开终端,进入您的项目目录,然后执行以下命令将此包添加到依赖项

composer require fleshgrinder/equalable

此命令需要您已全局安装 Composer,如 Composer 文档的 安装章节 中所述。

用法

只需实现接口并实现所需的 equals 方法。方法体的实际实现取决于用例。例如,值对象可能想要允许对标量类型的相等性检查。

final class UserId implements Equalable {
	private $uid;

	// ...

	public function equals($other): bool {
		if ($other instanceof $this) {
			$other = $other->uid;
		}

		return \is_int($other) && $this->uid === $other;
	}
}

另一方面,实体希望从判断中排除所有不是其身份定义值的封装值,例如。

final class User implements Equalable {
	private $uid;
	private $name;
	private $email;

	// ...

	public function equals($other): bool {
		return $other instanceof $this && $this->uid->equals($other->uid);
	}
}

测试

由于此库提供单个接口,该接口显然没有实现,因此没有测试。