diff --git a/src/Source/AbstractPropertySourceIterator.php b/src/Source/AbstractPropertySourceIterator.php index 21b0de0a..d32f4052 100644 --- a/src/Source/AbstractPropertySourceIterator.php +++ b/src/Source/AbstractPropertySourceIterator.php @@ -126,6 +126,8 @@ protected function getValue(mixed $value): bool|int|float|string|null $value instanceof \Traversable => '['.implode(', ', array_map([$this, 'getValue'], iterator_to_array($value))).']', $value instanceof \DateTimeInterface => $value->format($this->dateTimeFormat), $value instanceof \DateInterval => $this->getDuration($value), + $value instanceof \BackedEnum => $value->value, + $value instanceof \UnitEnum => $value->name, \is_object($value) => method_exists($value, '__toString') ? (string) $value : null, default => $value, }; diff --git a/tests/Source/AbstractPropertySourceIteratorTest.php b/tests/Source/AbstractPropertySourceIteratorTest.php index 620cc8dc..7ff54cec 100644 --- a/tests/Source/AbstractPropertySourceIteratorTest.php +++ b/tests/Source/AbstractPropertySourceIteratorTest.php @@ -15,7 +15,9 @@ use PHPUnit\Framework\TestCase; use Sonata\Exporter\Source\AbstractPropertySourceIterator; +use Sonata\Exporter\Tests\Source\Fixtures\Element; use Sonata\Exporter\Tests\Source\Fixtures\ObjectWithToString; +use Sonata\Exporter\Tests\Source\Fixtures\Suit; final class AbstractPropertySourceIteratorTest extends TestCase { @@ -48,32 +50,35 @@ public function provideGetValueCases(): iterable $datetime = new \DateTime(); $dateTimeImmutable = new \DateTimeImmutable(); - $data = [ - [[1, 2, 3], '[1, 2, 3]'], - [new \ArrayIterator([1, 2, 3]), '[1, 2, 3]'], - [(static function (): \Generator { yield from [1, 2, 3]; })(), '[1, 2, 3]'], - [$datetime, $datetime->format('r')], - [$datetime, $datetime->format('Y-m-d H:i:s'), 'Y-m-d H:i:s'], - [123, 123], - ['123', '123'], - [new ObjectWithToString('object with to string'), 'object with to string'], - [$dateTimeImmutable, $dateTimeImmutable->format('r')], - [$dateTimeImmutable, $dateTimeImmutable->format('Y-m-d H:i:s'), 'Y-m-d H:i:s'], - [new \DateInterval('P1Y'), 'P1Y'], - [new \DateInterval('P1M'), 'P1M'], - [new \DateInterval('P1D'), 'P1D'], - [new \DateInterval('PT1H'), 'PT1H'], - [new \DateInterval('PT1M'), 'PT1M'], - [new \DateInterval('PT1S'), 'PT1S'], - [new \DateInterval('P1Y1M'), 'P1Y1M'], - [new \DateInterval('P1Y1M1D'), 'P1Y1M1D'], - [new \DateInterval('P1Y1M1DT1H'), 'P1Y1M1DT1H'], - [new \DateInterval('P1Y1M1DT1H1M'), 'P1Y1M1DT1H1M'], - [new \DateInterval('P1Y1M1DT1H1M1S'), 'P1Y1M1DT1H1M1S'], - [new \DateInterval('P0Y'), 'P0Y'], - [new \DateInterval('PT0S'), 'P0Y'], - ]; + yield [[1, 2, 3], '[1, 2, 3]']; + yield [new \ArrayIterator([1, 2, 3]), '[1, 2, 3]']; + yield [(static function (): \Generator { yield from [1, 2, 3]; })(), '[1, 2, 3]']; + yield [$datetime, $datetime->format('r')]; + yield [$datetime, $datetime->format('Y-m-d H:i:s'), 'Y-m-d H:i:s']; + yield [123, 123]; + yield ['123', '123']; + yield [new ObjectWithToString('object with to string'), 'object with to string']; + yield [$dateTimeImmutable, $dateTimeImmutable->format('r')]; + yield [$dateTimeImmutable, $dateTimeImmutable->format('Y-m-d H:i:s'), 'Y-m-d H:i:s']; + yield [new \DateInterval('P1Y'), 'P1Y']; + yield [new \DateInterval('P1M'), 'P1M']; + yield [new \DateInterval('P1D'), 'P1D']; + yield [new \DateInterval('PT1H'), 'PT1H']; + yield [new \DateInterval('PT1M'), 'PT1M']; + yield [new \DateInterval('PT1S'), 'PT1S']; + yield [new \DateInterval('P1Y1M'), 'P1Y1M']; + yield [new \DateInterval('P1Y1M1D'), 'P1Y1M1D']; + yield [new \DateInterval('P1Y1M1DT1H'), 'P1Y1M1DT1H']; + yield [new \DateInterval('P1Y1M1DT1H1M'), 'P1Y1M1DT1H1M']; + yield [new \DateInterval('P1Y1M1DT1H1M1S'), 'P1Y1M1DT1H1M1S']; + yield [new \DateInterval('P0Y'), 'P0Y']; + yield [new \DateInterval('PT0S'), 'P0Y']; - return $data; + if (\PHP_VERSION_ID < 80100) { + return; + } + + yield [Element::Hydrogen, 'Hydrogen']; + yield [Suit::Diamonds, 'D']; } } diff --git a/tests/Source/Fixtures/Element.php b/tests/Source/Fixtures/Element.php new file mode 100644 index 00000000..d3cb35da --- /dev/null +++ b/tests/Source/Fixtures/Element.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\Exporter\Tests\Source\Fixtures; + +enum Element +{ + case Carbon; + case Helium; + case Hydrogen; + case Lithium; +} diff --git a/tests/Source/Fixtures/Suit.php b/tests/Source/Fixtures/Suit.php new file mode 100644 index 00000000..fa5bc3aa --- /dev/null +++ b/tests/Source/Fixtures/Suit.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\Exporter\Tests\Source\Fixtures; + +enum Suit: string +{ + case Clubs = 'C'; + case Diamonds = 'D'; + case Hearts = 'H'; + case Spades = 'S'; +}