Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / serializer / Normalizer / DataUriNormalizer.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Serializer\Normalizer;
13
14 use Symfony\Component\HttpFoundation\File\File;
15 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
16 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
17 use Symfony\Component\Serializer\Exception\InvalidArgumentException;
18 use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
19
20 /**
21  * Normalizes an {@see \SplFileInfo} object to a data URI.
22  * Denormalizes a data URI to a {@see \SplFileObject} object.
23  *
24  * @author Kévin Dunglas <dunglas@gmail.com>
25  */
26 class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface
27 {
28     private static $supportedTypes = array(
29         \SplFileInfo::class => true,
30         \SplFileObject::class => true,
31         File::class => true,
32     );
33
34     /**
35      * @var MimeTypeGuesserInterface
36      */
37     private $mimeTypeGuesser;
38
39     public function __construct(MimeTypeGuesserInterface $mimeTypeGuesser = null)
40     {
41         if (null === $mimeTypeGuesser && class_exists('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser')) {
42             $mimeTypeGuesser = MimeTypeGuesser::getInstance();
43         }
44
45         $this->mimeTypeGuesser = $mimeTypeGuesser;
46     }
47
48     /**
49      * {@inheritdoc}
50      */
51     public function normalize($object, $format = null, array $context = array())
52     {
53         if (!$object instanceof \SplFileInfo) {
54             throw new InvalidArgumentException('The object must be an instance of "\SplFileInfo".');
55         }
56
57         $mimeType = $this->getMimeType($object);
58         $splFileObject = $this->extractSplFileObject($object);
59
60         $data = '';
61
62         $splFileObject->rewind();
63         while (!$splFileObject->eof()) {
64             $data .= $splFileObject->fgets();
65         }
66
67         if ('text' === explode('/', $mimeType, 2)[0]) {
68             return sprintf('data:%s,%s', $mimeType, rawurlencode($data));
69         }
70
71         return sprintf('data:%s;base64,%s', $mimeType, base64_encode($data));
72     }
73
74     /**
75      * {@inheritdoc}
76      */
77     public function supportsNormalization($data, $format = null)
78     {
79         return $data instanceof \SplFileInfo;
80     }
81
82     /**
83      * {@inheritdoc}
84      *
85      * Regex adapted from Brian Grinstead code.
86      *
87      * @see https://gist.github.com/bgrins/6194623
88      *
89      * @throws InvalidArgumentException
90      * @throws NotNormalizableValueException
91      */
92     public function denormalize($data, $class, $format = null, array $context = array())
93     {
94         if (!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)) {
95             throw new NotNormalizableValueException('The provided "data:" URI is not valid.');
96         }
97
98         try {
99             switch ($class) {
100                 case 'Symfony\Component\HttpFoundation\File\File':
101                     return new File($data, false);
102
103                 case 'SplFileObject':
104                 case 'SplFileInfo':
105                     return new \SplFileObject($data);
106             }
107         } catch (\RuntimeException $exception) {
108             throw new NotNormalizableValueException($exception->getMessage(), $exception->getCode(), $exception);
109         }
110
111         throw new InvalidArgumentException(sprintf('The class parameter "%s" is not supported. It must be one of "SplFileInfo", "SplFileObject" or "Symfony\Component\HttpFoundation\File\File".', $class));
112     }
113
114     /**
115      * {@inheritdoc}
116      */
117     public function supportsDenormalization($data, $type, $format = null)
118     {
119         return isset(self::$supportedTypes[$type]);
120     }
121
122     /**
123      * Gets the mime type of the object. Defaults to application/octet-stream.
124      *
125      * @param \SplFileInfo $object
126      *
127      * @return string
128      */
129     private function getMimeType(\SplFileInfo $object)
130     {
131         if ($object instanceof File) {
132             return $object->getMimeType();
133         }
134
135         if ($this->mimeTypeGuesser && $mimeType = $this->mimeTypeGuesser->guess($object->getPathname())) {
136             return $mimeType;
137         }
138
139         return 'application/octet-stream';
140     }
141
142     /**
143      * Returns the \SplFileObject instance associated with the given \SplFileInfo instance.
144      *
145      * @param \SplFileInfo $object
146      *
147      * @return \SplFileObject
148      */
149     private function extractSplFileObject(\SplFileInfo $object)
150     {
151         if ($object instanceof \SplFileObject) {
152             return $object;
153         }
154
155         return $object->openFile();
156     }
157 }