Skip to content

Commit

Permalink
Fix SVGReader test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
meyfa committed Jan 4, 2018
1 parent 9055488 commit dadcabd
Showing 1 changed file with 15 additions and 38 deletions.
53 changes: 15 additions & 38 deletions src/Reading/SVGReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ class SVGReader

/**
* Parses the given string as XML and turns it into an instance of SVGImage.
* Returns null when parsing fails.
*
* @param string $string The XML string to parse.
*
* @return SVGImage An image object representing the parse result.
* @return SVGImage|null An image object representing the parse result.
*/
public function parseString($string)
{
Expand All @@ -87,10 +88,11 @@ public function parseString($string)
* instance of SVGImage.
*
* The path can be on the local file system, or a URL on the network.
* Returns null when parsing fails.
*
* @param string $filename The path or URL of the file to parse.
*
* @return SVGImage An image object representing the parse result.
* @return SVGImage|null An image object representing the parse result.
*/
public function parseFile($filename)
{
Expand All @@ -100,60 +102,35 @@ public function parseFile($filename)

/**
* Parses the given XML document into an instance of SVGImage.
* Returns null when parsing fails.
*
* @param \SimpleXMLElement $xml The root node of the SVG document to parse.
*
* @return SVGImage An image object representing the parse result.
* @return SVGImage|null An image object representing the parse result.
*/
public function parseXML(\SimpleXMLElement $xml)
{
$name = $xml->getName();
if ($name !== 'svg') {
return false;
return null;
}

$namespaces = array_keys($xml->getNamespaces(true));
$width = isset($xml['width']) ? $xml['width'] : null;
$height = isset($xml['height']) ? $xml['height'] : null;
$namespaces = $xml->getNamespaces(true);

$dim = $this->getDimensions($xml);
$img = new SVGImage($dim[0], $dim[1]);
$img = new SVGImage($width, $height, $namespaces);

$doc = $img->getDocument();
$nsKeys = array_keys($namespaces);

$this->applyAttributes($doc, $xml, $namespaces);
$doc = $img->getDocument();
$this->applyAttributes($doc, $xml, $nsKeys);
$this->applyStyles($doc, $xml);

$this->addChildren($doc, $xml, $namespaces);
$this->addChildren($doc, $xml, $nsKeys);

return $img;
}

/**
* Finds out the image dimensions from the given root node.
*
* The given node MUST be the root!
* Behavior when passing any other is unspecified.
*
* @param \SimpleXMLElement $svgXml The root node of an SVG document.
*
* @return float[] The image dimensions. d[0] = width, d[1] = height.
*/
private function getDimensions(\SimpleXMLElement $svgXml)
{
$width = floatval($svgXml['width']);
$height = floatval($svgXml['height']);
// If width and height are not defined, get dimensions from viewBox
if (empty($width) && empty($height)) {
$viewBox = SVGAttrParser::parseViewBox($svgXml['viewBox']);
$width = $viewBox[2];
$height = $viewBox[3];
}

return array(
$width,
$height,
);
}

/**
* Iterates over all XML attributes and applies them to the given node.
*
Expand Down

0 comments on commit dadcabd

Please sign in to comment.