2017 © Pedro PelĂĄez
 

library scalar

Enumeration types and value dependent sub-types for Doctrine

image

doctrineum/scalar

Enumeration types and value dependent sub-types for Doctrine

  • Wednesday, July 18, 2018
  • by jaroslavtyc
  • Repository
  • 1 Watchers
  • 0 Stars
  • 7,155 Installations
  • PHP
  • 9 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 22 Versions
  • 4 % Grown

The README.md

Build Status Test Coverage Latest Stable Version License, (*1)

Customizable enumeration type for Doctrine 2.4+

About custom Doctrine types, see the official documentation. For default types see the official documentation as well., (*2)

Usage

  1. Installation
  2. Custom type registration
  3. Map property as an enum
  4. Create enum
  5. Register subtype enum
  6. NULL is NULL, not Enum
  7. Understand the basics
  8. Exceptions philosophy

Installation

composer.phar require doctrineum/scalar

or manually edit composer.json at your project and "require": block (extend existing), (*3)

    "require": {
        "doctrineum/scalar": "dev-master"
    }

Custom type registration

By helper method, (*4)

ScalarEnum::registerSelf(); // quick self-registration

Or manually using "magic" class::class constant, (*5)

use Doctrineum\Scalar\ScalarEnumType;
// ...
Type::addType(ScalarEnumType::getTypeName(), ScalarEnumType::class);
Type::addType(BarScalarEnumType::getTypeName(), BarScalarEnumType::class);

Or manually by old fashion way, (*6)

<?php
// in bootstrapping code
// ...
use Doctrine\DBAL\Types\Type;
use Doctrineum\Scalar\ScalarEnumType;
// ...
// Register type
Type::addType(ScalarEnumType::getTypeName(), '\Doctrineum\ScalarEnumType');
Type::addType(BarScalarEnumType::getTypeName(), '\Foo\BarScalarEnumType');

Or if your project uses Symfony2, (*7)

# app/config/config.yml
doctrine:
    dbal:
        # ...
        types:
            scalar_enum: Doctrineum\Scalar\ScalarEnumType
            bar: Foo\BarScalarEnumType
            #...

Map property as an enum

<?php
class Foo
{
    /** @Column(type="scalar_enum") */
    protected $field;
}

Create enum

<?php
use Doctrineum\Scalar\ScalarEnum;
$enum = ScalarEnum::getEnum('foo bar');

Register subtype enum

You can register infinite number of enums, which are built according to a regexp of your choice., (*8)

<?php
use Doctrineum\Scalar\ScalarEnumType;
ScalarEnumType::addSubTypeEnum('\Foo\Bar\YourSubTypeEnum', '~get me different enum for this value~');
// ...
$enum = $ScalarEnumType->convertToPHPValue('foo');
get_class($enum) === '\Doctrineum\Scalar\ScalarEnum'; // true
get_class($enum) === '\Foo\Bar\YourSubTypeEnum'; // false
$byRegexpDeterminedEnum = $ScalarEnumType->convertToPHPValue('And now get me different enum for this value.');
get_class($byRegexpDeterminedEnum) === '\Foo\Bar\YourSubTypeEnum'; // true

NULL is NULL, Enum can not hold it

You can not create ScalarEnum with NULL value. Just use NULL directly for such column value., (*9)

Beware on using subtypes only when main enum is an abstract class. You have to resolve from-database-NULL->to-PHP-value conversion, or register subtype explicitly for NULL value (empty string respectively), otherwise fatal error by abstract class instance creation occurs., (*10)

Understand the basics

There are two roles - the factory and the value., (*11)

  • ScalarEnumType is the factory (as part of the Doctrine\DBAL\Types\Type family), building an ScalarEnum by following ScalarEnumType rules.
  • ScalarEnum is the value holder, de facto singleton, represented by a class. And class, as you know, can do a lot of things, which makes enum more sexy then whole scalar value.
  • Subtype is an ScalarEnumType, but ruled not just by type, but also by current value itself. One type can has any number of subtypes, in dependence on your imagination and used enum values.
Exceptions philosophy

Doctrineum adopts Granam exception hierarchy ideas. That means every exceptionable state is probably by a logic mistake, rather than a runtime situation., (*12)

The Versions

18/07 2018

dev-master

9999999-dev

Enumeration types and value dependent sub-types for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

18/07 2018

3.1.x-dev

3.1.9999999.9999999-dev

Enumeration types and value dependent sub-types for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

18/07 2018

3.1.2

3.1.2.0

Enumeration types and value dependent sub-types for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

18/07 2018

3.1.1

3.1.1.0

Enumeration types and value dependent sub-types for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

17/07 2018

3.1.0

3.1.0.0

Enumeration types and value dependent sub-types for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

19/02 2017

3.0.x-dev

3.0.9999999.9999999-dev

Enumeration types and value dependent sub-types for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

19/02 2017

3.0.1

3.0.1.0

Enumeration types and value dependent sub-types for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

19/02 2017

3.0.0

3.0.0.0

Enumeration types and value dependent sub-types for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

02/11 2015

2.0.x-dev

2.0.9999999.9999999-dev

Enumeration type and value dependent sub-type for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

02/11 2015

2.0.1

2.0.1.0

Enumeration type and value dependent sub-type for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

22/07 2015

2.0.0

2.0.0.0

Enumeration type and value dependent sub-type for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

16/07 2015

1.0.x-dev

1.0.9999999.9999999-dev

Enumeration type and value dependent sub-type for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

16/07 2015

1.0.4

1.0.4.0

Enumeration type and value dependent sub-type for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

16/07 2015

1.1.x-dev

1.1.9999999.9999999-dev

Enumeration type and value dependent sub-type for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

16/07 2015

1.1.3

1.1.3.0

Enumeration type and value dependent sub-type for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

28/05 2015

1.1.2

1.1.2.0

Enumeration type and value dependent sub-type for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

21/05 2015

1.1.1

1.1.1.0

Enumeration type and value dependent sub-type for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

21/05 2015

1.1.0

1.1.0.0

Enumeration type and value dependent sub-type for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

13/05 2015

1.0.3

1.0.3.0

Enumeration type and value dependent sub-type for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

10/05 2015

1.0.2

1.0.2.0

Enumeration type and value dependent sub-type for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

06/05 2015

1.0.1

1.0.1.0

Enumeration type and value dependent sub-type for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum

25/03 2015

1.0.0

1.0.0.0

Enumeration type and value dependent sub-type for Doctrine

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine enum sub-type value-dependent enum self-typed enum