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.

Maintainers

Statistics

Installs: 1 914

Dependents: 3

Suggesters: 0

Stars: 0

100.0.0 2026-06-23 12:12 UTC

This package is auto-updated.

Last update: 2026-07-02 10:26:37 UTC


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.