承接 fortuneglobe/types 相关项目开发

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

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

fortuneglobe/types

Composer 安装命令:

composer require fortuneglobe/types

包简介

Basic type classes wrapping scalar values to create types in applications.

README 文档

README

CircleCI Last Commit Dependencies Latest release Code Coverage

Beschreibung

Basistypen, die skalare Typen, aber wie bei DateType auch andere Typen aus der PHP Bibliothek wrappen, um Typen in Anwendungen zu erstellen.

Für jeden Typen (ausgenommen Uuid4) gibt es ein Interface und einen Trait, welcher bereits die meisten Interface-Methoden implementiert.

Die Typen können auch von den abstrakten Klassen abgeleitet werden. Diese implementieren alle Interface-Methoden und stellen zu dem eine automatische Validierung bereit.

Die abstrakten Klassen sind immutable.

Bis auf AbstractDateType haben alle abstrakten Typ Klassen eine transform Methode, welche im Konstruktor nach der Validierung (durch isValid) aufgerufen wird. Diese Methode verändert den Wert standardmäßig nicht. Sie kann vollständig überschrieben werden, falls der Wert verändert werden soll.

Anwendungsbeispiele

Strings

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

Integers

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

Man kann auch mit primitiven Datentypen rechnen.

$quantity  = new Quantity( 2 );

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

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

Eigene Uuid4-Typen

class FulfillmentId extends Uuid4
{
}

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

Uuid4

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

Additional methods provided by trait RepresentingStringType and so also by AbstractStringType

  • getLength
  • contains
  • split
  • splitRaw
  • matchRegularExpression

Additional methods provided by 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 extends following interfaces

  • \Stringable
  • \JsonSerializable

Methods provided by RepresentsDateType

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

Helpers

TypesToArrayHelper kann benutzt werden, um aus Arrays, bestehend aus StringTypes, FloatTypes, IntTypes oder ArrayTypes, Arrays mit primitiven Datentypen zu bauen.

Beispiel:

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

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

Json

Alle Typen implementieren \JsonSerializable und können damit mit json_encode serialisiert werden.

fortuneglobe/types 适用场景与选型建议

fortuneglobe/types 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.06k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 11 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: proprietary
  • 更新时间: 2017-11-13