selami / entity

一个用于断言使用 JSON Schema 标准(草案-07 和草案-06)定义的模型的变量类型和值的库。

1.4.2 2018-12-07 22:17 UTC

This package is auto-updated.

Last update: 2024-09-08 11:01:35 UTC


README

一个与框架无关的实体/值对象库,用于断言使用 JSON Schema 标准(草案-07 和草案-06)定义的模型的变量类型和值,由 PHP 7.2 编写。

Build Status Coverage Status Scrutinizer Code Quality Codacy Badge Latest Stable Version Total Downloads Latest Unstable Version License

动机

此库是一种辅助库,用于使用 JSON Schema 生成自己的实体或值对象模型。不期望直接使用此库中的类(请参阅以下示例用法部分)。要了解实体和值对象之间的区别,请阅读 Philip Brown 在 culttt.com 上的文章:culttt.com

安装

composer require selami/entity

值对象(查看解释

  • 使用 ValueObject 创建的对象是 不可变对象。这意味着其唯一的数据注入点是构造函数。
  • 它在对象创建时验证数据
  • 如果验证失败,则抛出 InvalidArgumentException。
  • 始终使用 ValueObjectBuilder 创建 ValueObject 实例。
使用 ValueObjectBuilder 的约定
  • 属性名的首字符大写。
  • 然后添加 "with" 前缀到属性名。

例如,如果我们的属性名是 creditCardNumber,则该属性的 setter 方法名为 withCreditCardNumber。

用法

假设您有一个位于 ./models/credit-card.json 的 JSON Schema 文件。请参阅 信用卡值对象模式

<?php
declare(strict_types=1);

use Selami\Entity\ValueObjectBuilder;

$creditCard = ValueObjectBuilder::createFromJsonFile(
	'./models/credit-card.json'
)
	->withCardNumber('5555555555555555')
	->withCardHolderName('Kedibey Mırmır')
	->withExpireDateMonth(8)
	->withExpireDateYear(24)
	->withCvvNumber('937')
	->build();

echo $creditCard->cardHolderName;

实体

  • 实体需要 id 属性。
  • 实体是可变的。
  • 实体在对象创建时不会自动验证。
  • 当验证失败时,它会抛出 InvalidArgumentException。
  • 可以部分验证实体。
  • 可以用于数据验证、在持久化之前的数据验证等。

用法

假设您有一个位于 ./models/profile.json 的 JSON Schema 文件。请参阅 个人资料实体模式

<?php
declare(strict_types=1);

use Selami\Entity\Entity;
use stdClass;
use Ramsey\Uuid\Uuid

$id = Uuid::uuid4()->toString();
$entity = Entity::createFromJsonFile('./models/profile.json', $id);
$entity->name = 'Kedibey';
$entity->age = 11;
$entity->email = 'kedibey@world-of-wonderful-cats-yay.com';
$entity->website = 'world-of-wonderful-cats-yay.com';
$entity->location = new stdClass();
$entity->location->country = 'TR';
$entity->location->address = 'Kadıköy, İstanbul';
$entity->available_for_hire = true;
$entity->interests = ['napping', 'eating', 'bird gazing'];
$entity->skills = [];
$entity->skills[0] = new stdClass();
$entity->skills[0]->name = 'PHP';
$entity->skills[0]->value = 0;

$entity->validate();

部分验证

<?php
declare(strict_types=1);

use Selami\Entity\Entity;
use stdClass;
use Ramsey\Uuid\Uuid

$id = Uuid::uuid4()->toString();
$entity = Entity::createFromJsonFile('./models/profile.json', $id);
$entity->name = 'Kedibey';
$entity->age = 11;
$entity->email = 'kedibey@world-of-wonderful-cats-yay.com';
$entity->website = 'world-of-wonderful-cats-yay.com';

$partiallyValidateFields = ['name', 'age', 'email', 'website'];

$entity->validatePartially(partiallyValidateFields);

预期用法示例

值对象示例

<?php
declare(strict_types=1);

namespace MyLibrary\ValueObject;

use Selami\Entity\ValueObjectBuilder;

final class CreditCard
{
    private static $schemaFile = './models/credit-card.json';

    public static function create() : ValueObjectBuilder
    {
        return ValueObjectBuilder::createFromJsonFile(self::$schemaFile);
    }
}
<?php
declare(strict_types=1);

require 'vendor/autoload.php';

use MyLibrary\ValueObject\CreditCard;

$valueObject = CreditCard::create()
    ->withCardNumber('5555555555555555')
    ->withCardHolderName('Kedibey Mırmır')
    ->withExpireDateMonth(8)
    ->withExpireDateYear(24)
    ->withCvvNumber('937')
    ->build();

// Prints "Kedibey Mırmır"
var_dump($valueObject->cardHolderName);

实体示例

<?php
declare(strict_types=1);

namespace MyLibrary\Entity;

use Selami\Entity\Interfaces\EntityInterface;
use stdClass;
use Selami\Entity\Model;
use Selami\Entity\EntityTrait;

final class Profile implements EntityInterface
{
	private static $schemaFile = './models/profile.json';

	use EntityTrait;

	public static function create(string $id, ?stdClass $data=null) : EntityInterface
	{
		$model = Model::createFromJsonFile(self::$schemaFile);
		return new static($model, $id, $data);
	}
}
<?php
declare(strict_types=1);

require 'vendor/autoload.php';

use Ramsey\Uuid\Uuid;

$id = Uuid::uuid4()->toString();

$entity = Profile::create($id);
$entity->name = 'Kedibey';

// Prints "Kedibey"
var_dump($entity->name);

// Throws "Selami\Entity\Exception\InvalidArgumentException"
$entity->validate();