phariscope/doctrineenumtype

A way to create quickly and well tested doctrine type for your php enums

v0.0.4 2024-05-03 07:42 UTC

This package is auto-updated.

Last update: 2024-05-03 09:27:17 UTC


README

To persist php enum fields with Doctrine, you have to create doctrine an enum type class for each enum.

To keep it easy and reduce the amount of tests use EnumType.

Installation

composer require phariscope/doctrineenumtype

Usage

Create your enum object as usual. For example:

enum Example: string
{
    case EXAMPLE = 'EXAMPLE';
    case FAKE = 'FAKE';
    case REAL = 'REAL';
}

Create your doctrine enum type test. For example:

class ExampleTypeTest extends TestCase
{
    public function testGetName(): void
    {
        $this->assertEquals('enum_example', (new ExampleType())->getName());
    }

Your production code source will be very short and look like to this:

use Phariscope\DoctrineEnumType\EnumType;

class ExampleType extends EnumType
{
    protected string $name = "enum_example";
    protected string $className = EnumExample::class;
}

Now you can use this new 'ExampleType' with Doctrine ORM.

<field name="myExample" type="enum_example" column="my_example"/>

of course don't forget to declare this new type to doctrine (Type::addType("enum_example", ExampleType::class) somewhere in your application).

To Contribute to phariscope/DoctrineEnumType

Requirements

  • docker
  • git

Install

git clone git@github.com:phariscope/DoctrineEnumType.git
cd DoctrineEnumType
./install

Installation will load php docker image and composer install, so all commands contained in the 'bin' folder will be easily accessbile.

Example:

bin/php -v

Unit test

bin/phpunit

Using Test-Driven Development (TDD) principles (thanks to Kent Beck and others), following good practices (thanks to Uncle Bob and others).

Quality

  • phpcs PSR12
  • phpstan level 9
  • coverage 100%
  • infection MSI >99%

Quick check with:

./codecheck

Check coverage with:

bin/phpunit --coverage-html var

and view 'var/index.html' with your browser

Check infection with:

bin/infection

and view 'var/infection.html' with your browser