comparison vendor/symfony/serializer/Normalizer/DataUriNormalizer.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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\UnexpectedValueException;
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 /**
29 * @var MimeTypeGuesserInterface
30 */
31 private $mimeTypeGuesser;
32
33 public function __construct(MimeTypeGuesserInterface $mimeTypeGuesser = null)
34 {
35 if (null === $mimeTypeGuesser && class_exists('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser')) {
36 $mimeTypeGuesser = MimeTypeGuesser::getInstance();
37 }
38
39 $this->mimeTypeGuesser = $mimeTypeGuesser;
40 }
41
42 /**
43 * {@inheritdoc}
44 */
45 public function normalize($object, $format = null, array $context = array())
46 {
47 if (!$object instanceof \SplFileInfo) {
48 throw new InvalidArgumentException('The object must be an instance of "\SplFileInfo".');
49 }
50
51 $mimeType = $this->getMimeType($object);
52 $splFileObject = $this->extractSplFileObject($object);
53
54 $data = '';
55
56 $splFileObject->rewind();
57 while (!$splFileObject->eof()) {
58 $data .= $splFileObject->fgets();
59 }
60
61 if ('text' === explode('/', $mimeType, 2)[0]) {
62 return sprintf('data:%s,%s', $mimeType, rawurlencode($data));
63 }
64
65 return sprintf('data:%s;base64,%s', $mimeType, base64_encode($data));
66 }
67
68 /**
69 * {@inheritdoc}
70 */
71 public function supportsNormalization($data, $format = null)
72 {
73 return $data instanceof \SplFileInfo;
74 }
75
76 /**
77 * {@inheritdoc}
78 *
79 * Regex adapted from Brian Grinstead code.
80 *
81 * @see https://gist.github.com/bgrins/6194623
82 *
83 * @throws InvalidArgumentException
84 * @throws UnexpectedValueException
85 */
86 public function denormalize($data, $class, $format = null, array $context = array())
87 {
88 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)) {
89 throw new UnexpectedValueException('The provided "data:" URI is not valid.');
90 }
91
92 try {
93 switch ($class) {
94 case 'Symfony\Component\HttpFoundation\File\File':
95 return new File($data, false);
96
97 case 'SplFileObject':
98 case 'SplFileInfo':
99 return new \SplFileObject($data);
100 }
101 } catch (\RuntimeException $exception) {
102 throw new UnexpectedValueException($exception->getMessage(), $exception->getCode(), $exception);
103 }
104
105 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));
106 }
107
108 /**
109 * {@inheritdoc}
110 */
111 public function supportsDenormalization($data, $type, $format = null)
112 {
113 $supportedTypes = array(
114 \SplFileInfo::class => true,
115 \SplFileObject::class => true,
116 'Symfony\Component\HttpFoundation\File\File' => true,
117 );
118
119 return isset($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 }