From ffbcdcf5f65396a5ec12703c62ba7d42105ccb39 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 29 Aug 2024 22:01:40 +0300 Subject: [PATCH] Add support integer, float, `Stringable` and backed enumerations as value of `SingleTable` attribute --- src/Annotation/Inheritance/SingleTable.php | 9 +++++- .../Attribute/SingleTable/IntegerEnum.php | 11 +++++++ .../Attribute/SingleTable/SingleTableTest.php | 31 +++++++++++++++++++ .../Unit/Attribute/SingleTable/StringEnum.php | 11 +++++++ .../SingleTable/StringableObject.php | 20 ++++++++++++ 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 tests/Annotated/Unit/Attribute/SingleTable/IntegerEnum.php create mode 100644 tests/Annotated/Unit/Attribute/SingleTable/SingleTableTest.php create mode 100644 tests/Annotated/Unit/Attribute/SingleTable/StringEnum.php create mode 100644 tests/Annotated/Unit/Attribute/SingleTable/StringableObject.php diff --git a/src/Annotation/Inheritance/SingleTable.php b/src/Annotation/Inheritance/SingleTable.php index b67d02aa..9de9c69b 100644 --- a/src/Annotation/Inheritance/SingleTable.php +++ b/src/Annotation/Inheritance/SingleTable.php @@ -4,8 +4,10 @@ namespace Cycle\Annotated\Annotation\Inheritance; +use BackedEnum; use Cycle\Annotated\Annotation\Inheritance; use Spiral\Attributes\NamedArgumentConstructor; +use Stringable; /** * @Annotation @@ -15,9 +17,14 @@ #[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor] class SingleTable extends Inheritance { + protected ?string $value; + public function __construct( - protected ?string $value = null + string|int|float|Stringable|BackedEnum|null $value = null ) { + $this->value = $value === null + ? null + : (string) ($value instanceof BackedEnum ? $value->value : $value); parent::__construct('single'); } diff --git a/tests/Annotated/Unit/Attribute/SingleTable/IntegerEnum.php b/tests/Annotated/Unit/Attribute/SingleTable/IntegerEnum.php new file mode 100644 index 00000000..251a807b --- /dev/null +++ b/tests/Annotated/Unit/Attribute/SingleTable/IntegerEnum.php @@ -0,0 +1,11 @@ +assertSame($expected, $attribute->getValue()); + } +} diff --git a/tests/Annotated/Unit/Attribute/SingleTable/StringEnum.php b/tests/Annotated/Unit/Attribute/SingleTable/StringEnum.php new file mode 100644 index 00000000..c9325a1e --- /dev/null +++ b/tests/Annotated/Unit/Attribute/SingleTable/StringEnum.php @@ -0,0 +1,11 @@ +value; + } +}