Skip to content

Commit

Permalink
Allow to extract values from enum types at `AbstractPropertySourceIte…
Browse files Browse the repository at this point in the history
…rator::getValue()`
  • Loading branch information
phansys committed Oct 21, 2023
1 parent e3be944 commit e500e94
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 27 deletions.
2 changes: 2 additions & 0 deletions src/Source/AbstractPropertySourceIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
54 changes: 27 additions & 27 deletions tests/Source/AbstractPropertySourceIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -48,32 +50,30 @@ 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'],
];

return $data;
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'];
yield [Element::Hydrogen, 'Hydrogen'];
yield [Suit::Diamonds, 'D'];
}
}
22 changes: 22 additions & 0 deletions tests/Source/Fixtures/Element.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* 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;
}
22 changes: 22 additions & 0 deletions tests/Source/Fixtures/Suit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* 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';
}

0 comments on commit e500e94

Please sign in to comment.