diff --git a/Form/BaseAbstractType.php b/Form/BaseAbstractType.php index 7be1e7d..233f0f5 100644 --- a/Form/BaseAbstractType.php +++ b/Form/BaseAbstractType.php @@ -19,9 +19,7 @@ use Symfony\Component\OptionsResolver\OptionsResolverInterface; */ abstract class BaseAbstractType extends AbstractType { - protected $options = array( - 'name' => '', - ); + protected $options = array(); public function __construct($mergeOptions = null) { @@ -63,17 +61,11 @@ abstract class BaseAbstractType extends AbstractType $resolver->setDefaults($this->options); } - // BC for SF < 2.7 - public function setDefaultOptions(OptionsResolverInterface $resolver) - { - $this->configureOptions($resolver); - } - /** * {@inheritdoc} */ public function getName() { - return $this->getOption('name'); + return get_class($this); } } diff --git a/Form/Type/ModelType.php b/Form/Type/ModelType.php index 132e694..22373a5 100644 --- a/Form/Type/ModelType.php +++ b/Form/Type/ModelType.php @@ -14,6 +14,7 @@ namespace Propel\Bundle\PropelBundle\Form\Type; use Propel\Bundle\PropelBundle\Form\ChoiceList\ModelChoiceList; use Propel\Bundle\PropelBundle\Form\DataTransformer\CollectionToArrayTransformer; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -101,19 +102,24 @@ class ModelType extends AbstractType )); } - // BC for SF < 2.7 - public function setDefaultOptions(OptionsResolverInterface $resolver) - { - $this->configureOptions($resolver); - } - + /** + * {@inheritdoc} + */ public function getParent() { - return 'choice'; + return ChoiceType::class; + } + + /** + * {@inheritdoc} + */ + public function getBlockPrefix() + { + return 'model'; } public function getName() { - return 'model'; + return $this->getBlockPrefix(); } } diff --git a/Form/Type/TranslationCollectionType.php b/Form/Type/TranslationCollectionType.php index f48d752..af80f54 100644 --- a/Form/Type/TranslationCollectionType.php +++ b/Form/Type/TranslationCollectionType.php @@ -12,11 +12,11 @@ namespace Propel\Bundle\PropelBundle\Form\Type; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\CollectionType; use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Propel\Bundle\PropelBundle\Form\EventListener\TranslationCollectionFormListener; -use Symfony\Component\OptionsResolver\OptionsResolverInterface; /** * form type for i18n-columns in propel @@ -30,30 +30,17 @@ class TranslationCollectionType extends AbstractType */ public function buildForm(FormBuilderInterface $builder, array $options) { - if (!isset($options['options']['data_class']) || null === $options['options']['data_class']) { + if (!isset($options['entry_options']['data_class']) || null === $options['entry_options']['data_class']) { throw new MissingOptionsException('data_class must be set'); } - if (!isset($options['options']['columns']) || null === $options['options']['columns']) { + if (!isset($options['entry_options']['columns']) || null === $options['entry_options']['columns']) { throw new MissingOptionsException('columns must be set'); } - $listener = new TranslationCollectionFormListener($options['languages'], $options['options']['data_class']); + $listener = new TranslationCollectionFormListener($options['languages'], $options['entry_options']['data_class']); $builder->addEventSubscriber($listener); } - public function getParent() - { - return 'collection'; - } - - /** - * {@inheritdoc} - */ - public function getName() - { - return 'propel_translation_collection'; - } - /** * {@inheritdoc} */ @@ -64,19 +51,34 @@ class TranslationCollectionType extends AbstractType )); $resolver->setDefaults(array( - 'type' => 'propel_translation', + 'entry_type' => TranslationType::class, 'allow_add' => false, 'allow_delete' => false, - 'options' => array( + 'entry_options' => array( 'data_class' => null, 'columns' => null ) )); } - // BC for SF < 2.7 - public function setDefaultOptions(OptionsResolverInterface $resolver) + /** + * {@inheritdoc} + */ + public function getParent() { - $this->configureOptions($resolver); + return CollectionType::class; + } + + /** + * {@inheritdoc} + */ + public function getBlockPrefix() + { + return 'propel_translation_collection'; + } + + public function getName() + { + return $this->getBlockPrefix(); } } diff --git a/Form/Type/TranslationType.php b/Form/Type/TranslationType.php index 81d87a5..d7a248d 100644 --- a/Form/Type/TranslationType.php +++ b/Form/Type/TranslationType.php @@ -34,14 +34,6 @@ class TranslationType extends AbstractType ); } - /** - * {@inheritdoc} - */ - public function getName() - { - return 'propel_translation'; - } - /** * {@inheritdoc} */ @@ -53,9 +45,16 @@ class TranslationType extends AbstractType )); } - // BC for SF < 2.7 - public function setDefaultOptions(OptionsResolverInterface $resolver) + /** + * {@inheritdoc} + */ + public function getBlockPrefix() { - $this->configureOptions($resolver); + return 'propel_translation'; + } + + public function getName() + { + return $this->getBlockPrefix(); } } diff --git a/Form/TypeGuesser.php b/Form/TypeGuesser.php index 2ce1a4d..ea8d3ce 100644 --- a/Form/TypeGuesser.php +++ b/Form/TypeGuesser.php @@ -11,8 +11,18 @@ namespace Propel\Bundle\PropelBundle\Form; +use Propel\Bundle\PropelBundle\Form\Type\ModelType; use Propel\Runtime\Map\RelationMap; use Propel\Generator\Model\PropelTypes; +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; +use Symfony\Component\Form\Extension\Core\Type\DateTimeType; +use Symfony\Component\Form\Extension\Core\Type\DateType; +use Symfony\Component\Form\Extension\Core\Type\IntegerType; +use Symfony\Component\Form\Extension\Core\Type\NumberType; +use Symfony\Component\Form\Extension\Core\Type\TextareaType; +use Symfony\Component\Form\Extension\Core\Type\TextType; +use Symfony\Component\Form\Extension\Core\Type\TimeType; use Symfony\Component\Form\FormTypeGuesserInterface; use Symfony\Component\Form\Guess\Guess; use Symfony\Component\Form\Guess\TypeGuess; @@ -33,27 +43,27 @@ class TypeGuesser implements FormTypeGuesserInterface public function guessType($class, $property) { if (!$table = $this->getTable($class)) { - return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE); + return new TypeGuess(TextType::class, array(), Guess::LOW_CONFIDENCE); } foreach ($table->getRelations() as $relation) { if ($relation->getType() === RelationMap::MANY_TO_ONE) { if (strtolower($property) === strtolower($relation->getName())) { - return new TypeGuess('model', array( + return new TypeGuess(ModelType::class, array( 'class' => $relation->getForeignTable()->getClassName(), 'multiple' => false, ), Guess::HIGH_CONFIDENCE); } } elseif ($relation->getType() === RelationMap::ONE_TO_MANY) { if (strtolower($property) === strtolower($relation->getPluralName())) { - return new TypeGuess('model', array( + return new TypeGuess(ModelType::class, array( 'class' => $relation->getForeignTable()->getClassName(), 'multiple' => true, ), Guess::HIGH_CONFIDENCE); } } elseif ($relation->getType() === RelationMap::MANY_TO_MANY) { if (strtolower($property) == strtolower($relation->getPluralName())) { - return new TypeGuess('model', array( + return new TypeGuess(ModelType::class, array( 'class' => $relation->getLocalTable()->getClassName(), 'multiple' => true, ), Guess::HIGH_CONFIDENCE); @@ -62,32 +72,32 @@ class TypeGuesser implements FormTypeGuesserInterface } if (!$column = $this->getColumn($class, $property)) { - return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE); + return new TypeGuess(TextType::class, array(), Guess::LOW_CONFIDENCE); } switch ($column->getType()) { case PropelTypes::BOOLEAN: case PropelTypes::BOOLEAN_EMU: - return new TypeGuess('checkbox', array(), Guess::HIGH_CONFIDENCE); + return new TypeGuess(CheckboxType::class, array(), Guess::HIGH_CONFIDENCE); case PropelTypes::TIMESTAMP: case PropelTypes::BU_TIMESTAMP: - return new TypeGuess('datetime', array(), Guess::HIGH_CONFIDENCE); + return new TypeGuess(DateTimeType::class, array(), Guess::HIGH_CONFIDENCE); case PropelTypes::DATE: case PropelTypes::BU_DATE: - return new TypeGuess('date', array(), Guess::HIGH_CONFIDENCE); + return new TypeGuess(DateType::class, array(), Guess::HIGH_CONFIDENCE); case PropelTypes::TIME: - return new TypeGuess('time', array(), Guess::HIGH_CONFIDENCE); + return new TypeGuess(TimeType::class, array(), Guess::HIGH_CONFIDENCE); case PropelTypes::FLOAT: case PropelTypes::REAL: case PropelTypes::DOUBLE: case PropelTypes::DECIMAL: - return new TypeGuess('number', array(), Guess::MEDIUM_CONFIDENCE); + return new TypeGuess(NumberType::class, array(), Guess::MEDIUM_CONFIDENCE); case PropelTypes::TINYINT: case PropelTypes::SMALLINT: case PropelTypes::INTEGER: case PropelTypes::BIGINT: case PropelTypes::NUMERIC: - return new TypeGuess('integer', array(), Guess::MEDIUM_CONFIDENCE); + return new TypeGuess(IntegerType::class, array(), Guess::MEDIUM_CONFIDENCE); case PropelTypes::ENUM: case PropelTypes::CHAR: if ($column->getValueSet()) { @@ -95,17 +105,17 @@ class TypeGuesser implements FormTypeGuesserInterface $choices = $column->getValueSet(); $labels = array_map('ucfirst', $choices); - return new TypeGuess('choice', array('choices' => array_combine($choices, $labels)), Guess::MEDIUM_CONFIDENCE); + return new TypeGuess(ChoiceType::class, array('choices' => array_combine($choices, $labels)), Guess::MEDIUM_CONFIDENCE); } case PropelTypes::VARCHAR: - return new TypeGuess('text', array(), Guess::MEDIUM_CONFIDENCE); + return new TypeGuess(TextType::class, array(), Guess::MEDIUM_CONFIDENCE); case PropelTypes::LONGVARCHAR: case PropelTypes::BLOB: case PropelTypes::CLOB: case PropelTypes::CLOB_EMU: - return new TypeGuess('textarea', array(), Guess::MEDIUM_CONFIDENCE); + return new TypeGuess(TextareaType::class, array(), Guess::MEDIUM_CONFIDENCE); default: - return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE); + return new TypeGuess(TextType::class, array(), Guess::LOW_CONFIDENCE); } } diff --git a/Tests/Form/Type/TranslationCollectionTypeTest.php b/Tests/Form/Type/TranslationCollectionTypeTest.php index ca95373..6f03824 100644 --- a/Tests/Form/Type/TranslationCollectionTypeTest.php +++ b/Tests/Form/Type/TranslationCollectionTypeTest.php @@ -11,6 +11,7 @@ namespace Propel\Bundle\PropelBundle\Tests\Form\Form\Type; +use Propel\Bundle\PropelBundle\Form\Type\TranslationCollectionType; use Propel\Bundle\PropelBundle\Tests\Fixtures\Item; use Propel\Bundle\PropelBundle\Form\PropelExtension; use Propel\Bundle\PropelBundle\Tests\Fixtures\TranslatableItemI18n; @@ -45,9 +46,9 @@ class TranslationCollectionTypeTest extends TypeTestCase 'data_class' => self::TRANSLATION_CLASS )); - $builder->add('translatableItemI18ns', 'propel_translation_collection', array( + $builder->add('translatableItemI18ns', TranslationCollectionType::class, array( 'languages' => array('en', 'fr'), - 'options' => array( + 'entry_options' => array( 'data_class' => self::TRANSLATABLE_I18N_CLASS, 'columns' => array('value', 'value2' => array('label' => 'Label', 'type' => 'textarea')) ) @@ -81,9 +82,9 @@ class TranslationCollectionTypeTest extends TypeTestCase $builder = $this->factory->createBuilder('form', null, array( 'data_class' => self::TRANSLATION_CLASS )); - $builder->add('translatableItemI18ns', 'propel_translation_collection', array( + $builder->add('translatableItemI18ns', TranslationCollectionType::class, array( 'languages' => array('en', 'fr'), - 'options' => array( + 'entry_options' => array( 'data_class' => self::TRANSLATABLE_I18N_CLASS, 'columns' => array('value', 'value2' => array('label' => 'Label', 'type' => 'textarea')) ) @@ -105,9 +106,9 @@ class TranslationCollectionTypeTest extends TypeTestCase $builder = $this->factory->createBuilder('form', null, array( 'data_class' => self::NON_TRANSLATION_CLASS )); - $builder->add('value', 'propel_translation_collection', array( + $builder->add('value', TranslationCollectionType::class, array( 'languages' => array('en', 'fr'), - 'options' => array( + 'entry_options' => array( 'data_class' => self::TRANSLATABLE_I18N_CLASS, 'columns' => array('value', 'value2' => array('label' => 'Label', 'type' => 'textarea')) ) @@ -122,9 +123,9 @@ class TranslationCollectionTypeTest extends TypeTestCase */ public function testNoDataClassAdded() { - $this->factory->createNamed('itemI18ns', 'propel_translation_collection', null, array( + $this->factory->createNamed('itemI18ns', TranslationCollectionType::class, null, array( 'languages' => array('en', 'fr'), - 'options' => array( + 'entry_options' => array( 'columns' => array('value', 'value2') ) )); @@ -135,8 +136,8 @@ class TranslationCollectionTypeTest extends TypeTestCase */ public function testNoLanguagesAdded() { - $this->factory->createNamed('itemI18ns', 'propel_translation_collection', null, array( - 'options' => array( + $this->factory->createNamed('itemI18ns', TranslationCollectionType::class, null, array( + 'entry_options' => array( 'data_class' => self::TRANSLATABLE_I18N_CLASS, 'columns' => array('value', 'value2') ) @@ -148,9 +149,9 @@ class TranslationCollectionTypeTest extends TypeTestCase */ public function testNoColumnsAdded() { - $this->factory->createNamed('itemI18ns', 'propel_translation_collection', null, array( + $this->factory->createNamed('itemI18ns', TranslationCollectionType::class, null, array( 'languages' => array('en', 'fr'), - 'options' => array( + 'entry_options' => array( 'data_class' => self::TRANSLATABLE_I18N_CLASS ) )); diff --git a/Tests/Form/TypeGuesserTest.php b/Tests/Form/TypeGuesserTest.php index 046b7a7..12b7994 100644 --- a/Tests/Form/TypeGuesserTest.php +++ b/Tests/Form/TypeGuesserTest.php @@ -11,8 +11,14 @@ namespace Propel\Bundle\PropelBundle\Tests\Form; +use Propel\Bundle\PropelBundle\Form\Type\ModelType; use Propel\Bundle\PropelBundle\Form\TypeGuesser; use Propel\Bundle\PropelBundle\Tests\TestCase; +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; +use Symfony\Component\Form\Extension\Core\Type\DateTimeType; +use Symfony\Component\Form\Extension\Core\Type\IntegerType; +use Symfony\Component\Form\Extension\Core\Type\NumberType; +use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Guess\Guess; class TypeGuesserTest extends TestCase @@ -80,7 +86,7 @@ class TypeGuesserTest extends TestCase $value = $this->guesser->guessType(self::UNKNOWN_CLASS_NAME, 'property'); $this->assertNotNull($value); - $this->assertEquals('text', $value->getType()); + $this->assertEquals(TextType::class, $value->getType()); $this->assertEquals(Guess::LOW_CONFIDENCE, $value->getConfidence()); } @@ -89,7 +95,7 @@ class TypeGuesserTest extends TestCase $value = $this->guesser->guessType(self::CLASS_NAME, 'property'); $this->assertNotNull($value); - $this->assertEquals('text', $value->getType()); + $this->assertEquals(TextType::class, $value->getType()); $this->assertEquals(Guess::LOW_CONFIDENCE, $value->getConfidence()); } @@ -104,7 +110,7 @@ class TypeGuesserTest extends TestCase $this->assertEquals($type, $value->getType()); $this->assertEquals($confidence, $value->getConfidence()); - if ($type === 'model') { + if ($type === ModelType::class) { $options = $value->getOptions(); $this->assertSame($multiple, $options['multiple']); @@ -114,16 +120,16 @@ class TypeGuesserTest extends TestCase public static function dataProviderForGuessType() { return array( - array('is_active', 'checkbox', Guess::HIGH_CONFIDENCE), - array('enabled', 'checkbox', Guess::HIGH_CONFIDENCE), - array('id', 'integer', Guess::MEDIUM_CONFIDENCE), - array('value', 'text', Guess::MEDIUM_CONFIDENCE), - array('price', 'number', Guess::MEDIUM_CONFIDENCE), - array('updated_at', 'datetime', Guess::HIGH_CONFIDENCE), + array('is_active', CheckboxType::class, Guess::HIGH_CONFIDENCE), + array('enabled', CheckboxType::class, Guess::HIGH_CONFIDENCE), + array('id', IntegerType::class, Guess::MEDIUM_CONFIDENCE), + array('value', TextType::class, Guess::MEDIUM_CONFIDENCE), + array('price', NumberType::class, Guess::MEDIUM_CONFIDENCE), + array('updated_at', DateTimeType::class, Guess::HIGH_CONFIDENCE), - array('Authors', 'model', Guess::HIGH_CONFIDENCE, true), - array('Resellers', 'model', Guess::HIGH_CONFIDENCE, true), - array('MainAuthor', 'model', Guess::HIGH_CONFIDENCE, false), + array('Authors', ModelType::class, Guess::HIGH_CONFIDENCE, true), + array('Resellers', ModelType::class, Guess::HIGH_CONFIDENCE, true), + array('MainAuthor', ModelType::class, Guess::HIGH_CONFIDENCE, false), ); } }