定制 data-type/contracts 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

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

Link to Packagist

The Date package provides classes for working with dates, including:

  • Date
  • Month
  • Weekday
  • Year

Related Packages

License

MIT License. See LICENSE file for details.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固