-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
1,294 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,33 @@ | ||
UPGRADE 3.x | ||
=========== | ||
|
||
UPGRADE FROM 3.x to 3.x | ||
======================= | ||
|
||
## Formatters for writers | ||
|
||
- Added `Sonata\Exporter\Formatter\BoolFormatter`, `Sonata\Exporter\Formatter\DateIntervalFormatter`, `Sonata\Exporter\Formatter\DateTimeFormatter`, | ||
`Sonata\Exporter\Formatter\EnumFormatter`, `Sonata\Exporter\Formatter\IterableFormatter`, `Sonata\Exporter\Formatter\StringableFormatter` and | ||
`Sonata\Exporter\Formatter\SymfonyTranslationFormatter` | ||
classes to be used within implementations of `Sonata\Exporter\Formatter\Writer\FormatAwareInterface`. | ||
- Deprecated `Sonata\Exporter\Writer\FormattedBoolWriter`, use `Sonata\Exporter\Formatter\BoolFormatter` instead. | ||
- Deprecated arguments `dateTimeFormat` and `useBackedEnumValue` in `Sonata\Exporter\Source\AbstractPropertySourceIterator::__construct()` and | ||
their children classes. To disable the source formatting you MUST pass `true` in argument `disableSourceFormatters` and use | ||
`Sonata\Exporter\Formatter\Writer\FormatAwareInterface::addFormatter()` in your writers instead. | ||
|
||
## Symfony Bridge | ||
|
||
- Added `sonata_exporter.writers.{writer}.formatters` configuration in order to determine which formatters will be used by each writer. | ||
|
||
```yaml | ||
sonata_exporter: | ||
writers: | ||
csv: | ||
formatters: | ||
- datetime | ||
- enum | ||
# - ... | ||
``` | ||
|
||
By default, "bool", "dateinterval", "datetime", "enum", "iterable" and "stringable" formatters are configured. | ||
If "symfony/translations-contracts" is installed, "symfony_translator" formatter is also enabled. |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?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\Formatter; | ||
|
||
final class BoolFormatter implements FormatterInterface | ||
{ | ||
private const LABEL_TRUE = 'yes'; | ||
private const LABEL_FALSE = 'no'; | ||
|
||
public function __construct( | ||
private string $trueLabel = self::LABEL_TRUE, | ||
private string $falseLabel = self::LABEL_FALSE | ||
) { | ||
} | ||
|
||
public function format(array $data): array | ||
{ | ||
foreach ($data as $key => $value) { | ||
if (!\is_bool($value)) { | ||
continue; | ||
} | ||
|
||
$data[$key] = $value ? $this->trueLabel : $this->falseLabel; | ||
} | ||
|
||
return $data; | ||
} | ||
} |
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,69 @@ | ||
<?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\Formatter; | ||
|
||
final class DateIntervalFormatter implements FormatterInterface | ||
{ | ||
private const DATE_PARTS = [ | ||
'y' => 'Y', | ||
'm' => 'M', | ||
'd' => 'D', | ||
]; | ||
private const TIME_PARTS = [ | ||
'h' => 'H', | ||
'i' => 'M', | ||
's' => 'S', | ||
]; | ||
|
||
public function format(array $data): array | ||
{ | ||
foreach ($data as $key => $value) { | ||
if (!$value instanceof \DateInterval) { | ||
continue; | ||
} | ||
|
||
$data[$key] = self::getDuration($value); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* @return string An ISO8601 duration | ||
*/ | ||
private static function getDuration(\DateInterval $interval): string | ||
{ | ||
$datePart = ''; | ||
|
||
foreach (self::DATE_PARTS as $datePartAttribute => $datePartAttributeString) { | ||
if ($interval->$datePartAttribute !== 0) { | ||
$datePart .= $interval->$datePartAttribute.$datePartAttributeString; | ||
} | ||
} | ||
|
||
$timePart = ''; | ||
|
||
foreach (self::TIME_PARTS as $timePartAttribute => $timePartAttributeString) { | ||
if ($interval->$timePartAttribute !== 0) { | ||
$timePart .= $interval->$timePartAttribute.$timePartAttributeString; | ||
} | ||
} | ||
|
||
if ('' === $datePart && '' === $timePart) { | ||
return 'P0Y'; | ||
} | ||
|
||
return 'P'.$datePart.('' !== $timePart ? 'T'.$timePart : ''); | ||
} | ||
} |
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,35 @@ | ||
<?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\Formatter; | ||
|
||
final class DateTimeFormatter implements FormatterInterface | ||
{ | ||
public function __construct( | ||
private string $dateTimeFormat = \DateTimeInterface::RFC2822 | ||
) { | ||
} | ||
|
||
public function format(array $data): array | ||
{ | ||
foreach ($data as $key => $value) { | ||
if (!$value instanceof \DateTimeInterface) { | ||
continue; | ||
} | ||
|
||
$data[$key] = $value->format($this->dateTimeFormat); | ||
} | ||
|
||
return $data; | ||
} | ||
} |
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,41 @@ | ||
<?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\Formatter; | ||
|
||
final class EnumFormatter implements FormatterInterface | ||
{ | ||
public function __construct( | ||
private bool $useBackedEnumValue = true | ||
) { | ||
} | ||
|
||
public function format(array $data): array | ||
{ | ||
foreach ($data as $key => $value) { | ||
if (!$value instanceof \UnitEnum) { | ||
continue; | ||
} | ||
|
||
if ($this->useBackedEnumValue && $value instanceof \BackedEnum) { | ||
$data[$key] = $value->value; | ||
|
||
continue; | ||
} | ||
|
||
$data[$key] = $value->name; | ||
} | ||
|
||
return $data; | ||
} | ||
} |
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,24 @@ | ||
<?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\Formatter; | ||
|
||
interface FormatterInterface | ||
{ | ||
/** | ||
* @param array<int|string, mixed> $data | ||
* | ||
* @return array<int|string, mixed> | ||
*/ | ||
public function format(array $data): array; | ||
} |
Oops, something went wrong.