Compare commits
3 Commits
issue/7-th
...
issue/8-th
| Author | SHA1 | Date | |
|---|---|---|---|
| 440053bf4f | |||
| a8af463c5c | |||
| 3f27e50c32 |
7
src/Exception/InvalidSvgException.php
Normal file
7
src/Exception/InvalidSvgException.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pcm\IconBundle\Exception;
|
||||
|
||||
class InvalidSvgException extends \Exception {};
|
||||
@@ -7,6 +7,7 @@ namespace Pcm\IconBundle\Twig\Runtime;
|
||||
use Pcm\IconBundle\Exception\ColourNotFound;
|
||||
use Pcm\IconBundle\Exception\EmptyFileException;
|
||||
use Pcm\IconBundle\Exception\IconNotFound;
|
||||
use Pcm\IconBundle\Exception\InvalidSvgException;
|
||||
use Twig\Extension\RuntimeExtensionInterface;
|
||||
|
||||
final class IconRuntime implements RuntimeExtensionInterface
|
||||
@@ -49,6 +50,10 @@ final class IconRuntime implements RuntimeExtensionInterface
|
||||
throw new EmptyFileException(\sprintf("The file %s.svg was found, but it was empty!", $options['icon']));
|
||||
}
|
||||
|
||||
if (!$this->isValidXml($svg)) {
|
||||
throw new InvalidSvgException(\sprintf("The file %s.svg was found, but it does not contain valid SVG code!", $options['icon']));
|
||||
}
|
||||
|
||||
$this->sanitiseSvg($svg);
|
||||
|
||||
$colourClasses = $this->getColourClasses($options['colour'], $options['hover']);
|
||||
@@ -211,4 +216,15 @@ final class IconRuntime implements RuntimeExtensionInterface
|
||||
|
||||
$svg = $this->removeXMLDeclaration($svgAsXml->saveXML());
|
||||
}
|
||||
|
||||
private function isValidXml(string $input): bool
|
||||
{
|
||||
try {
|
||||
new \SimpleXMLElement($input);
|
||||
} catch (\Exception) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ use Pcm\IconBundle\DependencyInjection\Configuration;
|
||||
use Pcm\IconBundle\Exception\ColourNotFound;
|
||||
use Pcm\IconBundle\Exception\EmptyFileException;
|
||||
use Pcm\IconBundle\Exception\IconNotFound;
|
||||
use Pcm\IconBundle\Exception\InvalidSvgException;
|
||||
use Pcm\IconBundle\Twig\Runtime\IconRuntime;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
@@ -57,6 +58,12 @@ class IconRuntimeTest extends TestCase
|
||||
$this->icon->renderIcon(['icon' => 'empty']);
|
||||
}
|
||||
|
||||
public function testThrowsIfContentsIsNotValidSvg(): void
|
||||
{
|
||||
$this->expectException(InvalidSvgException::class);
|
||||
$this->icon->renderIcon(['icon' => 'invalid']);
|
||||
}
|
||||
|
||||
public function testNoTitleExistsIfNotPassedIn(): void
|
||||
{
|
||||
$content = $this->icon->renderIcon(['icon' => self::ICON]);
|
||||
|
||||
1
tests/icons/invalid.svg
Normal file
1
tests/icons/invalid.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg><svg>
|
||||
|
After Width: | Height: | Size: 11 B |
Reference in New Issue
Block a user