-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Add a data: URI normalizer #16164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
efa58f4
28b1e13
790ad3a
b0fe18c
7e5e845
f207713
f4ee396
53e2b26
c03aa28
68869f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,19 +44,13 @@ public function __construct(MimeTypeGuesserInterface $mimeTypeGuesser = null) | |
*/ | ||
public function normalize($object, $format = null, array $context = array()) | ||
{ | ||
if ($object instanceof File) { | ||
$mimeType = $object->getMimeType(); | ||
} elseif ($this->mimeTypeGuesser) { | ||
$mimeType = $this->mimeTypeGuesser->guess($object->getPathname()); | ||
} else { | ||
$mimeType = 'application/octet-stream'; | ||
if (!$object instanceof \SplFileInfo) { | ||
throw new InvalidArgumentException('The object must be an instance of "\SplFileInfo".'); | ||
} | ||
|
||
$mimeType = $this->getMimeType($object); | ||
list($typeName) = explode('/', $mimeType, 2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minutiae detail, but I'd like this line moved right before the |
||
|
||
if (!$object instanceof \SplFileObject) { | ||
$object = $object->openFile(); | ||
} | ||
$object = $this->extractSplFileObject($object); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe call this |
||
|
||
$data = ''; | ||
|
||
|
@@ -125,4 +119,40 @@ public function supportsDenormalization($data, $type, $format = null) | |
|
||
return isset($supportedTypes[$type]); | ||
} | ||
|
||
/** | ||
* Gets the mime type of the object. Defaults to application/octet-stream. | ||
* | ||
* @param \SplFileInfo $object | ||
* | ||
* @return string | ||
*/ | ||
private function getMimeType(\SplFileInfo $object) | ||
{ | ||
if ($object instanceof File) { | ||
return $object->getMimeType(); | ||
} | ||
|
||
if ($this->mimeTypeGuesser && $mimeType = $this->mimeTypeGuesser->guess($object->getPathname())) { | ||
return $mimeType; | ||
} | ||
|
||
return 'application/octet-stream'; | ||
} | ||
|
||
/** | ||
* Returns the \SplFileObject instance associated with the given \SplFileInfo instance. | ||
* | ||
* @param \SplFileInfo $object | ||
* | ||
* @return \SplFileObject | ||
*/ | ||
private function extractSplFileObject(\SplFileInfo $object) | ||
{ | ||
if ($object instanceof \SplFileObject) { | ||
return $object; | ||
} | ||
|
||
return $object->openFile(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why you limit
explode()
too 2 & use just one variable?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it won't work: