Skip to content

Commit

Permalink
Allow symfony 7 (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
ropczan authored and konradkozaczenko committed Nov 29, 2024
1 parent c52533c commit 99b2ab5
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 107 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ jobs:
strategy:
matrix:
php-version:
- "7.4"
- "8.0"
- "8.1"
- "8.2"
deps:
- "normal"
include:
- deps: "low"
php-version: "7.4"
php-version: "8.0"

steps:
- name: "Checkout"
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ jobs:
strategy:
matrix:
php-version:
- "7.4"
- "8.0"
- "8.1"
steps:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
composer.lock
coverage/
.phpunit.result.cache
.idea/
89 changes: 46 additions & 43 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,46 +1,49 @@
{
"name": "survos/liform",
"description": "Library to transform Symfony Forms into Json Schema",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Nacho Martín",
"email": "[email protected]"
"name": "answear/liform",
"description": "Library to transform Symfony Forms into Json Schema",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Nacho Martín",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Limenius\\Liform\\": "src/Limenius/Liform"
}
},
"autoload-dev": {
"psr-4": {
"Limenius\\Liform\\Tests\\": "tests/Limenius/Liform/Tests"
}
},
"require": {
"php": "^8.2",
"symfony/form": "^6.4|^7.0",
"symfony/translation": "^6.4|^7.0",
"symfony/serializer": "^6.4|^7.0",
"symfony/validator": "^6.4|^7.0",
"symfony/translation-contracts": "^1.0|^2.1|^3.0"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^v0.7.2",
"escapestudios/symfony2-coding-standard": "^3.0",
"phpunit/phpunit": "^8.0",
"phpcompatibility/php-compatibility": "^9.3",
"phpstan/phpstan": "^1.8"
},
"replace": {
"limenius/liform": "*"
},
"scripts": {
"test": "phpunit",
"cs": "phpcs"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
],
"autoload": {
"psr-4": {
"Limenius\\Liform\\": "src/Limenius/Liform"
}
},
"autoload-dev": {
"psr-4": {
"Limenius\\Liform\\Tests\\": "tests/Limenius/Liform/Tests"
}
},
"require": {
"php": "^8.2",
"symfony/form": "^6.4|^7.0",
"symfony/translation": "^6.4|^7.0",
"symfony/serializer": "^6.4|^7.0",
"symfony/validator": "^6.4|^7.0",
"symfony/translation-contracts": "^1.0|^2.1|^3.0"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^v0.7.2",
"escapestudios/symfony2-coding-standard": "^3.0",
"phpunit/phpunit": "^8.0",
"phpcompatibility/php-compatibility": "^9.3",
"phpstan/phpstan": "^1.8"
},
"scripts": {
"test": "phpunit",
"cs": "phpcs"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
38 changes: 9 additions & 29 deletions src/Limenius/Liform/Serializer/Normalizer/FormErrorNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface as TranslatorContract;

/**
* Normalizes invalid Form instances.
Expand All @@ -41,7 +40,7 @@ public function __construct(TranslatorInterface $translator)
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = []): float|array|\ArrayObject|bool|int|string|null
public function normalize(mixed $object, ?string $format = null, array $context = []): float|array|\ArrayObject|bool|int|string|null
{
return [
'code' => isset($context['status_code']) ? $context['status_code'] : null,
Expand All @@ -52,22 +51,20 @@ public function normalize($object, $format = null, array $context = []): float|a

/**
* {@inheritdoc}
* @param mixed $data
* @param null $format
* @param array $context
*/
public function supportsNormalization($data, $format = null, array $context = []): bool
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $data instanceof FormInterface && $data->isSubmitted() && !$data->isValid();
}

/**
* This code has been taken from JMSSerializer.
*
* @param FormInterface $data
*
* @return array
* {@inheritdoc}
*/
public function getSupportedTypes(?string $format): array
{
return [Form::class => true];
}

private function convertFormToArray(FormInterface $data): array
{
$form = $errors = [];
Expand All @@ -93,21 +90,9 @@ private function convertFormToArray(FormInterface $data): array
return $form;
}

/**
* @param FormError $error
*
* @return string
*/
private function getErrorMessage(FormError $error)
private function getErrorMessage(FormError $error): string
{
if (null !== $error->getMessagePluralization()) {
// old way
// if ($this->translator instanceof TranslatorContract) {
// return $this->translator->trans($error->getMessageTemplate(), ['%count%' => $error->getMessagePluralization()] + $error->getMessageParameters(), 'validators');
// } else {
// return $this->translator->transChoice($error->getMessageTemplate(), $error->getMessagePluralization(), $error->getMessageParameters(), 'validators');
// }

return $this->translator->trans(
$error->getMessageTemplate(),
array_merge(
Expand All @@ -120,9 +105,4 @@ private function getErrorMessage(FormError $error)

return $this->translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), 'validators');
}

public function getSupportedTypes(?string $format): array
{
return [Form::class];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,29 @@ class InitialValuesNormalizer implements NormalizerInterface
/**
* {@inheritdoc}
*/
public function normalize($form, $format = null, array $context = []): float|array|\ArrayObject|bool|int|string|null
public function normalize($object, $format = null, array $context = []): float|array|\ArrayObject|bool|int|string|null
{
$formView = $form->createView();
$formView = $object->createView();

return $this->getValues($form, $formView);
return $this->getValues($object, $formView);
}

/**
* {@inheritdoc}
* @param mixed $data
* @param null $format
* @param array $context
*/
public function supportsNormalization($data, $format = null, array $context = []): bool
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $data instanceof Form;
}

/**
* Gets the values of the form
* @param Form|FormInterface $form
* @param FormView $formView
*
* @return mixed
* {@inheritdoc}
*/
// private function getValues(Form $form, FormView $formView): mixed
public function getSupportedTypes(?string $format): array
{
return [Form::class => true];
}

private function getValues(FormInterface $form, FormView $formView): mixed
{
if (!empty($formView->children)) {
Expand All @@ -66,7 +63,7 @@ private function getValues(FormInterface $form, FormView $formView): mixed
return $this->normalizeExpandedChoice($formView);
}
// Force serialization as {} instead of []
$data = (object) array();
$data = (object) [];
foreach ($formView->children as $name => $child) {
// Avoid unknown field error when csrf_protection is true
// CSRF token should be extracted another way
Expand All @@ -87,15 +84,9 @@ private function getValues(FormInterface $form, FormView $formView): mixed
return $formView->vars['value'];
}

/**
* Normalize when choice is multiple
* @param FormView $formView
*
* @return array
*/
private function normalizeMultipleExpandedChoice(FormView $formView): array
{
$data = array();
$data = [];
foreach ($formView->children as $name => $child) {
if ($child->vars['checked']) {
$data[] = $child->vars['value'];
Expand All @@ -105,12 +96,6 @@ private function normalizeMultipleExpandedChoice(FormView $formView): array
return $data;
}

/**
* Normalize when choice is expanded
* @param FormView $formView
*
* @return mixed
*/
private function normalizeExpandedChoice(FormView $formView): mixed
{
foreach ($formView->children as $name => $child) {
Expand All @@ -121,9 +106,4 @@ private function normalizeExpandedChoice(FormView $formView): mixed

return null;
}

public function getSupportedTypes(?string $format): array
{
return [Form::class];
}
}

0 comments on commit 99b2ab5

Please sign in to comment.