PHP types as objects

1.0.0 2024-01-11 06:48 UTC

This package is auto-updated.

Last update: 2024-11-11 08:36:41 UTC


README

tests version downloads license

This package provides the ability to work with types as objects.

Available types:

  • Integer
  • String
  • Float
  • Boolean
  • Array

Installation

composer require elegant-php/types

Usage

Use predefined classes and contracts

final class AuthorizedUser implements User
{
  public function __construct(
    private readonly StringType $name
  ) { }
}

$user = new AuthorizedUser(new DefaultString('my user'));

Implement type contracts

final class UserName extends StringType
{
  public function __construct(
    private readonly string $name
  ) { }

  public function value(): string
  {
    return strtolower($this->name);
  }
}

Compare types

$first = new DefaultString('first');
$second = new DefaultString('first');

var_dump($first->equals($second));

// bool(false)

Type casting

$id = new StringAsInteger('123');

var_dump($id->value());

// int(123)

Release notes