-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
- Loading branch information
There are no files selected for viewing
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 | ||
Check failure on line 27 in src/Formatter/BoolFormatter.php GitHub Actions / PHPStan
|
||
{ | ||
foreach ($data as $key => $value) { | ||
if (!\is_bool($value)) { | ||
continue; | ||
} | ||
|
||
$data[$key] = $value ? $this->trueLabel : $this->falseLabel; | ||
} | ||
|
||
return $data; | ||
} | ||
} |
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 | ||
Check failure on line 29 in src/Formatter/DateIntervalFormatter.php GitHub Actions / PHPStan
|
||
{ | ||
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 : ''); | ||
} | ||
} |
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 | ||
Check failure on line 23 in src/Formatter/DateTimeFormatter.php GitHub Actions / PHPStan
|
||
{ | ||
foreach ($data as $key => $value) { | ||
if (!$value instanceof \DateTimeInterface) { | ||
continue; | ||
} | ||
|
||
$data[$key] = $value->format($this->dateTimeFormat); | ||
} | ||
|
||
return $data; | ||
} | ||
} |
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 | ||
Check failure on line 23 in src/Formatter/EnumFormatter.php GitHub Actions / PHPStan
|
||
{ | ||
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?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 | ||
{ | ||
public function format(array $data): array; | ||
Check failure on line 18 in src/Formatter/FormatterInterface.php GitHub Actions / PHPStan
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?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 IterableFormatter implements FormatterInterface | ||
{ | ||
public function __construct( | ||
private array $formatters = [] | ||
) { | ||
} | ||
|
||
public function format(array $data, array $formatters = []): array | ||
{ | ||
$this->formatters = $formatters; | ||
foreach ($data as $key => $value) { | ||
if (!is_iterable($value)) { | ||
continue; | ||
} | ||
|
||
if ($value instanceof \Iterator) { | ||
$value = iterator_to_array($value); | ||
} | ||
|
||
$data[$key] = '['.implode(', ', array_map([$this, 'formatFromIterable'], $value)).']'; | ||
Check failure on line 35 in src/Formatter/IterableFormatter.php GitHub Actions / PsalmInvalidArgument
Check failure on line 35 in src/Formatter/IterableFormatter.php GitHub Actions / PsalmInvalidArrayAccess
|
||
} | ||
|
||
return $data; | ||
} | ||
|
||
private function formatFromIterable(mixed $value): mixed | ||
{ | ||
foreach ($this->formatters as $formatter) { | ||
$value = $formatter->format($value, $this->formatters); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?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 StringableFormatter implements FormatterInterface | ||
{ | ||
public function format(array $data): array | ||
{ | ||
foreach ($data as $key => $value) { | ||
if (!$value instanceof \Stringable) { | ||
continue; | ||
} | ||
|
||
$data[$key] = (string) $value; | ||
} | ||
|
||
return $data; | ||
} | ||
} |