4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Normalizer;
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;
21 * Normalizes an {@see \SplFileInfo} object to a data URI.
22 * Denormalizes a data URI to a {@see \SplFileObject} object.
24 * @author Kévin Dunglas <dunglas@gmail.com>
26 class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface
28 private static $supportedTypes = array(
29 \SplFileInfo::class => true,
30 \SplFileObject::class => true,
35 * @var MimeTypeGuesserInterface
37 private $mimeTypeGuesser;
39 public function __construct(MimeTypeGuesserInterface $mimeTypeGuesser = null)
41 if (null === $mimeTypeGuesser && class_exists('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser')) {
42 $mimeTypeGuesser = MimeTypeGuesser::getInstance();
45 $this->mimeTypeGuesser = $mimeTypeGuesser;
51 public function normalize($object, $format = null, array $context = array())
53 if (!$object instanceof \SplFileInfo) {
54 throw new InvalidArgumentException('The object must be an instance of "\SplFileInfo".');
57 $mimeType = $this->getMimeType($object);
58 $splFileObject = $this->extractSplFileObject($object);
62 $splFileObject->rewind();
63 while (!$splFileObject->eof()) {
64 $data .= $splFileObject->fgets();
67 if ('text' === explode('/', $mimeType, 2)[0]) {
68 return sprintf('data:%s,%s', $mimeType, rawurlencode($data));
71 return sprintf('data:%s;base64,%s', $mimeType, base64_encode($data));
77 public function supportsNormalization($data, $format = null)
79 return $data instanceof \SplFileInfo;
85 * Regex adapted from Brian Grinstead code.
87 * @see https://gist.github.com/bgrins/6194623
89 * @throws InvalidArgumentException
90 * @throws NotNormalizableValueException
92 public function denormalize($data, $class, $format = null, array $context = array())
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.');
100 case 'Symfony\Component\HttpFoundation\File\File':
101 return new File($data, false);
103 case 'SplFileObject':
105 return new \SplFileObject($data);
107 } catch (\RuntimeException $exception) {
108 throw new NotNormalizableValueException($exception->getMessage(), $exception->getCode(), $exception);
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));
117 public function supportsDenormalization($data, $type, $format = null)
119 return isset(self::$supportedTypes[$type]);
123 * Gets the mime type of the object. Defaults to application/octet-stream.
125 * @param \SplFileInfo $object
129 private function getMimeType(\SplFileInfo $object)
131 if ($object instanceof File) {
132 return $object->getMimeType();
135 if ($this->mimeTypeGuesser && $mimeType = $this->mimeTypeGuesser->guess($object->getPathname())) {
139 return 'application/octet-stream';
143 * Returns the \SplFileObject instance associated with the given \SplFileInfo instance.
145 * @param \SplFileInfo $object
147 * @return \SplFileObject
149 private function extractSplFileObject(\SplFileInfo $object)
151 if ($object instanceof \SplFileObject) {
155 return $object->openFile();