基本类型类,用于封装标量值以在应用程序中创建类型。

8.1.1 2024-05-29 18:58 UTC

README

CircleCI Last Commit Dependencies Latest release Code Coverage

描述

基本类型,它封装标量类型,就像DateType一样也封装PHP库中的其他类型,以在应用程序中创建类型。

对于每个类型(除了Uuid4),都有一个接口和一个特质,该特质已经实现了大多数接口方法。

这些类型也可以从抽象类派生。这些类实现了所有接口方法,并提供自动验证。

抽象类是不可变的。

除了AbstractDateType之外,所有抽象类型类都有一个transform方法,该方法在构造函数验证(通过isValid)之后调用。此方法默认不更改值。如果需要更改值,则可以完全重写。

应用示例

字符串

class ClientId extends AbstractStringType
{
    public static function isValid( string $value ): bool
    {
        return $value !== '';
    }
}
class ChannelId extends AbstractStringType
{
    public static function isValid( string $value ): bool
    {
        return $value !== '';
    }
}
$clientId           = new ClientId( 'gmo' );
$anotherClientId    = new ClientId( 'gmo' );
$yetAnotherClientId = new ClientId( 'maerz' );
$channelId          = new ChannelId( 'gmo' );
$anotherChannelId   = new ChannelId( 'zalando' );

$clientId->equals( $anotherClientId ) //true
$clientId->equals( $yetAnotherClientId ) //false
$clientId->equals( $channelId ) //false
$clientId->equalsValue( $channelId ) //true
$clientId->equalsValue( 'gmo' ) //true

$newClientId = ClientId::fromStringType( $anotherChannelId );
get_class( $newClientId ); //ClientId
$newClientId->toString(); //zalando
(string)$newClientId; //zalando

整数

class Quantity extends AbstractStringType
{
    public static function isValid( int $value ): bool
    {
        return $value > 0;
    }
}
$quantityOfFirstItem  = new Quantity( 2 );
$quantityOfSecondItem = new Quantity( 5 );

$totalQuantity = $quantityOfFirstItem->add( $quantityOfSecondItem ); //7
$difference    = $quantityOfFirstItem->subtract( $quantityOfSecondItem ); //throws ValidationException
$difference    = $quantityOfSecondItem->subtract( $quantityOfFirstItem ); //3

$incrementedQuantity = $quantityOfFirstItem->increment( $quantityOfSecondItem ); //7

也可以使用原始数据类型进行计算。

$quantity  = new Quantity( 2 );

$totalQuantity = $quantity->add( 5 ); //7
$difference    = $quantity->subtract( 5 ); //-3

$incrementedQuantity = $quantity->increment( 10 ); //12

自定义Uuid4类型

class FulfillmentId extends Uuid4
{
}

$fulfillmentId        = FulfillmentId::generate(); //some UUID4
$anotherFulfillmentId = new FulfillmentId( '9b856c0e-610a-4e38-9ea6-b9ac63cfb521' ); 

Uuid4

$uuid4 = (string)Uuid4::generate();

特质RepresentingStringType和抽象类AbstractStringType提供的额外方法

  • getLength
  • contains
  • split
  • splitRaw
  • matchRegularExpression

抽象类AbstractStringType提供的额外方法

  • trim
  • replace
  • substring
  • toLowerCase
  • toUpperCase
  • capitalizeFirst
  • deCapitalizeFirst
  • toKebabCase
  • toSnakeCase
  • toUpperCamelCase
  • toLowerCamelCase

DateType

class UpdatedOn extends AbstractDateType
{
    public static function isValid( \DateTimeInterface $value ): bool
    {
        return true;
    }
}

$updatedOn = new UpdatedOn('2023-07-07 08:01:20', new \DateTimeZone( '+0200' ) ))->toString() ); //some UUID4
$updatedOn->hasExpired(); //Checks if current date time is greater than date time of UpdatedOn. Returns boolean
$updatedOn->hasExpired( new \DateInterval('PT15M') ); //Checks if current date time is greater than date time of UpdatedOn and added \DateInterval. Returns boolean

RepresentsDateType扩展以下接口

  • \Stringable
  • \JsonSerializable

RepresentsDateType提供的方法

  • equals
  • equalsValue
  • toDateTime
  • sub
  • add
  • diff
  • isLessThan
  • isGreaterThan
  • isGreaterThanOrEqual
  • isLessThanOrEqual
  • hasExpired
  • format
  • getOffset
  • getTimestamp
  • getTimezone
  • toString

辅助工具

TypesToArrayHelper可用于将包含StringTypes、FloatTypes、IntTypes或ArrayTypes的数组转换为包含原始数据类型的数组。

示例

$types = [
    new AnyStringType( 'one' ),
    new AnyStringType( 'two' ),
    new AnotherStringType( 'three' ),
];

TypesToArrayHelper::toStringArray( $types ); // [ 'one', 'two', 'three' ]

Json

所有类型都实现了\JsonSerializable,可以使用json_encode进行序列化。