Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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

Closed
wants to merge 10 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Take into account @weaverryan's comments
  • Loading branch information
dunglas committed Jan 17, 2016
commit 53e2b263178474a07611a51ab2a9956e0b79ab1c
50 changes: 40 additions & 10 deletions 50 src/Symfony/Component/Serializer/Normalizer/DataUriNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

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?

Copy link
Member Author

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:

php > var_dump(explode('/', 'text/plain', 1));
array(1) {
  [0] =>
  string(10) "text/plain"
}

Copy link
Member

Choose a reason for hiding this comment

The 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 statement that actually uses $typeName - it's not needed up here.


if (!$object instanceof \SplFileObject) {
$object = $object->openFile();
}
$object = $this->extractSplFileObject($object);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe call this $splFileObject for clarity and so we're not overriding the previous variable (even though we don't need it)


$data = '';

Expand Down Expand Up @@ -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();
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.