Deprecated: Constant E_STRICT is deprecated in /home/normanv/www/annuairepro/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/normanv/www/annuairepro/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

vendor/symfony/serializer/Normalizer/DataUriNormalizer.php line 79

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Serializer\Normalizer;
  11. use Symfony\Component\HttpFoundation\File\File;
  12. use Symfony\Component\Mime\MimeTypeGuesserInterface;
  13. use Symfony\Component\Mime\MimeTypes;
  14. use Symfony\Component\Serializer\Exception\InvalidArgumentException;
  15. use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
  16. /**
  17.  * Normalizes an {@see \SplFileInfo} object to a data URI.
  18.  * Denormalizes a data URI to a {@see \SplFileObject} object.
  19.  *
  20.  * @author Kévin Dunglas <dunglas@gmail.com>
  21.  */
  22. class DataUriNormalizer implements NormalizerInterfaceDenormalizerInterfaceCacheableSupportsMethodInterface
  23. {
  24.     private const SUPPORTED_TYPES = [
  25.         \SplFileInfo::class => true,
  26.         \SplFileObject::class => true,
  27.         File::class => true,
  28.     ];
  29.     /**
  30.      * @var MimeTypeGuesserInterface|null
  31.      */
  32.     private $mimeTypeGuesser;
  33.     public function __construct(MimeTypeGuesserInterface $mimeTypeGuesser null)
  34.     {
  35.         if (!$mimeTypeGuesser && class_exists(MimeTypes::class)) {
  36.             $mimeTypeGuesser MimeTypes::getDefault();
  37.         }
  38.         $this->mimeTypeGuesser $mimeTypeGuesser;
  39.     }
  40.     /**
  41.      * {@inheritdoc}
  42.      *
  43.      * @return string
  44.      */
  45.     public function normalize($objectstring $format null, array $context = [])
  46.     {
  47.         if (!$object instanceof \SplFileInfo) {
  48.             throw new InvalidArgumentException('The object must be an instance of "\SplFileInfo".');
  49.         }
  50.         $mimeType $this->getMimeType($object);
  51.         $splFileObject $this->extractSplFileObject($object);
  52.         $data '';
  53.         $splFileObject->rewind();
  54.         while (!$splFileObject->eof()) {
  55.             $data .= $splFileObject->fgets();
  56.         }
  57.         if ('text' === explode('/'$mimeType2)[0]) {
  58.             return sprintf('data:%s,%s'$mimeTyperawurlencode($data));
  59.         }
  60.         return sprintf('data:%s;base64,%s'$mimeTypebase64_encode($data));
  61.     }
  62.     /**
  63.      * {@inheritdoc}
  64.      */
  65.     public function supportsNormalization($datastring $format null)
  66.     {
  67.         return $data instanceof \SplFileInfo;
  68.     }
  69.     /**
  70.      * {@inheritdoc}
  71.      *
  72.      * Regex adapted from Brian Grinstead code.
  73.      *
  74.      * @see https://gist.github.com/bgrins/6194623
  75.      *
  76.      * @return \SplFileInfo
  77.      *
  78.      * @throws InvalidArgumentException
  79.      * @throws NotNormalizableValueException
  80.      */
  81.     public function denormalize($datastring $typestring $format null, array $context = [])
  82.     {
  83.         if (null === $data || !preg_match('/^data:([a-z0-9][a-z0-9\!\#\$\&\-\^\_\+\.]{0,126}\/[a-z0-9][a-z0-9\!\#\$\&\-\^\_\+\.]{0,126}(;[a-z0-9\-]+\=[a-z0-9\-]+)?)?(;base64)?,[a-z0-9\!\$\&\\\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*$/i'$data)) {
  84.             throw NotNormalizableValueException::createForUnexpectedDataType('The provided "data:" URI is not valid.'$data, ['string'], $context['deserialization_path'] ?? nulltrue);
  85.         }
  86.         try {
  87.             switch ($type) {
  88.                 case File::class:
  89.                     if (!class_exists(File::class)) {
  90.                         throw new InvalidArgumentException(sprintf('Cannot denormalize to a "%s" without the HttpFoundation component installed. Try running "composer require symfony/http-foundation".'File::class));
  91.                     }
  92.                     return new File($datafalse);
  93.                 case 'SplFileObject':
  94.                 case 'SplFileInfo':
  95.                     return new \SplFileObject($data);
  96.             }
  97.         } catch (\RuntimeException $exception) {
  98.             throw NotNormalizableValueException::createForUnexpectedDataType($exception->getMessage(), $data, ['string'], $context['deserialization_path'] ?? nullfalse$exception->getCode(), $exception);
  99.         }
  100.         throw new InvalidArgumentException(sprintf('The class parameter "%s" is not supported. It must be one of "SplFileInfo", "SplFileObject" or "Symfony\Component\HttpFoundation\File\File".'$type));
  101.     }
  102.     /**
  103.      * {@inheritdoc}
  104.      */
  105.     public function supportsDenormalization($datastring $typestring $format null)
  106.     {
  107.         return isset(self::SUPPORTED_TYPES[$type]);
  108.     }
  109.     /**
  110.      * {@inheritdoc}
  111.      */
  112.     public function hasCacheableSupportsMethod(): bool
  113.     {
  114.         return __CLASS__ === static::class;
  115.     }
  116.     /**
  117.      * Gets the mime type of the object. Defaults to application/octet-stream.
  118.      */
  119.     private function getMimeType(\SplFileInfo $object): string
  120.     {
  121.         if ($object instanceof File) {
  122.             return $object->getMimeType();
  123.         }
  124.         if ($this->mimeTypeGuesser && $mimeType $this->mimeTypeGuesser->guessMimeType($object->getPathname())) {
  125.             return $mimeType;
  126.         }
  127.         return 'application/octet-stream';
  128.     }
  129.     /**
  130.      * Returns the \SplFileObject instance associated with the given \SplFileInfo instance.
  131.      */
  132.     private function extractSplFileObject(\SplFileInfo $object): \SplFileObject
  133.     {
  134.         if ($object instanceof \SplFileObject) {
  135.             return $object;
  136.         }
  137.         return $object->openFile();
  138.     }
  139. }