use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
-use Symfony\Component\Serializer\Exception\UnexpectedValueException;
+use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
/**
* Normalizes an {@see \SplFileInfo} object to a data URI.
*/
class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface
{
+ private static $supportedTypes = array(
+ \SplFileInfo::class => true,
+ \SplFileObject::class => true,
+ File::class => true,
+ );
+
/**
* @var MimeTypeGuesserInterface
*/
* Regex adapted from Brian Grinstead code.
*
* @see https://gist.github.com/bgrins/6194623
+ *
+ * @throws InvalidArgumentException
+ * @throws NotNormalizableValueException
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
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)) {
- throw new UnexpectedValueException('The provided "data:" URI is not valid.');
+ throw new NotNormalizableValueException('The provided "data:" URI is not valid.');
}
try {
return new \SplFileObject($data);
}
} catch (\RuntimeException $exception) {
- throw new UnexpectedValueException($exception->getMessage(), $exception->getCode(), $exception);
+ throw new NotNormalizableValueException($exception->getMessage(), $exception->getCode(), $exception);
}
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));
*/
public function supportsDenormalization($data, $type, $format = null)
{
- $supportedTypes = array(
- \SplFileInfo::class => true,
- \SplFileObject::class => true,
- 'Symfony\Component\HttpFoundation\File\File' => true,
- );
-
- return isset($supportedTypes[$type]);
+ return isset(self::$supportedTypes[$type]);
}
/**