Some convenient classes to describe/cast types of values

Maintainers

👁 matthiasmullie

Package info

github.com/matthiasmullie/types

pkg:composer/matthiasmullie/types

Fund package maintenance!

matthiasmullie

Statistics

Installs: 118

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

1.0.2 2024-05-28 03:26 UTC

Requires

Requires (Dev)

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 17e7abf7b58454cbe8bb85e873cb64e241a14a98

types

This package is auto-updated.

Last update: 2026-06-04 16:57:39 UTC


README

👁 Build status
👁 Code coverage
👁 Latest version
👁 Downloads total
👁 License

Installation

Simply add a dependency on matthiasmullie/types to your composer.json file if you use Composer to manage the dependencies of your project:

composer require matthiasmullie/types

Usage

use MatthiasMullie\Types;

$type = new Types\Json(
 new Types\Map([
 'user_id' => new Types\Sha1(
 description: 'Unique user id',
 ),
 'email' => new Types\Email(
 description: 'Email address of the user',
 ),
 'gender' => new Types\Enum(
 ['m', 'f'],
 description: 'Biological sex',
 ),
 'birthdate' => new Types\Optional(
 new Types\Integer(),
 description: 'Birth date, UNIX timestamp',
 ),
 ]),
);
// this will succeed because the input is valid;
// `birthdate`, given as a string, will be cast to an integer
$safeInput = $type([
 'user_id' => 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd4',
 'email' => 'jane.doe@example',
 'gender' => 'f',
 'birthdate' => '1715950172',
]);
// this will throw an exception because a required field (`gender`)
// is missing; note: `$type->test(...)` can also be used to simply
// check validity instead
$safeInput = $type([
 'user_id' => 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd4',
 'email' => 'jane.doe@example',
 'birthdate' => '1715950172',
]);
// this will throw an exception because the input for `email` is not
// a valid email address
$safeInput = $type([
 'user_id' => 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd4',
 'email' => 'not an email address',
 'gender' => 'f',
 'birthdate' => '1715950172',
]);

License

types is MIT licensed.