-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #183 FlagBagType support (michnovka)
This PR was squashed before being merged into the 2.x-dev branch. Discussion ---------- FlagBagType support This is initial commit for `FlagBagType` support. Lets discuss in here Commits ------- d66316e FlagBagType support
- Loading branch information
Showing
8 changed files
with
479 additions
and
8 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
67 changes: 67 additions & 0 deletions
67
src/Bridge/Symfony/Form/DataTransformer/AbstractFlagBagTransformer.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,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "elao/enum" package. | ||
* | ||
* Copyright (C) Elao | ||
* | ||
* @author Elao <contact@elao.com> | ||
*/ | ||
|
||
namespace Elao\Enum\Bridge\Symfony\Form\DataTransformer; | ||
|
||
use Elao\Enum\Exception\InvalidArgumentException as ElaoInvalidArgumentException; | ||
use Elao\Enum\Exception\LogicException; | ||
use Elao\Enum\FlagBag; | ||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Symfony\Component\Form\Exception\InvalidArgumentException; | ||
|
||
abstract class AbstractFlagBagTransformer implements DataTransformerInterface | ||
{ | ||
/** @var class-string<\BackedEnum> */ | ||
protected string $enumType; | ||
|
||
/** @var class-string<\BackedEnum> */ | ||
public function __construct(string $enumType) | ||
{ | ||
if (!is_a($enumType, \BackedEnum::class, true)) { | ||
throw new InvalidArgumentException(sprintf( | ||
'"%s" is not an instance of "%s"', | ||
$enumType, | ||
\BackedEnum::class | ||
)); | ||
} | ||
|
||
try { | ||
FlagBag::getBitmask($enumType); | ||
} catch (LogicException $e) { | ||
throw new InvalidArgumentException(sprintf( | ||
'"%s" is not a valid bitmask enum', | ||
$enumType | ||
), 0, $e); | ||
} catch (ElaoInvalidArgumentException $e) { | ||
throw new InvalidArgumentException($e->getMessage(), 0, $e); | ||
} | ||
|
||
$this->enumType = $enumType; | ||
} | ||
|
||
/** | ||
* @param int|\BackedEnum[] $value | ||
*/ | ||
protected function createEnum(int|array $value): FlagBag | ||
{ | ||
if (\is_int($value)) { | ||
return new FlagBag($this->enumType, $value); | ||
} else { | ||
return FlagBag::from($this->enumType, ...$value); | ||
} | ||
} | ||
|
||
protected function isAcceptableValueForEnum(int $value): bool | ||
{ | ||
return FlagBag::accepts($this->enumType, $value); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
src/Bridge/Symfony/Form/DataTransformer/FlagBagToCollectionTransformer.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,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "elao/enum" package. | ||
* | ||
* Copyright (C) Elao | ||
* | ||
* @author Elao <contact@elao.com> | ||
*/ | ||
|
||
namespace Elao\Enum\Bridge\Symfony\Form\DataTransformer; | ||
|
||
use Elao\Enum\FlagBag; | ||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
|
||
class FlagBagToCollectionTransformer extends AbstractFlagBagTransformer | ||
{ | ||
/** | ||
* Transforms a FlagBag objects to an array of BackedEnum | ||
* | ||
* @param FlagBag $value A FlagBag instance | ||
* | ||
* @throws TransformationFailedException When the transformation fails | ||
* | ||
* @return \BackedEnum[]|null An array of FlagBag instances with single bit flag | ||
*/ | ||
public function transform($value): ?array | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
if (!$value instanceof FlagBag) { | ||
throw new TransformationFailedException(sprintf( | ||
'Expected instance of "%s". Got "%s".', | ||
FlagBag::class, | ||
get_debug_type($value) | ||
)); | ||
} | ||
|
||
if ($value->getType() !== $this->enumType) { | ||
throw new TransformationFailedException(sprintf( | ||
'Expected FlagBag instance of "%s" values. Got FlagBag instance of "%s" values.', | ||
$this->enumType, | ||
$value->getType() | ||
)); | ||
} | ||
|
||
return $value->getFlags(); | ||
} | ||
|
||
/** | ||
* Transforms an array of BackedEnum to single FlagBag instance. | ||
* | ||
* @param \BackedEnum[] $values An array of flag enums | ||
* | ||
* @throws TransformationFailedException When the transformation fails | ||
* | ||
* @return FlagBag|null A single FlagBag instance or null | ||
*/ | ||
public function reverseTransform($values): ?FlagBag | ||
{ | ||
if (null === $values) { | ||
return null; | ||
} | ||
|
||
if (!\is_array($values)) { | ||
throw new TransformationFailedException(sprintf( | ||
'Expected array. Got "%s".', | ||
get_debug_type($values) | ||
)); | ||
} | ||
|
||
if (0 === \count($values)) { | ||
return $this->createEnum(FlagBag::NONE); | ||
} | ||
|
||
foreach ($values as $value) { | ||
if (!$value instanceof $this->enumType) { | ||
throw new TransformationFailedException(sprintf( | ||
'Expected array of "%s". Got a "%s" inside.', | ||
$this->enumType, | ||
get_debug_type($value) | ||
)); | ||
} | ||
} | ||
|
||
return $this->createEnum($values); | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "elao/enum" package. | ||
* | ||
* Copyright (C) Elao | ||
* | ||
* @author Elao <contact@elao.com> | ||
*/ | ||
|
||
namespace Elao\Enum\Bridge\Symfony\Form\Type; | ||
|
||
use Elao\Enum\Bridge\Symfony\Form\DataTransformer\FlagBagToCollectionTransformer; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Exception\InvalidConfigurationException; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* @final | ||
*/ | ||
class FlagBagType extends AbstractType | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
if (!$options['multiple']) { | ||
throw new InvalidConfigurationException(sprintf( | ||
'The "multiple" option of the "%s" form type cannot be set to false.', | ||
static::class | ||
)); | ||
} | ||
|
||
$builder->addModelTransformer(new FlagBagToCollectionTransformer($options['class'])); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver | ||
->setDefault('multiple', true) | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParent(): ?string | ||
{ | ||
return EnumType::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
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "elao/enum" package. | ||
* | ||
* Copyright (C) Elao | ||
* | ||
* @author Elao <contact@elao.com> | ||
*/ | ||
|
||
namespace Elao\Enum\Tests\Fixtures\Enum; | ||
|
||
use Elao\Enum\Attribute\EnumCase; | ||
use Elao\Enum\ReadableEnumInterface; | ||
use Elao\Enum\ReadableEnumTrait; | ||
|
||
enum PermissionsReadable: int implements ReadableEnumInterface | ||
{ | ||
use ReadableEnumTrait; | ||
|
||
#[EnumCase('Execute')] | ||
case Execute = 1 << 0; | ||
#[EnumCase('Write')] | ||
case Write = 1 << 1; | ||
#[EnumCase('Read')] | ||
case Read = 1 << 2; | ||
} |
Oops, something went wrong.