Skip to content

Instantly share code, notes, and snippets.

@lsv
Last active August 13, 2023 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsv/29201054e056cf16b465a99f5fce7cdc to your computer and use it in GitHub Desktop.
Save lsv/29201054e056cf16b465a99f5fce7cdc to your computer and use it in GitHub Desktop.
symfony carbon normalizer
<?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;
}
}
<?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,
];
}
}
<?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