data-type/contracts
Composer 安装命令:
composer require data-type/contracts
包简介
DataType closes an important gap in PHP by providing a simple and consistent pattern for designing complex data types such as `Date`, `Email`, `Amount`. Together with its sister package, data-type/operator, it adds functionality similar to operator overloading.
README 文档
README
DataType closes an important gap in PHP by providing a simple and consistent
pattern for designing complex data types such as Date, Email, Amount.
Together with its sister package, data-type/operator, it provides functionality similar to operator overloading, a feature that PHP does not natively support.
By using the class O, complex data types can gain capabilities such as:
- Comparison
- Increment and decrement operations
- Arithmetic calculations
This allows developers to work with rich DataTYpes in a more expressive, readable, and type-safe way.
Installation
composer require data-type/contracts
Example
composer require data-type/date
#[Test]
public function date(): void
{
$date = new Date('2025-10-11');
self::assertSame('2025-10-11', $date->serializeToString());
self::assertSame('2025-10-11', (string) $date);
}
Example with Operator
For this example, the operator package is required as well:
composer require data-type/operator
data-type/operator - Operator for comparable types
#[Test]
public function operator(): void
{
$date1 = new Date('2025-10-11');
$date2 = new Date('2025-10-10');
self::assertTrue(O::greater($date1, $date2));
O::decrement($date1);
self::assertTrue(O::equals($date1, $date2));
}
The result is a programming model that feels closer to native operators while remaining fully compatible with PHP.
How to Build a Custom DataType
To build a custom DataType, implement the DataTypeInterface interface
and make sure to implement the serializeToString() method.
The serializeToString() method must return the same string
representation that was passed to the constructor.
You can use the AbstractDataType class as a base class.
Please note that the DataType class must be marked as final and readonly.
<?php
declare(strict_types=1);
namespace MyVendor\DataType;
use DataType\Contracts\Abstract\AbstractDataType;
use Override;
final readonly class Email extends AbstractDataType
{
public function __construct(protected string $serializedString) {
}
#[Override]
protected function _serializeToString(): string
{
return $this->serializedString;
}
}
How to Build a Custom DataType Test Case
#[Test]
public function email(): void
{
$email = new Email('max.mustermann@example.com');
self::assertSame('max.mustermann@example.com', $email->serializeToString());
}
Predefined DataTypes
Date
composer require data-type/date
The Date package provides classes for working with dates, including:
DateMonthWeekdayYear
Related Packages
- data-type/contracts-operator - Operator contracts for comparable types
- data-type/operator - Operator for comparable types
License
MIT License. See LICENSE file for details.
统计信息
- 总下载量: 1.76k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-24