PHPackages                             somnambulist/doctrine-enum-bridge - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. somnambulist/doctrine-enum-bridge

Abandoned → [somnambulist/domain](/?search=somnambulist%2Fdomain)ArchivedLibrary[Database &amp; ORM](/categories/database)

somnambulist/doctrine-enum-bridge
=================================

Allows any enumeration library to be used with Doctrine type system.

1.2.0(8y ago)015.7k1MITPHPPHP &gt;=7

Since Jun 24Pushed 6y ago1 watchersCompare

[ Source](https://github.com/dave-redfern/somnambulist-doctrine-enum-bridge)[ Packagist](https://packagist.org/packages/somnambulist/doctrine-enum-bridge)[ RSS](/packages/somnambulist-doctrine-enum-bridge/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (4)Versions (5)Used By (1)

Moved to
==============================================================

[](#moved-to-httpsgithubcomdave-redfernsomnambulist-domain)

This repository has been archived. Please update to the combined package.s

Doctrine Enum Bridge
--------------------

[](#doctrine-enum-bridge)

Provides a bridge between different enumeration implementations and Doctrine. Any type of PHP enumerable (e.g. Eloquent\\Enumeration or myclabs/php-enum can be used with this adaptor.

A default, string casting, serializer is used if no serializer is provided.

All enumerations are stored using the DBs native varchar format. If you wish to use a custom DB type, extend and re-implement the `getSQLDeclaration()` method.

### Requirements

[](#requirements)

- PHP 7+
- Doctrine 2.5+

### Installation

[](#installation)

Install using composer, or checkout / pull the files from github.com.

- composer require somnambulist/doctrine-enum-bridge

### Usage

[](#usage)

In your frameworks application service provider / bundle boot method, register your enums and the appropriate callables to create / serialize as needed. A default serializer that casts the enumerable to a string will be used if none is provided.

The callbacks will receive:

- value - the current value either a PHP type, or the database type (for constructor)
- name - the bound name given to the enumeration this could be the FQCN
- platform - the Doctrine AbstractPlatform instance

For example, in a Symfony project, in your AppBundle class:

```
class AppBundle extends Bundle
{
    public function boot()
    {
        EnumerationBridge::registerEnumType(Action::class, function ($value) {
            if (Action::isValid($value)) {
                return new Action($value);
            }

            throw new InvalidArgumentException(sprintf(
                'The value "%s" is not valid for the enum "%s". Expected one of ["%s"]',
                $value,
                Action::class,
                implode('", "', Action::toArray())
            ));
        });
    }
}
```

In Laravel, add to your AppServiceProvider (`register` and `boot` should both work):

```
class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        EnumerationBridge::registerEnumType(Action::class, function ($value) {
            if (Action::isValid($value)) {
                return new Action($value);
            }

            throw new InvalidArgumentException(sprintf(
                'The value "%s" is not valid for the enum "%s". Expected one of ["%s"]',
                $value,
                Action::class,
                implode('", "', Action::toArray())
            ));
        });
    }
}
```

**Note:** the bridge will check if the type has already been registered and skip it if that is the case. If you wish to replace an existing type then you should use `Type::overrideType()`, however that will only work if the type has already been registered.

#### Register Multiple Types

[](#register-multiple-types)

Multiple enumerations can be registered at once by calling `registerEnumTypes()` and passing an array of enum name and either an array of callables (constructor, serializer) or just the constructor:

```
class AppBundle extends Bundle
{
    public function boot()
    {
        EnumerationBridge::registerEnumTypes(
            [
                'gender' => [
                    function ($value) {
                        if (Gender::isValidValue($value)) {
                            return Gender::memberByValue($value);
                        }

                        throw new InvalidArgumentException(sprintf(
                            'The value "%s" is not valid for the enum "%s"', $value, Gender::class
                        ));
                    },
                    function ($value, $platform) {
                        return is_null($value) ? 'default' : $value->value();
                    }
                ]
            ]
        );
    }
}
```

#### Usage in Doctrine Mapping Files

[](#usage-in-doctrine-mapping-files)

In your Doctrine mapping files simply set the type on the field:

```
fields:
    name:
        type: string
        length: 255

    gender:
        type: gender

    action:
        type: AppBundle\Enumerable\Action
```

#### Share a Constructor

[](#share-a-constructor)

For enumerables that have the same signature / method names, a constructor callable can be shared.

To share a constructor callable, use the FQCN as the name of the enumerable or you could use a hash map of aliases to Enumerables:

```
class AppBundle extends Bundle
{
    public function boot()
    {
        $constructor = function ($value, $class) {
            // constructor method should handle nulls
            if (is_null($value)) {
                return null;
            }

            if ($class::isValid($value)) {
                return new $class($value);
            }

            throw new InvalidArgumentException(sprintf(
                'The value "%s" is not valid for the enum "%s". Expected one of ["%s"]',
                $value,
                $class,
                implode('", "', $class::toArray())
           ));
        }

        EnumerationBridge::registerEnumType(Action::class, $constructor);
        EnumerationBridge::registerEnumType(Country::class, $constructor);
        EnumerationBridge::registerEnumType(Currency::class, $constructor);
    }
}
```

Because each enumerable can be mapped to its own construct / serializer handlers, complex multitions from the Eloquent\\Enumerable library can be handled by this bridge.

### Links

[](#links)

- [Doctrine](http://doctrine-project.org)
- [Doctrine Enum Type](https://github.com/acelaya/doctrine-enum-type)
- [Eloquent Enumeration](https://github.com/eloquent/enumeration)
- [PHP-Enum](https://github.com/myclabs/php-enum)

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~40 days

Total

4

Last Release

3122d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c0e439bf91b68c2e81e0250c752d3a552e78752c91acdf00c72a1f93aeb1ceca?d=identicon)[dredfern](/maintainers/dredfern)

---

Top Contributors

[![dave-redfern](https://avatars.githubusercontent.com/u/1477147?v=4)](https://github.com/dave-redfern "dave-redfern (2 commits)")

---

Tags

doctrineenumerationdoctrinedddenumerations

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/somnambulist-doctrine-enum-bridge/health.svg)

```
[![Health](https://phpackages.com/badges/somnambulist-doctrine-enum-bridge/health.svg)](https://phpackages.com/packages/somnambulist-doctrine-enum-bridge)
```

###  Alternatives

[scienta/doctrine-json-functions

A set of extensions to Doctrine that add support for json query functions.

58523.9M36](/packages/scienta-doctrine-json-functions)[laravel-doctrine/orm

An integration library for Laravel and Doctrine ORM

8425.3M87](/packages/laravel-doctrine-orm)[martin-georgiev/postgresql-for-doctrine

Extends Doctrine with native PostgreSQL support for arrays, JSONB, ranges, PostGIS geometries, text search, ltree, uuid, and 100+ PostgreSQL-specific functions.

4485.3M4](/packages/martin-georgiev-postgresql-for-doctrine)[damienharper/auditor-bundle

Integrate auditor library in your Symfony projects.

4542.8M](/packages/damienharper-auditor-bundle)[flow-php/doctrine-dbal-bulk

Bulk inserts and updates for Doctrine DBAL

14295.2k1](/packages/flow-php-doctrine-dbal-bulk)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
