Skip to content

Commit

Permalink
Backport for SonataCoreBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
wbloszyk authored and greg0ire committed May 6, 2020
1 parent dddbb3c commit f1890d9
Show file tree
Hide file tree
Showing 28 changed files with 538 additions and 119 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ branches:
only:
- master
- 1.x
- 0.x

language: php

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -56,7 +56,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
"dev-master": "0.x-dev"
}
},
"autoload": {
Expand Down
4 changes: 2 additions & 2 deletions src/Bridge/Symfony/Bundle/SonataFormBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
final class SonataFormBundle extends Bundle
{
/**
* {@inheritdoc}
* @return string
*/
public function getPath()
{
return __DIR__.'/..';
}

/**
* {@inheritdoc}
* @return string
*/
protected function getContainerExtensionClass()
{
Expand Down
9 changes: 6 additions & 3 deletions src/DataTransformer/BooleanTypeToBooleanTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Date/MomentFormatConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
11 changes: 8 additions & 3 deletions src/EventListener/FixCheckboxDataListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@
* returning true value when the form is bind.
*
* @author Sylvain Rascar <[email protected]>
*
* @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();
Expand Down
12 changes: 7 additions & 5 deletions src/EventListener/ResizeFormListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
* Resize a collection form element based on the data sent from the client.
*
* @author Bernhard Schussek <[email protected]>
*
* @final since sonata-project/form-extensions 0.x
*/
final class ResizeFormListener implements EventSubscriberInterface
class ResizeFormListener implements EventSubscriberInterface
{
/**
* @var string
Expand Down Expand Up @@ -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',
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions src/Fixtures/StubFilesystemLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* 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);
}
}
27 changes: 22 additions & 5 deletions src/Serializer/BaseSerializerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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();

Expand All @@ -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]);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Serializer/SerializerHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
*/
interface SerializerHandlerInterface extends SubscribingHandlerInterface
{
public static function getType(): string;
/**
* @return string
*/
public static function getType();
}
27 changes: 19 additions & 8 deletions src/Test/AbstractWidgetTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -89,40 +89,51 @@ 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';

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*>([^<]+)</', static function (array $value): string {
return preg_replace_callback('/\s*>([^<]+)</', static function ($value) {
return '>'.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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Type/BaseDoctrineORMSerializationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit f1890d9

Please sign in to comment.