greg0ire / enum-bundle
Integrates greg0ire/enum in a Symfony2 project
Installs: 8 513
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 2
Forks: 2
Open Issues: 1
Requires
- greg0ire/enum: ^2.0
- symfony/form: ~2.3|^3.0
- symfony/http-kernel: ~2.3|^3.0
Requires (Dev)
- phpunit/phpunit: ~4.1
- symfony/phpunit-bridge: ~2.7|^3.0
README
This package is abandoned in favor of greg0ire/enum
EnumBundle
Integrates greg0ire/enum in a Symfony2 project. This actually not a real bundle yet but :
- it has a dependency on symfony/form
- it could become a real bundle someday, if something needs to be configured
Installation
composer require greg0ire/enum-bundle
Usage
The bundle provides its own BaseEnum
class. It inherits from greg0ire/enum
's
BaseEnum
class and provides an additional method, getChoices()
, which
is meant to be used as value for the choices
option of a choice widget.
It has a mandatory parameter, which is a sprintf
format string and let's you choose
how to generate your labels.
<?php use Greg0ire\EnumBundle\BaseEnum; final class ColorType extends BaseEnum { const BLACK_WHITE = 'black-white', COLOR = 'color', COLORIZED = 'colorized' ; }
A few moments later, in another file…
<?php class MyType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add( 'aspect_ratio', 'choice', array('choices' => ColorType::getChoices('color_type_%s')) ); } }
You then need to create translations for :
color_type_black-white
color_type_color
color_type_colorized
The first argument to getChoices()
is optional, and the value will be used
directly as a label should you choose not to specify it. This makes sense if
you decide to have a translation catalogue just for your enumeration.
The second argument, choicesAsValues
only takes effect if your symfony version
is < 3.0 and >= 2.7. It defaults to false
for the moment, so that B.C. is
kept. If you use symfony >= 2.8 and < 3.0, you will get a deprecation notice
unless you set it to true and set choices_as_values
option to true
.