adrenalinkin/enum-property-bundle
Symfony bundle integrating EnumMapper: adds Twig filters/functions to convert enum DB values to human labels and back, fetch full enum maps, and provides validation support for enum properties. Install via Composer and enable the bundle in AppKernel.
Installation
composer require adrenalinkin/enum-property-bundle
Ensure your project uses Symfony 4+ (or 3.x with AppKernel.php).
Enable the Bundle
Add to config/bundles.php (Symfony 4+) or app/AppKernel.php (Symfony 3):
Linkin\Bundle\EnumPropertyBundle\LinkinEnumPropertyBundle::class => ['all' => true],
First Use Case
Create a custom AbstractEnumMapper (e.g., src/Mapper/GenderMapper.php):
namespace App\Mapper;
use Linkin\Component\EnumMapper\Mapper\AbstractEnumMapper;
class GenderMapper extends AbstractEnumMapper
{
const DB_UNDEFINED = 0;
const DB_MALE = 1;
const DB_FEMALE = 2;
protected function getEnumMap(): array
{
return [
self::DB_UNDEFINED => 'undefined',
self::DB_MALE => 'male',
self::DB_FEMALE => 'female',
];
}
}
Register the Mapper
Add to services.yaml:
services:
App\Mapper\GenderMapper:
tags: ['linkin.enum_mapper']
Use in Twig
{{ dump(app.enum_mapper('App\Mapper\GenderMapper').map(1)) }} {# Outputs: "male" #}
Form Integration
Use the EnumType form field for enum-backed properties:
use Linkin\Bundle\EnumPropertyBundle\Form\Type\EnumType;
$builder->add('gender', EnumType::class, [
'mapper' => 'App\Mapper\GenderMapper',
'choices_as_values' => true,
]);
Validation
Leverage the Enum constraint:
use Linkin\Bundle\EnumPropertyBundle\Validator\Constraints\Enum;
/**
* @Assert\Constraint(Enum::class, mapper="App\Mapper\GenderMapper")
*/
private $gender;
Twig Filters Map values directly in templates:
{{ 1 | enum_map('App\Mapper\GenderMapper') }} {# "male" #}
{{ 'male' | enum_unmap('App\Mapper\GenderMapper') }} {# 1 #}
Dependency Injection
Inject the EnumMapperRegistry service for dynamic mapping:
use Linkin\Component\EnumMapper\Registry\EnumMapperRegistry;
public function __construct(private EnumMapperRegistry $registry) {}
public function resolveGender(int $value): string {
return $this->registry->get('App\Mapper\GenderMapper')->map($value);
}
linkin.enum_mapper to auto-register them.config/packages/linkin_enum_property.yaml:
linkin_enum_property:
mapper_paths: ['%kernel.project_dir%/src/Mapper']
EnumType in entities for database storage:
use Linkin\Bundle\EnumPropertyBundle\Doctrine\Types\EnumType;
/**
* @ORM\Column(type="enum")
*/
private $gender;
Mapper Registration
linkin.enum_mapper tag is applied or use EnumMapperRegistry::add().Circular Dependencies
Kernel::boot().Case Sensitivity
enum_unmap() fails if enum keys differ in case (e.g., 'Male' vs 'male').getEnumMap():
return array_change_key_case($this->getRawEnumMap(), CASE_LOWER);
Doctrine Migrations
EnumType may not generate correct SQL for legacy databases.EnumType for custom DDL.$this->registry->getAll(); // Debug available mappers
php bin/console cache:clear) if filters fail post-install.validator:validate to test constraints:
php bin/console validator:validate App\Entity\User gender
Custom Mappers
Extend AbstractEnumMapper for complex logic:
class StatusMapper extends AbstractEnumMapper {
protected function getEnumMap(): array {
return [
self::DB_PENDING => new Status('pending', 'Pending Review'),
// Use objects for rich data
];
}
}
Override Twig Filters
Extend the bundle’s TwigExtension to add custom filters:
use Linkin\Bundle\EnumPropertyBundle\Twig\EnumPropertyExtension;
class CustomEnumExtension extends EnumPropertyExtension {
public function getFilters() {
return array_merge(parent::getFilters(), [
new \Twig\TwigFilter('custom_map', [$this, 'customMap']),
]);
}
}
Validator Extensions Add custom validation logic:
use Linkin\Bundle\EnumPropertyBundle\Validator\Constraints\Enum;
class CustomEnum extends Enum {
public function validatedBy() {
return static::class . 'Validator';
}
}
Performance
services:
App\Mapper\GenderMapper:
tags: ['linkin.enum_mapper']
public: false
synthetic: true
How can I help you explore Laravel packages today?