-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow Enum constraint to be used as PHP 8.0 Attribute (#50)
* Allow Enum constraint to be used as PHP 8.0 Attribute * Review testing matrix for wider versions range : PHP 7.1 & Symfony 5.3 * Separate testing model for annotations & attributes testing * Replace annotations with attributes in doc
- Loading branch information
1 parent
258f25e
commit 01744ff
Showing
15 changed files
with
173 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,13 +12,56 @@ | |
* | ||
* @author Yann Eugoné <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD)] | ||
final class Enum extends Choice | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public $enum; | ||
|
||
public function __construct( | ||
$enum = null, | ||
$callback = null, | ||
bool $multiple = null, | ||
bool $strict = null, | ||
int $min = null, | ||
int $max = null, | ||
string $message = null, | ||
string $multipleMessage = null, | ||
string $minMessage = null, | ||
string $maxMessage = null, | ||
$groups = null, | ||
$payload = null, | ||
array $options = [] | ||
) { | ||
if (\is_array($enum)) { | ||
// Symfony 4.4 Constraints has single constructor argument containing all options | ||
parent::__construct($enum); | ||
} else { | ||
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 | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
framework: | ||
annotations: true | ||
|
||
sensio_framework_extra: | ||
router: | ||
annotations: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
framework: ~ | ||
framework: | ||
test: true | ||
form: true | ||
property_access: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,6 @@ | |
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Yokai\EnumBundle\Tests\Integration\App\Model\PullRequest; | ||
|
||
/** | ||
* @author Yann Eugoné <[email protected]> | ||
|
@@ -19,9 +17,4 @@ public function buildForm(FormBuilderInterface $builder, array $options): void | |
$builder->add('status'); | ||
$builder->add('labels'); | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver->setDefault('data_class', PullRequest::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,8 @@ | |
|
||
namespace Yokai\EnumBundle\Tests\Integration\App; | ||
|
||
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; | ||
use Symfony\Component\Config\Loader\LoaderInterface; | ||
use Symfony\Component\HttpKernel\Kernel as BaseKernel; | ||
use Yokai\EnumBundle\YokaiEnumBundle; | ||
|
||
/** | ||
* @author Yann Eugoné <[email protected]> | ||
|
@@ -16,8 +14,11 @@ final class Kernel extends BaseKernel | |
{ | ||
public function registerBundles(): iterable | ||
{ | ||
yield new FrameworkBundle(); | ||
yield new YokaiEnumBundle(); | ||
yield new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(); | ||
yield new \Yokai\EnumBundle\YokaiEnumBundle(); | ||
if (\PHP_VERSION_ID < 80000) { | ||
yield new \Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(); | ||
} | ||
} | ||
|
||
public function getProjectDir(): string | ||
|
@@ -27,7 +28,12 @@ public function getProjectDir(): string | |
|
||
public function registerContainerConfiguration(LoaderInterface $loader): void | ||
{ | ||
$loader->load($this->getProjectDir() . '/config/packages/'); | ||
$loader->load($this->getProjectDir() . '/config/services.yaml'); | ||
$loader->load(__DIR__ . '/../config/packages/framework.yaml'); | ||
$loader->load(__DIR__ . '/../config/packages/translation.yaml'); | ||
if (\PHP_VERSION_ID < 80000) { | ||
$loader->load(__DIR__ . '/../config/packages/annotations.yaml'); | ||
} | ||
|
||
$loader->load(__DIR__ . '/../config/services.yaml'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
/** | ||
* @author Yann Eugoné <[email protected]> | ||
*/ | ||
final class PullRequest | ||
final class PullRequestUsingAnnotations | ||
{ | ||
/** | ||
* @var Status | ||
|
27 changes: 27 additions & 0 deletions
27
tests/Integration/src/Model/PullRequestUsingAttributes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yokai\EnumBundle\Tests\Integration\App\Model; | ||
|
||
use Yokai\EnumBundle\Validator\Constraints\Enum; | ||
use Yokai\EnumBundle\Tests\Integration\App\Enum\PullRequestStatusEnum; | ||
use Yokai\EnumBundle\Tests\Integration\App\Enum\PullRequestLabelEnum; | ||
|
||
/** | ||
* @author Yann Eugoné <[email protected]> | ||
*/ | ||
final class PullRequestUsingAttributes | ||
{ | ||
/** | ||
* @var Status | ||
*/ | ||
#[Enum(enum: PullRequestStatusEnum::class)] | ||
public $status; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
#[Enum(enum: PullRequestLabelEnum::class, multiple: true)] | ||
public $labels; | ||
} |
Oops, something went wrong.