diff --git a/.travis.yml b/.travis.yml index 34ed55fc..4d743018 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ branches: only: - master - 1.x + - 0.x language: php diff --git a/composer.json b/composer.json index 6a920fa0..9c2d607d 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ "twig/twig": "^2.0 || ^3.0" }, "conflict": { - "sonata-project/core-bundle": ">=3.13" + "sonata-project/core-bundle": "<3.19" }, "require-dev": { "doctrine/persistence": "^1.1", @@ -56,7 +56,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.x-dev" + "dev-master": "0.x-dev" } }, "autoload": { diff --git a/src/Bridge/Symfony/Bundle/SonataFormBundle.php b/src/Bridge/Symfony/Bundle/SonataFormBundle.php index 96db3603..cbea288d 100644 --- a/src/Bridge/Symfony/Bundle/SonataFormBundle.php +++ b/src/Bridge/Symfony/Bundle/SonataFormBundle.php @@ -19,7 +19,7 @@ final class SonataFormBundle extends Bundle { /** - * {@inheritdoc} + * @return string */ public function getPath() { @@ -27,7 +27,7 @@ public function getPath() } /** - * {@inheritdoc} + * @return string */ protected function getContainerExtensionClass() { diff --git a/src/DataTransformer/BooleanTypeToBooleanTransformer.php b/src/DataTransformer/BooleanTypeToBooleanTransformer.php index 4e60c580..a2dd5490 100644 --- a/src/DataTransformer/BooleanTypeToBooleanTransformer.php +++ b/src/DataTransformer/BooleanTypeToBooleanTransformer.php @@ -16,9 +16,12 @@ use Sonata\Form\Type\BooleanType; use Symfony\Component\Form\DataTransformerInterface; -final class BooleanTypeToBooleanTransformer implements DataTransformerInterface +/** + * @final since sonata-project/form-extensions 0.x + */ +class BooleanTypeToBooleanTransformer implements DataTransformerInterface { - public function transform($value): ?int + public function transform($value) { if (true === $value or BooleanType::TYPE_YES === (int) $value) { return BooleanType::TYPE_YES; @@ -29,7 +32,7 @@ public function transform($value): ?int return null; } - public function reverseTransform($value): ?bool + public function reverseTransform($value) { if (BooleanType::TYPE_YES === $value) { return true; diff --git a/src/Date/MomentFormatConverter.php b/src/Date/MomentFormatConverter.php index 9502f29d..bd811b6e 100644 --- a/src/Date/MomentFormatConverter.php +++ b/src/Date/MomentFormatConverter.php @@ -56,7 +56,7 @@ class MomentFormatConverter * * @return string Moment.js date format */ - public function convert(string $format): string + public function convert($format) { $size = \strlen($format); diff --git a/src/EventListener/FixCheckboxDataListener.php b/src/EventListener/FixCheckboxDataListener.php index acbf6b48..9f0ceb7c 100644 --- a/src/EventListener/FixCheckboxDataListener.php +++ b/src/EventListener/FixCheckboxDataListener.php @@ -24,15 +24,20 @@ * returning true value when the form is bind. * * @author Sylvain Rascar + * + * @final since sonata-project/form-extensions 0.x */ -final class FixCheckboxDataListener implements EventSubscriberInterface +class FixCheckboxDataListener implements EventSubscriberInterface { - public static function getSubscribedEvents(): array + /** + * @return array + */ + public static function getSubscribedEvents() { return [FormEvents::PRE_SUBMIT => 'preSubmit']; } - public function preSubmit(FormEvent $event): void + public function preSubmit(FormEvent $event) { $data = $event->getData(); $transformers = $event->getForm()->getConfig()->getViewTransformers(); diff --git a/src/EventListener/ResizeFormListener.php b/src/EventListener/ResizeFormListener.php index c7d13c03..28966bbc 100644 --- a/src/EventListener/ResizeFormListener.php +++ b/src/EventListener/ResizeFormListener.php @@ -22,8 +22,10 @@ * Resize a collection form element based on the data sent from the client. * * @author Bernhard Schussek + * + * @final since sonata-project/form-extensions 0.x */ -final class ResizeFormListener implements EventSubscriberInterface +class ResizeFormListener implements EventSubscriberInterface { /** * @var string @@ -67,7 +69,7 @@ public function __construct( $this->preSubmitDataCallback = $preSubmitDataCallback; } - public static function getSubscribedEvents(): array + public static function getSubscribedEvents() { return [ FormEvents::PRE_SET_DATA => 'preSetData', @@ -79,7 +81,7 @@ public static function getSubscribedEvents(): array /** * @throws UnexpectedTypeException */ - public function preSetData(FormEvent $event): void + public function preSetData(FormEvent $event) { $form = $event->getForm(); $data = $event->getData(); @@ -111,7 +113,7 @@ public function preSetData(FormEvent $event): void /** * @throws UnexpectedTypeException */ - public function preSubmit(FormEvent $event): void + public function preSubmit(FormEvent $event) { if (!$this->resizeOnSubmit) { return; @@ -158,7 +160,7 @@ public function preSubmit(FormEvent $event): void /** * @throws UnexpectedTypeException */ - public function onSubmit(FormEvent $event): void + public function onSubmit(FormEvent $event) { if (!$this->resizeOnSubmit) { return; diff --git a/src/Fixtures/StubFilesystemLoader.php b/src/Fixtures/StubFilesystemLoader.php new file mode 100644 index 00000000..7b3b4167 --- /dev/null +++ b/src/Fixtures/StubFilesystemLoader.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\Form\Fixtures; + +use Twig\Loader\FilesystemLoader; + +final class StubFilesystemLoader extends FilesystemLoader +{ + protected function findTemplate($name, $throw = true) + { + // strip away bundle name + $parts = explode(':', $name); + + return parent::findTemplate(end($parts), $throw); + } +} diff --git a/src/Serializer/BaseSerializerHandler.php b/src/Serializer/BaseSerializerHandler.php index f47da0a9..a511215e 100644 --- a/src/Serializer/BaseSerializerHandler.php +++ b/src/Serializer/BaseSerializerHandler.php @@ -38,17 +38,26 @@ public function __construct(ManagerInterface $manager) $this->manager = $manager; } - final public static function setFormats(array $formats): void + /** + * @param string[] $formats + */ + final public static function setFormats($formats) { static::$formats = $formats; } - final public static function addFormat(string $format): void + /** + * @param string $format + */ + final public static function addFormat($format) { static::$formats[] = $format; } - public static function getSubscribingMethods(): array + /** + * @return array[] + */ + public static function getSubscribingMethods() { $type = static::getType(); $methods = []; @@ -74,8 +83,12 @@ public static function getSubscribingMethods(): array /** * Serialize data object to id. + * + * @param object $data + * + * @return int|null */ - public function serializeObjectToId(VisitorInterface $visitor, object $data, array $type, Context $context): ?int + public function serializeObjectToId(VisitorInterface $visitor, $data, $type, Context $context) { $className = $this->manager->getClass(); @@ -88,8 +101,12 @@ public function serializeObjectToId(VisitorInterface $visitor, object $data, arr /** * Deserialize object from its id. + * + * @param int $data + * + * @return object|null */ - public function deserializeObjectFromId(VisitorInterface $visitor, int $data, array $type): ?object + public function deserializeObjectFromId(VisitorInterface $visitor, $data, array $type) { return $this->manager->findOneBy(['id' => $data]); } diff --git a/src/Serializer/SerializerHandlerInterface.php b/src/Serializer/SerializerHandlerInterface.php index 4f8c6525..cf0160b0 100644 --- a/src/Serializer/SerializerHandlerInterface.php +++ b/src/Serializer/SerializerHandlerInterface.php @@ -20,5 +20,8 @@ */ interface SerializerHandlerInterface extends SubscribingHandlerInterface { - public static function getType(): string; + /** + * @return string + */ + public static function getType(); } diff --git a/src/Test/AbstractWidgetTestCase.php b/src/Test/AbstractWidgetTestCase.php index bc39b5e8..b1183cf0 100644 --- a/src/Test/AbstractWidgetTestCase.php +++ b/src/Test/AbstractWidgetTestCase.php @@ -78,7 +78,7 @@ protected function getEnvironment(): Environment * * @return string[] */ - protected function getTemplatePaths(): array + protected function getTemplatePaths() { // this is an workaround for different composer requirements and different TwigBridge installation directories $twigPaths = array_filter([ @@ -89,7 +89,7 @@ protected function getTemplatePaths(): array // symfony/symfony (running from this bundle) __DIR__.'/../../vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form', // symfony/symfony (running from other bundles) - __DIR__.'/../../../../../symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form', + __DIR__.'/../../../../symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form', ], 'is_dir'); $twigPaths[] = __DIR__.'/../Bridge/Symfony/Resources/views/Form'; @@ -97,32 +97,43 @@ protected function getTemplatePaths(): array return $twigPaths; } - protected function getRenderingEngine(Environment $environment): TwigRendererEngine + protected function getRenderingEngine(Environment $environment) { return new TwigRendererEngine(['form_div_layout.html.twig'], $environment); } /** * Renders widget from FormView, in SonataAdmin context, with optional view variables $vars. Returns plain HTML. + * + * @return string */ - final protected function renderWidget(FormView $view, array $vars = []): string + final protected function renderWidget(FormView $view, array $vars = []) { return (string) $this->renderer->searchAndRenderBlock($view, 'widget', $vars); } /** * Helper method to strip newline and space characters from html string to make comparing easier. + * + * @param string $html + * + * @return string */ - final protected function cleanHtmlWhitespace(string $html): string + final protected function cleanHtmlWhitespace($html) { - return preg_replace_callback('/\s*>([^<]+)([^<]+)'.trim($value[1]).'<'; }, $html); } - final protected function cleanHtmlAttributeWhitespace(string $html): string + /** + * @param string $html + * + * @return string + */ + final protected function cleanHtmlAttributeWhitespace($html) { - return preg_replace_callback('~<([A-Z0-9]+) \K(.*?)>~i', static function (array $m): string { + return preg_replace_callback('~<([A-Z0-9]+) \K(.*?)>~i', static function ($m) { return preg_replace('~\s*~', '', $m[0]); }, $html); } diff --git a/src/Type/BaseDoctrineORMSerializationType.php b/src/Type/BaseDoctrineORMSerializationType.php index cc8af366..7dbc7d2b 100644 --- a/src/Type/BaseDoctrineORMSerializationType.php +++ b/src/Type/BaseDoctrineORMSerializationType.php @@ -78,7 +78,7 @@ public function __construct(MetadataFactoryInterface $metadataFactory, ManagerRe $this->identifierOverwrite = $identifierOverwrite; } - public function buildForm(FormBuilderInterface $builder, array $options): void + public function buildForm(FormBuilderInterface $builder, array $options) { $serializerMetadata = $this->metadataFactory->getMetadataForClass($this->class); @@ -147,7 +147,7 @@ public function getName() return $this->getBlockPrefix(); } - public function configureOptions(OptionsResolver $resolver): void + public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'data_class' => $this->class, diff --git a/src/Type/BasePickerType.php b/src/Type/BasePickerType.php index a725429a..38ea6d5b 100644 --- a/src/Type/BasePickerType.php +++ b/src/Type/BasePickerType.php @@ -46,13 +46,17 @@ abstract class BasePickerType extends AbstractType */ private $formatConverter; - public function __construct(MomentFormatConverter $formatConverter, $translator, ?RequestStack $requestStack = null) + /** + * @param LegacyTranslatorInterface|TranslatorInterface|null $translator + */ + public function __construct(MomentFormatConverter $formatConverter, $translator = null, ?RequestStack $requestStack = null) { - if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) { + if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface && null !== $translator) { throw new \InvalidArgumentException(sprintf( - 'Argument 2 should be an instance of %s or %s', + 'Argument 2 should be an instance of %s or %s or %s', LegacyTranslatorInterface::class, - TranslatorInterface::class + TranslatorInterface::class, + 'null' )); } @@ -65,7 +69,7 @@ public function __construct(MomentFormatConverter $formatConverter, $translator, } @trigger_error(sprintf( - 'Not passing the request stack as argument 3 to %s() is deprecated since sonata-project/form-extensions 1.2 and will be mandatory in 2.0.', + 'Not passing the request stack as argument 3 to %s() is deprecated since sonata-project/form-extensions 0.x and will be mandatory in 2.0.', __METHOD__ ), E_USER_DEPRECATED); } @@ -75,15 +79,23 @@ public function __construct(MomentFormatConverter $formatConverter, $translator, if ($translator instanceof LegacyTranslatorInterface) { $this->locale = $this->translator->getLocale(); - } else { + } elseif ($translator instanceof TranslatorInterface) { $this->locale = $this->getLocale($requestStack); + } else { + /* + * NEXT_MAJOR: remove this check + */ + @trigger_error( + 'Initializing '.__CLASS__.' without TranslatorInterface + is deprecated since 0.x and will be removed in 1.0.', + E_USER_DEPRECATED + ); + + $this->locale = \Locale::getDefault(); } } - /** - * {@inheritdoc} - */ - public function configureOptions(OptionsResolver $resolver): void + public function configureOptions(OptionsResolver $resolver) { $resolver->setNormalizer('format', function (Options $options, $format) { if (isset($options['date_format']) && \is_string($options['date_format'])) { @@ -112,7 +124,7 @@ public function configureOptions(OptionsResolver $resolver): void }); } - public function finishView(FormView $view, FormInterface $form, array $options): void + public function finishView(FormView $view, FormInterface $form, array $options) { $format = $options['format']; @@ -149,8 +161,10 @@ public function finishView(FormView $view, FormInterface $form, array $options): /** * Gets base default options for the date pickers. + * + * @return array */ - protected function getCommonDefaults(): array + protected function getCommonDefaults() { return [ 'widget' => 'single_text', diff --git a/src/Type/BaseStatusType.php b/src/Type/BaseStatusType.php index a3def1fb..7b1a4093 100644 --- a/src/Type/BaseStatusType.php +++ b/src/Type/BaseStatusType.php @@ -34,24 +34,40 @@ abstract class BaseStatusType extends AbstractType */ protected $name; - public function __construct(string $class, string $getter, string $name) + /** + * @param string $class + * @param string $getter + * @param string $name + */ + public function __construct($class, $getter, $name) { $this->class = $class; $this->getter = $getter; $this->name = $name; } - public function getParent(): string + /** + * @return string + */ + public function getParent() { return ChoiceType::class; } - public function getBlockPrefix(): string + /** + * @return string + */ + public function getBlockPrefix() { return $this->name; } - public function configureOptions(OptionsResolver $resolver): void + public function getName() + { + return $this->getBlockPrefix(); + } + + public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'choices' => \call_user_func([$this->class, $this->getter]), diff --git a/src/Type/BooleanType.php b/src/Type/BooleanType.php index ae9986eb..a7cc8da6 100644 --- a/src/Type/BooleanType.php +++ b/src/Type/BooleanType.php @@ -19,20 +19,23 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; -final class BooleanType extends AbstractType +/** + * @final since sonata-project/form-extensions 0.x + */ +class BooleanType extends AbstractType { public const TYPE_YES = 1; public const TYPE_NO = 2; - public function buildForm(FormBuilderInterface $builder, array $options): void + public function buildForm(FormBuilderInterface $builder, array $options) { if ($options['transform']) { $builder->addModelTransformer(new BooleanTypeToBooleanTransformer()); } } - public function configureOptions(OptionsResolver $resolver): void + public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'transform' => false, @@ -45,12 +48,26 @@ public function configureOptions(OptionsResolver $resolver): void ]); } - public function getParent(): string + /** + * @return string + */ + public function getParent() { return ChoiceType::class; } - public function getBlockPrefix(): string + /** + * @return string + */ + public function getName() + { + return $this->getBlockPrefix(); + } + + /** + * @return string + */ + public function getBlockPrefix() { return 'sonata_type_boolean'; } diff --git a/src/Type/CollectionType.php b/src/Type/CollectionType.php index 56122d89..03b72dab 100644 --- a/src/Type/CollectionType.php +++ b/src/Type/CollectionType.php @@ -21,9 +21,12 @@ use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; -final class CollectionType extends AbstractType +/** + * @final since sonata-project/form-extensions 0.x + */ +class CollectionType extends AbstractType { - public function buildForm(FormBuilderInterface $builder, array $options): void + public function buildForm(FormBuilderInterface $builder, array $options) { $builder->addEventSubscriber(new ResizeFormListener( $options['type'], @@ -33,13 +36,13 @@ public function buildForm(FormBuilderInterface $builder, array $options): void )); } - public function buildView(FormView $view, FormInterface $form, array $options): void + public function buildView(FormView $view, FormInterface $form, array $options) { $view->vars['btn_add'] = $options['btn_add']; $view->vars['btn_catalogue'] = $options['btn_catalogue']; } - public function configureOptions(OptionsResolver $resolver): void + public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'modifiable' => false, @@ -51,8 +54,19 @@ public function configureOptions(OptionsResolver $resolver): void ]); } - public function getBlockPrefix(): string + /** + * @return string + */ + public function getBlockPrefix() { return 'sonata_type_collection'; } + + /** + * @return string + */ + public function getName() + { + return $this->getBlockPrefix(); + } } diff --git a/src/Type/DatePickerType.php b/src/Type/DatePickerType.php index 6ad17ec1..36ffecf3 100644 --- a/src/Type/DatePickerType.php +++ b/src/Type/DatePickerType.php @@ -18,10 +18,12 @@ /** * @author Hugo Briand + * + * @final since sonata-project/form-extensions 0.x */ -final class DatePickerType extends BasePickerType +class DatePickerType extends BasePickerType { - public function configureOptions(OptionsResolver $resolver): void + public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array_merge($this->getCommonDefaults(), [ 'dp_pick_time' => false, @@ -31,13 +33,27 @@ public function configureOptions(OptionsResolver $resolver): void parent::configureOptions($resolver); } - public function getParent(): string + /** + * @return string, + */ + public function getParent() { return DateType::class; } - public function getBlockPrefix(): string + /** + * @return string + */ + public function getBlockPrefix() { return 'sonata_type_date_picker'; } + + /** + * @return string + */ + public function getName() + { + return $this->getBlockPrefix(); + } } diff --git a/src/Type/DateRangePickerType.php b/src/Type/DateRangePickerType.php index 424a70e2..ff3a1b51 100644 --- a/src/Type/DateRangePickerType.php +++ b/src/Type/DateRangePickerType.php @@ -19,10 +19,12 @@ * DateRangePickerType. * * @author Andrej Hudec + * + * @final since sonata-project/form-extensions 0.x */ -final class DateRangePickerType extends DateRangeType +class DateRangePickerType extends DateRangeType { - public function configureOptions(OptionsResolver $resolver): void + public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'field_options' => [], @@ -34,8 +36,19 @@ public function configureOptions(OptionsResolver $resolver): void ]); } - public function getBlockPrefix(): string + /** + * @return string + */ + public function getBlockPrefix() { return 'sonata_type_date_range_picker'; } + + /** + * @return string + */ + public function getName() + { + return $this->getBlockPrefix(); + } } diff --git a/src/Type/DateRangeType.php b/src/Type/DateRangeType.php index 6c1197c7..4e0920f4 100644 --- a/src/Type/DateRangeType.php +++ b/src/Type/DateRangeType.php @@ -17,10 +17,55 @@ use Symfony\Component\Form\Extension\Core\Type\DateType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface; +use Symfony\Contracts\Translation\TranslatorInterface; class DateRangeType extends AbstractType { - public function buildForm(FormBuilderInterface $builder, array $options): void + /** + * NEXT_MAJOR: remove this property. + * + * @var LegacyTranslatorInterface|TranslatorInterface|null + * + * @deprecated translator property is deprecated since sonata-project/form-extensions 0.x, to be removed in 1.0 + */ + protected $translator; + + /** + * NEXT_MAJOR: remove this method. + * + * @deprecated translator dependency is deprecated since sonata-project/form-extensions 0.x, to be removed in 1.0 + * + * @param LegacyTranslatorInterface|TranslatorInterface|null $translator + */ + public function __construct($translator = null) + { + if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface && null !== $translator) { + throw new \InvalidArgumentException(sprintf( + 'Argument 1 should be an instance of %s or %s or %s', + LegacyTranslatorInterface::class, + TranslatorInterface::class, + 'null' + )); + } + + if (null !== $translator && __CLASS__ !== static::class && DateRangePickerType::class !== static::class) { + @trigger_error( + sprintf( + 'The translator dependency in %s is deprecated since 0.x and will be removed in 1.0. '. + 'Please do not call %s with translator argument in %s.', + __CLASS__, + __METHOD__, + static::class + ), + E_USER_DEPRECATED + ); + } + + $this->translator = $translator; + } + + public function buildForm(FormBuilderInterface $builder, array $options) { $options['field_options_start'] = array_merge( [ @@ -50,12 +95,23 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ); } - public function getBlockPrefix(): string + /** + * @return string + */ + public function getBlockPrefix() { return 'sonata_type_date_range'; } - public function configureOptions(OptionsResolver $resolver): void + /** + * @return string + */ + public function getName() + { + return $this->getBlockPrefix(); + } + + public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'field_options' => [], diff --git a/src/Type/DateTimePickerType.php b/src/Type/DateTimePickerType.php index c84f8cf7..fb824668 100644 --- a/src/Type/DateTimePickerType.php +++ b/src/Type/DateTimePickerType.php @@ -18,10 +18,12 @@ /** * @author Hugo Briand + * + * @final since sonata-project/form-extensions 0.x */ -final class DateTimePickerType extends BasePickerType +class DateTimePickerType extends BasePickerType { - public function configureOptions(OptionsResolver $resolver): void + public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array_merge($this->getCommonDefaults(), [ 'dp_use_minutes' => true, @@ -34,13 +36,27 @@ public function configureOptions(OptionsResolver $resolver): void parent::configureOptions($resolver); } - public function getParent(): string + /** + * @return string + */ + public function getParent() { return DateTimeType::class; } - public function getBlockPrefix(): string + /** + * @return string + */ + public function getBlockPrefix() { return 'sonata_type_datetime_picker'; } + + /** + * @return string + */ + public function getName() + { + return $this->getBlockPrefix(); + } } diff --git a/src/Type/DateTimeRangePickerType.php b/src/Type/DateTimeRangePickerType.php index c6f20198..c60a4d51 100644 --- a/src/Type/DateTimeRangePickerType.php +++ b/src/Type/DateTimeRangePickerType.php @@ -14,6 +14,8 @@ namespace Sonata\Form\Type; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface; +use Symfony\Contracts\Translation\TranslatorInterface; /** * DateTimeRangePickerType. @@ -22,7 +24,50 @@ */ class DateTimeRangePickerType extends DateTimeRangeType { - public function configureOptions(OptionsResolver $resolver): void + /** + * NEXT_MAJOR: remove this property. + * + * @var LegacyTranslatorInterface|TranslatorInterface|null + * + * @deprecated translator property is deprecated since sonata-project/form-extensions 0.x, to be removed in 1.0 + */ + protected $translator; + + /** + * NEXT_MAJOR: remove this method. + * + * @deprecated translator dependency is deprecated since sonata-project/form-extensions 0.x, to be removed in 1.0 + * + * @param LegacyTranslatorInterface|TranslatorInterface|null $translator + */ + public function __construct($translator = null) + { + if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface && null !== $translator) { + throw new \InvalidArgumentException(sprintf( + 'Argument 1 should be an instance of %s or %s or %s', + LegacyTranslatorInterface::class, + TranslatorInterface::class, + 'null' + )); + } + + if (null !== $translator && __CLASS__ !== static::class && DateRangePickerType::class !== static::class) { + @trigger_error( + sprintf( + 'The translator dependency in %s is deprecated since 0.x and will be removed in 1.0. '. + 'Please do not call %s with translator argument in %s.', + __CLASS__, + __METHOD__, + static::class + ), + E_USER_DEPRECATED + ); + } + + $this->translator = $translator; + } + + public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'field_options' => [], @@ -34,8 +79,19 @@ public function configureOptions(OptionsResolver $resolver): void ]); } - public function getBlockPrefix(): string + /** + * @return string + */ + public function getBlockPrefix() { return 'sonata_type_datetime_range_picker'; } + + /** + * @return string + */ + public function getName() + { + return $this->getBlockPrefix(); + } } diff --git a/src/Type/DateTimeRangeType.php b/src/Type/DateTimeRangeType.php index 0df96c50..f11b45e6 100644 --- a/src/Type/DateTimeRangeType.php +++ b/src/Type/DateTimeRangeType.php @@ -20,7 +20,7 @@ class DateTimeRangeType extends AbstractType { - public function buildForm(FormBuilderInterface $builder, array $options): void + public function buildForm(FormBuilderInterface $builder, array $options) { $options['field_options_start'] = array_merge( [ @@ -50,12 +50,23 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ); } - public function getBlockPrefix(): string + /** + * @return string + */ + public function getBlockPrefix() { return 'sonata_type_datetime_range'; } - public function configureOptions(OptionsResolver $resolver): void + /** + * @return string + */ + public function getName() + { + return $this->getBlockPrefix(); + } + + public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'field_options' => [], diff --git a/src/Type/EqualType.php b/src/Type/EqualType.php index 0c6d8fa8..cf7c680b 100644 --- a/src/Type/EqualType.php +++ b/src/Type/EqualType.php @@ -16,6 +16,8 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface; +use Symfony\Contracts\Translation\TranslatorInterface; @trigger_error( 'The '.__NAMESPACE__.'\EqualType class is deprecated since version 1.2 and will be removed in 2.0.' @@ -27,14 +29,58 @@ * NEXT_MAJOR: remove this class. * * @deprecated since sonata-project/form-extensions 1.2, to be removed with 2.0 + * + * @final since sonata-project/form-extensions 0.x */ -final class EqualType extends AbstractType +class EqualType extends AbstractType { public const TYPE_IS_EQUAL = 1; public const TYPE_IS_NOT_EQUAL = 2; + /** + * NEXT_MAJOR: remove this property. + * + * @var LegacyTranslatorInterface|TranslatorInterface|null + * + * @deprecated translator property is deprecated since sonata-project/form-extensions 0.x, to be removed in 1.0 + */ + protected $translator; - public function configureOptions(OptionsResolver $resolver): void + /** + * NEXT_MAJOR: remove this method. + * + * @deprecated translator dependency is deprecated since sonata-project/form-extensions 0.x, to be removed in 1.0 + * + * @param LegacyTranslatorInterface|TranslatorInterface|null $translator + */ + public function __construct($translator = null) + { + if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface && null !== $translator) { + throw new \InvalidArgumentException(sprintf( + 'Argument 1 should be an instance of %s or %s or %s', + LegacyTranslatorInterface::class, + TranslatorInterface::class, + 'null' + )); + } + + if (null !== $translator && __CLASS__ !== static::class && DateRangePickerType::class !== static::class) { + @trigger_error( + sprintf( + 'The translator dependency in %s is deprecated since 0.x and will be removed in 1.0. '. + 'Please do not call %s with translator argument in %s.', + __CLASS__, + __METHOD__, + static::class + ), + E_USER_DEPRECATED + ); + } + + $this->translator = $translator; + } + + public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'choice_translation_domain' => 'SonataFormBundle', @@ -45,13 +91,27 @@ public function configureOptions(OptionsResolver $resolver): void ]); } - public function getParent(): string + /** + * @return string + */ + public function getParent() { return ChoiceType::class; } - public function getBlockPrefix(): string + /** + * @return string + */ + public function getBlockPrefix() { return 'sonata_type_equal'; } + + /** + * @return string + */ + public function getName() + { + return $this->getBlockPrefix(); + } } diff --git a/src/Type/ImmutableArrayType.php b/src/Type/ImmutableArrayType.php index d4b4f530..90fb2b9c 100644 --- a/src/Type/ImmutableArrayType.php +++ b/src/Type/ImmutableArrayType.php @@ -17,9 +17,12 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; -final class ImmutableArrayType extends AbstractType +/** + * @final since sonata-project/form-extensions 0.x + */ +class ImmutableArrayType extends AbstractType { - public function buildForm(FormBuilderInterface $builder, array $options): void + public function buildForm(FormBuilderInterface $builder, array $options) { foreach ($options['keys'] as $infos) { if ($infos instanceof FormBuilderInterface) { @@ -44,7 +47,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void } } - public function configureOptions(OptionsResolver $resolver): void + public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'keys' => [], @@ -61,8 +64,19 @@ public function configureOptions(OptionsResolver $resolver): void }); } - public function getBlockPrefix(): string + /** + * @return string + */ + public function getBlockPrefix() { return 'sonata_type_immutable_array'; } + + /** + * @return string + */ + public function getName() + { + return $this->getBlockPrefix(); + } } diff --git a/src/Validator/Constraints/InlineConstraint.php b/src/Validator/Constraints/InlineConstraint.php index 5dbc6d4a..729cccca 100755 --- a/src/Validator/Constraints/InlineConstraint.php +++ b/src/Validator/Constraints/InlineConstraint.php @@ -20,8 +20,10 @@ * * @Annotation * @Target({"CLASS"}) + * + * @final since sonata-project/form-extensions 0.x */ -final class InlineConstraint extends Constraint +class InlineConstraint extends Constraint { /** * @var mixed @@ -61,24 +63,27 @@ public function __sleep(): array return array_keys(get_object_vars($this)); } - public function __wakeup(): void + public function __wakeup() { if (\is_string($this->service) && \is_string($this->method)) { return; } - $this->method = static function (): void { + $this->method = static function () { }; $this->serializingWarning = true; } - public function validatedBy(): string + public function validatedBy() { return 'sonata.form.validator.inline'; } - public function isClosure(): bool + /** + * @return bool + */ + public function isClosure() { return $this->method instanceof \Closure; } @@ -91,12 +96,18 @@ public function getClosure() return $this->method; } - public function getTargets(): string + /** + * @return mixed + */ + public function getTargets() { return self::CLASS_CONSTRAINT; } - public function getRequiredOptions(): array + /** + * @return array + */ + public function getRequiredOptions() { return [ 'service', diff --git a/src/Validator/ErrorElement.php b/src/Validator/ErrorElement.php index b92f7f04..8b7fa51c 100755 --- a/src/Validator/ErrorElement.php +++ b/src/Validator/ErrorElement.php @@ -19,54 +19,57 @@ use Symfony\Component\Validator\ConstraintValidatorFactoryInterface; use Symfony\Component\Validator\Context\ExecutionContextInterface; -final class ErrorElement +/** + * @final since sonata-project/form-extensions 0.x + */ +class ErrorElement { private const DEFAULT_TRANSLATION_DOMAIN = 'validators'; /** * @var ExecutionContextInterface */ - private $context; + protected $context; /** * @var string */ - private $group; + protected $group; /** * @var ConstraintValidatorFactoryInterface */ - private $constraintValidatorFactory; + protected $constraintValidatorFactory; /** * @var string[] */ - private $stack = []; + protected $stack = []; /** - * @var string[] + * @var PropertyPath[] */ - private $propertyPaths = []; + protected $propertyPaths = []; /** * @var mixed */ - private $subject; + protected $subject; /** * @var string */ - private $current = ''; + protected $current = ''; /** * @var string */ - private $basePropertyPath; + protected $basePropertyPath; /** * @var array */ - private $errors = []; + protected $errors = []; /** * @param mixed $subject @@ -76,7 +79,7 @@ public function __construct( $subject, ConstraintValidatorFactoryInterface $constraintValidatorFactory, ExecutionContextInterface $context, - ?string $group + $group ) { $this->subject = $subject; $this->context = $context; @@ -86,9 +89,13 @@ public function __construct( } /** + * @param string $name + * * @throws \RuntimeException + * + * @return ErrorElement */ - public function __call(string $name, array $arguments = []): self + public function __call($name, array $arguments = []) { if ('assert' === substr($name, 0, 6)) { $this->validate($this->newConstraint(substr($name, 6), $arguments[0] ?? [])); @@ -99,14 +106,23 @@ public function __call(string $name, array $arguments = []): self return $this; } - public function addConstraint(Constraint $constraint): self + /** + * @return ErrorElement + */ + public function addConstraint(Constraint $constraint) { $this->validate($constraint); return $this; } - public function with(string $name, bool $key = false): self + /** + * @param string $name + * @param bool $key + * + * @return ErrorElement + */ + public function with($name, $key = false) { $key = $key ? $name.'.'.$key : $name; $this->stack[] = $key; @@ -120,7 +136,10 @@ public function with(string $name, bool $key = false): self return $this; } - public function end(): self + /** + * @return ErrorElement + */ + public function end() { array_pop($this->stack); @@ -129,7 +148,10 @@ public function end(): self return $this; } - public function getFullPropertyPath(): string + /** + * @return string + */ + public function getFullPropertyPath() { if ($this->getCurrentPropertyPath()) { return sprintf('%s.%s', $this->basePropertyPath, $this->getCurrentPropertyPath()); @@ -148,11 +170,12 @@ public function getSubject() /** * @param string|array $message + * @param array $parameters * @param mixed|null $value * * @return ErrorElement */ - public function addViolation($message, array $parameters = [], $value = null, string $translationDomain = self::DEFAULT_TRANSLATION_DOMAIN): self + public function addViolation($message, $parameters = [], $value = null, string $translationDomain = self::DEFAULT_TRANSLATION_DOMAIN) { if (\is_array($message)) { $value = $message[2] ?? $value; @@ -178,12 +201,15 @@ public function addViolation($message, array $parameters = [], $value = null, st return $this; } - public function getErrors(): array + /** + * @return array + */ + public function getErrors() { return $this->errors; } - private function validate(Constraint $constraint): void + protected function validate(Constraint $constraint) { $this->context->getValidator() ->inContext($this->context) @@ -196,7 +222,7 @@ private function validate(Constraint $constraint): void * * @return mixed */ - private function getValue() + protected function getValue() { if ('' === $this->current) { return $this->subject; @@ -208,9 +234,11 @@ private function getValue() } /** + * @param string $name + * * @return object */ - private function newConstraint(string $name, array $options = []) + protected function newConstraint($name, array $options = []) { if (false !== strpos($name, '\\') && class_exists($name)) { $className = (string) $name; @@ -221,7 +249,10 @@ private function newConstraint(string $name, array $options = []) return new $className($options); } - private function getCurrentPropertyPath(): ?PropertyPath + /** + * @return PropertyPath|null + */ + protected function getCurrentPropertyPath() { if (!isset($this->propertyPaths[$this->current])) { return null; //global error diff --git a/src/Validator/InlineValidator.php b/src/Validator/InlineValidator.php index d04ad89b..311cb3d9 100755 --- a/src/Validator/InlineValidator.php +++ b/src/Validator/InlineValidator.php @@ -18,7 +18,10 @@ use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\ConstraintValidatorFactoryInterface; -final class InlineValidator extends ConstraintValidator +/** + * @final since sonata-project/form-extensions 0.x + */ +class InlineValidator extends ConstraintValidator { /** * @var ContainerInterface @@ -38,7 +41,7 @@ public function __construct( $this->constraintValidatorFactory = $constraintValidatorFactory; } - public function validate($value, Constraint $constraint): void + public function validate($value, Constraint $constraint) { if ($constraint->isClosure()) { $function = $constraint->getClosure(); @@ -57,8 +60,10 @@ public function validate($value, Constraint $constraint): void /** * @param mixed $value + * + * @return ErrorElement */ - protected function getErrorElement($value): ErrorElement + protected function getErrorElement($value) { return new ErrorElement( $value, diff --git a/tests/Type/FormChoiceWidgetTest.php b/tests/Type/FormChoiceWidgetTest.php index 2ae5ef62..aa48733b 100644 --- a/tests/Type/FormChoiceWidgetTest.php +++ b/tests/Type/FormChoiceWidgetTest.php @@ -21,7 +21,7 @@ */ class FormChoiceWidgetTest extends AbstractWidgetTestCase { - public function testLabelRendering(): void + public function testLabelRendering() { $choices = ['some' => 0, 'choices' => 1];