freyr/identity

A PHP library for Identity concept.

Installs: 751

Dependents: 3

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/freyr/identity

v1.0.1 2026-03-01 17:41 UTC

This package is auto-updated.

Last update: 2026-03-01 17:42:23 UTC


README

Monotonic ULID identity objects for PHP domain models. Provides an immutable Id value object and an IdCollection for working with sets of identifiers.

Installation

composer require freyr/identity

Requires PHP 8.2+.

Usage

Id

Create, convert and compare identifiers.

use Freyr\Identity\Id;

$id = Id::new();
$id = Id::fromString('01ARYZ6S41TSV4RRFFQ69G5FAV');
$id = Id::fromBinary($bytes);

(string) $id;    // ULID string (26 chars, Crockford Base32)
$id->toBinary(); // 16-byte binary
$id->sameAs($other);

Extend for typed identifiers.

class UserId extends Id {}

$userId = UserId::new();

IdCollection

Immutable collection with standard operations.

use Freyr\Identity\IdCollection;

$collection = IdCollection::fromArray([$id1, $id2]);
$collection = IdCollection::empty();

$collection->add($id);
$collection->remove($id);
$collection->contains($id);
$collection->merge($other);
$collection->intersect($other);
$collection->filter(fn (Id $id) => /* ... */);
$collection->map(fn (Id $id) => /* ... */);

$collection->first();
$collection->last();
$collection->count();
$collection->isEmpty();
$collection->toArray();
$collection->toStringArray();
$collection->toBinaryArray();

All mutating methods return new instances.

Licence

MIT