comparison vendor/symfony/serializer/Normalizer/DataUriNormalizer.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 7a779792577d
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
13 13
14 use Symfony\Component\HttpFoundation\File\File; 14 use Symfony\Component\HttpFoundation\File\File;
15 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser; 15 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
16 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; 16 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
17 use Symfony\Component\Serializer\Exception\InvalidArgumentException; 17 use Symfony\Component\Serializer\Exception\InvalidArgumentException;
18 use Symfony\Component\Serializer\Exception\UnexpectedValueException; 18 use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
19 19
20 /** 20 /**
21 * Normalizes an {@see \SplFileInfo} object to a data URI. 21 * Normalizes an {@see \SplFileInfo} object to a data URI.
22 * Denormalizes a data URI to a {@see \SplFileObject} object. 22 * Denormalizes a data URI to a {@see \SplFileObject} object.
23 * 23 *
24 * @author Kévin Dunglas <dunglas@gmail.com> 24 * @author Kévin Dunglas <dunglas@gmail.com>
25 */ 25 */
26 class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface 26 class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface
27 { 27 {
28 private static $supportedTypes = array(
29 \SplFileInfo::class => true,
30 \SplFileObject::class => true,
31 File::class => true,
32 );
33
28 /** 34 /**
29 * @var MimeTypeGuesserInterface 35 * @var MimeTypeGuesserInterface
30 */ 36 */
31 private $mimeTypeGuesser; 37 private $mimeTypeGuesser;
32 38
77 * {@inheritdoc} 83 * {@inheritdoc}
78 * 84 *
79 * Regex adapted from Brian Grinstead code. 85 * Regex adapted from Brian Grinstead code.
80 * 86 *
81 * @see https://gist.github.com/bgrins/6194623 87 * @see https://gist.github.com/bgrins/6194623
88 *
89 * @throws InvalidArgumentException
90 * @throws NotNormalizableValueException
82 */ 91 */
83 public function denormalize($data, $class, $format = null, array $context = array()) 92 public function denormalize($data, $class, $format = null, array $context = array())
84 { 93 {
85 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)) { 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)) {
86 throw new UnexpectedValueException('The provided "data:" URI is not valid.'); 95 throw new NotNormalizableValueException('The provided "data:" URI is not valid.');
87 } 96 }
88 97
89 try { 98 try {
90 switch ($class) { 99 switch ($class) {
91 case 'Symfony\Component\HttpFoundation\File\File': 100 case 'Symfony\Component\HttpFoundation\File\File':
94 case 'SplFileObject': 103 case 'SplFileObject':
95 case 'SplFileInfo': 104 case 'SplFileInfo':
96 return new \SplFileObject($data); 105 return new \SplFileObject($data);
97 } 106 }
98 } catch (\RuntimeException $exception) { 107 } catch (\RuntimeException $exception) {
99 throw new UnexpectedValueException($exception->getMessage(), $exception->getCode(), $exception); 108 throw new NotNormalizableValueException($exception->getMessage(), $exception->getCode(), $exception);
100 } 109 }
101 110
102 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)); 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));
103 } 112 }
104 113
105 /** 114 /**
106 * {@inheritdoc} 115 * {@inheritdoc}
107 */ 116 */
108 public function supportsDenormalization($data, $type, $format = null) 117 public function supportsDenormalization($data, $type, $format = null)
109 { 118 {
110 $supportedTypes = array( 119 return isset(self::$supportedTypes[$type]);
111 \SplFileInfo::class => true,
112 \SplFileObject::class => true,
113 'Symfony\Component\HttpFoundation\File\File' => true,
114 );
115
116 return isset($supportedTypes[$type]);
117 } 120 }
118 121
119 /** 122 /**
120 * Gets the mime type of the object. Defaults to application/octet-stream. 123 * Gets the mime type of the object. Defaults to application/octet-stream.
121 * 124 *