Last active
August 18, 2024 13:57
-
-
Save lsv/29201054e056cf16b465a99f5fce7cdc to your computer and use it in GitHub Desktop.
symfony carbon normalizer
This file contains hidden or 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
<?php | |
namespace App\Command; | |
use Carbon\Carbon; | |
use Symfony\Component\Console\Attribute\AsCommand; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Serializer\SerializerInterface; | |
#[AsCommand( | |
name: 'app:carbon', | |
description: 'Add a short description for your command', | |
)] | |
class CarbonCommand extends Command | |
{ | |
public function __construct(private readonly SerializerInterface $serializer) | |
{ | |
parent::__construct(); | |
} | |
protected function execute(InputInterface $input, OutputInterface $output): int | |
{ | |
$data = [ | |
'datetime' => new \DateTime(), | |
'carbon' => Carbon::now(), | |
]; | |
$output->write($this->serializer->serialize($data, 'json')); | |
return Command::SUCCESS; | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace App; | |
use Carbon\Carbon; | |
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | |
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | |
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | |
class CarbonNormalizer implements NormalizerInterface, DenormalizerInterface | |
{ | |
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): string | |
{ | |
return 'ouputting carbon'; | |
} | |
public function supportsDenormalization(mixed $data, string $type, string $format = null): bool | |
{ | |
return $data instanceof Carbon; | |
} | |
public function normalize(mixed $object, string $format = null, array $context = []) | |
{ | |
if (!$object instanceof Carbon) { | |
throw new InvalidArgumentException('The object must implement the "\Carbon\Carbon".'); | |
} | |
return 'Hello im a carbon instance - ' . $object->toAtomString(); | |
} | |
public function supportsNormalization(mixed $data, string $format = null): bool | |
{ | |
return $data instanceof Carbon; | |
} | |
public function getSupportedTypes(?string $format): array | |
{ | |
return [ | |
Carbon::class => false, | |
]; | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace App; | |
use Carbon\Carbon; | |
use Symfony\Component\DependencyInjection\Attribute\AsDecorator; | |
use Symfony\Component\DependencyInjection\Attribute\AutowireDecorated; | |
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; | |
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | |
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | |
#[AsDecorator('serializer.normalizer.datetime')] | |
class DateTimeNormalizerDecorator implements NormalizerInterface, DenormalizerInterface | |
{ | |
public function __construct( | |
#[AutowireDecorated] private DateTimeNormalizer $inner | |
) | |
{ | |
} | |
public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool | |
{ | |
if ($data instanceof Carbon) { | |
return false; | |
} | |
return $this->inner->supportsNormalization($data, $format, $context); | |
} | |
public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool | |
{ | |
if ($data instanceof Carbon) { | |
return false; | |
} | |
return $this->inner->supportsDenormalization($data, $type, $context); | |
} | |
public function denormalize(mixed $data, string $type, string $format = null, array $context = []) | |
{ | |
return $this->inner->denormalize($data, $type, $format, $context); | |
} | |
public function normalize(mixed $object, string $format = null, array $context = []) | |
{ | |
return $this->inner->normalize($object, $format, $context); | |
} | |
public function getSupportedTypes(?string $format) | |
{ | |
return $this->inner->getSupportedTypes($format); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment