Skip to content

Commit

Permalink
Fix Choice annotation/attribute constructor args changing with Symfon…
Browse files Browse the repository at this point in the history
…y versions (#53)
  • Loading branch information
yann-eugone authored Feb 21, 2022
1 parent e98614b commit 8e56a65
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ jobs:
symfony-version: 4.4.*
- php-version: 8.1
symfony-version: 4.4.*
- php-version: 7.2
symfony-version: 5.2.*
- php-version: 7.2
symfony-version: 5.3.*
- php-version: 7.2
symfony-version: 5.4.*
- php-version: 8.1
Expand Down
54 changes: 38 additions & 16 deletions src/Validator/Constraints/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,45 @@ public function __construct(
if (\is_string($enum)) {
$this->enum = $enum;
}

// Symfony 5.x Constraints has many constructor arguments for PHP 8.0 Attributes support
parent::__construct(
null,
$callback,
$multiple,
$strict,
$min,
$max,
$message,
$multipleMessage,
$minMessage,
$maxMessage,
$groups,
$payload,
$options
);

$firstConstructorArg = (new \ReflectionClass(Choice::class))
->getConstructor()->getParameters()[0]->getName();
if ($firstConstructorArg === 'choices') {
// Prior to Symfony 5.3, first argument of Choice was $choices
parent::__construct(
null,
$callback,
$multiple,
$strict,
$min,
$max,
$message,
$multipleMessage,
$minMessage,
$maxMessage,
$groups,
$payload,
$options
);
} else {
// Since Symfony 5.3, first argument of Choice is $options
parent::__construct(
$options,
null,
$callback,
$multiple,
$strict,
$min,
$max,
$message,
$multipleMessage,
$minMessage,
$maxMessage,
$groups,
$payload
);
}
}
}

Expand Down

0 comments on commit 8e56a65

Please sign in to comment.