Submit
Path:
~
/
/
usr
/
local
/
psa
/
admin
/
plib
/
modules
/
performance-booster
/
vendor
/
symfony
/
serializer
/
Normalizer
/
File Content:
DateTimeNormalizer.php
<?php /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace PerformanceBooster\Symfony\Component\Serializer\Normalizer; use PerformanceBooster\Symfony\Component\PropertyInfo\Type; use PerformanceBooster\Symfony\Component\Serializer\Exception\InvalidArgumentException; use PerformanceBooster\Symfony\Component\Serializer\Exception\NotNormalizableValueException; /** * Normalizes an object implementing the {@see \DateTimeInterface} to a date string. * Denormalizes a date string to an instance of {@see \DateTime} or {@see \DateTimeImmutable}. * * @author Kévin Dunglas <dunglas@gmail.com> * * @final since Symfony 6.3 */ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface { public const FORMAT_KEY = 'datetime_format'; public const TIMEZONE_KEY = 'datetime_timezone'; private array $defaultContext = [self::FORMAT_KEY => \DateTimeInterface::RFC3339, self::TIMEZONE_KEY => null]; private const SUPPORTED_TYPES = [\DateTimeInterface::class => \true, \DateTimeImmutable::class => \true, \DateTime::class => \true]; public function __construct(array $defaultContext = []) { $this->setDefaultContext($defaultContext); } public function setDefaultContext(array $defaultContext) : void { $this->defaultContext = \array_merge($this->defaultContext, $defaultContext); } public function getSupportedTypes(?string $format) : array { $isCacheable = __CLASS__ === static::class || $this->hasCacheableSupportsMethod(); return [\DateTimeInterface::class => $isCacheable, \DateTimeImmutable::class => $isCacheable, \DateTime::class => $isCacheable]; } /** * @throws InvalidArgumentException */ public function normalize(mixed $object, ?string $format = null, array $context = []) : string { if (!$object instanceof \DateTimeInterface) { throw new InvalidArgumentException('The object must implement the "\\DateTimeInterface".'); } $dateTimeFormat = $context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY]; $timezone = $this->getTimezone($context); if (null !== $timezone) { $object = clone $object; $object = $object->setTimezone($timezone); } return $object->format($dateTimeFormat); } /** * @param array $context */ public function supportsNormalization(mixed $data, ?string $format = null) : bool { return $data instanceof \DateTimeInterface; } /** * @throws NotNormalizableValueException */ public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []) : \DateTimeInterface { if (\is_int($data) || \is_float($data)) { switch ($context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY] ?? null) { case 'U': $data = \sprintf('%d', $data); break; case 'U.u': $data = \sprintf('%.6F', $data); break; } } if (!\is_string($data) || '' === \trim($data)) { throw NotNormalizableValueException::createForUnexpectedDataType('The data is either not an string, an empty string, or null; you should pass a string that can be parsed with the passed format or a valid DateTime string.', $data, [Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, \true); } try { if (\DateTimeInterface::class === $type) { $type = \DateTimeImmutable::class; } $timezone = $this->getTimezone($context); $dateTimeFormat = $context[self::FORMAT_KEY] ?? null; if (null !== $dateTimeFormat) { if (\false !== ($object = $type::createFromFormat($dateTimeFormat, $data, $timezone))) { return $object; } $dateTimeErrors = $type::getLastErrors(); throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Parsing datetime string "%s" using format "%s" resulted in %d errors: ', $data, $dateTimeFormat, $dateTimeErrors['error_count']) . "\n" . \implode("\n", $this->formatDateTimeErrors($dateTimeErrors['errors'])), $data, [Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, \true); } $defaultDateTimeFormat = $this->defaultContext[self::FORMAT_KEY] ?? null; if (null !== $defaultDateTimeFormat) { if (\false !== ($object = $type::createFromFormat($defaultDateTimeFormat, $data, $timezone))) { return $object; } } return new $type($data, $timezone); } catch (NotNormalizableValueException $e) { throw $e; } catch (\Exception $e) { throw NotNormalizableValueException::createForUnexpectedDataType($e->getMessage(), $data, [Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, \false, $e->getCode(), $e); } } /** * @param array $context */ public function supportsDenormalization(mixed $data, string $type, ?string $format = null) : bool { return isset(self::SUPPORTED_TYPES[$type]); } /** * @deprecated since Symfony 6.3, use "getSupportedTypes()" instead */ public function hasCacheableSupportsMethod() : bool { trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, implement "%s::getSupportedTypes()" instead.', __METHOD__, \get_debug_type($this)); return __CLASS__ === static::class; } /** * Formats datetime errors. * * @return string[] */ private function formatDateTimeErrors(array $errors) : array { $formattedErrors = []; foreach ($errors as $pos => $message) { $formattedErrors[] = \sprintf('at position %d: %s', $pos, $message); } return $formattedErrors; } private function getTimezone(array $context) : ?\DateTimeZone { $dateTimeZone = $context[self::TIMEZONE_KEY] ?? $this->defaultContext[self::TIMEZONE_KEY]; if (null === $dateTimeZone) { return null; } return $dateTimeZone instanceof \DateTimeZone ? $dateTimeZone : new \DateTimeZone($dateTimeZone); } }
Submit
FILE
FOLDER
INFO
Name
Size
Permission
Action
AbstractNormalizer.php
25063 bytes
0644
AbstractObjectNormalizer.php
38443 bytes
0644
ArrayDenormalizer.php
4381 bytes
0644
BackedEnumNormalizer.php
3643 bytes
0644
CacheableSupportsMethodInterface.php
806 bytes
0644
ConstraintViolationListNormalizer.php
4604 bytes
0644
ContextAwareDenormalizerInterface.php
827 bytes
0644
ContextAwareNormalizerInterface.php
801 bytes
0644
CustomNormalizer.php
2893 bytes
0644
DataUriNormalizer.php
5874 bytes
0644
DateIntervalNormalizer.php
5025 bytes
0644
DateTimeNormalizer.php
6617 bytes
0644
DateTimeZoneNormalizer.php
2974 bytes
0644
DenormalizableInterface.php
1658 bytes
0644
DenormalizerAwareInterface.php
563 bytes
0644
DenormalizerAwareTrait.php
639 bytes
0644
DenormalizerInterface.php
3575 bytes
0644
FormErrorNormalizer.php
2517 bytes
0644
GetSetMethodNormalizer.php
8172 bytes
0644
JsonSerializableNormalizer.php
2620 bytes
0644
MimeMessageNormalizer.php
5231 bytes
0644
NormalizableInterface.php
1475 bytes
0644
NormalizerAwareInterface.php
553 bytes
0644
NormalizerAwareTrait.php
623 bytes
0644
NormalizerInterface.php
2993 bytes
0644
ObjectNormalizer.php
10093 bytes
0644
ObjectToPopulateTrait.php
1073 bytes
0644
ProblemNormalizer.php
4867 bytes
0644
PropertyNormalizer.php
8364 bytes
0644
TranslatableNormalizer.php
1962 bytes
0644
UidNormalizer.php
4541 bytes
0644
UnwrappingDenormalizer.php
2708 bytes
0644
N4ST4R_ID | Naxtarrr