From b6dab5247964cbaef1e0e214c4f2442694eca182 Mon Sep 17 00:00:00 2001 From: Felix Althaus Date: Tue, 10 Oct 2017 22:46:50 +0200 Subject: [PATCH] Add possibility to define XML namespace for tags You can either define a namespace in constructor or via setter. You can then prefix tags like . This way you can benefit from autocompletion and XSD validation. --- README.md | 35 +++++++++++++++++++++++++++++++++-- phpunit.xml | 5 +++++ src/Inky.php | 43 +++++++++++++++++++++++++++++++++++++++---- tests/InkyTest.php | 36 ++++++++++++++++++++++++++++++++++-- 4 files changed, 111 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 9865e70..08147d1 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ $inky->releaseTheKraken('html...'); use Hampe\Inky\Inky; $inky = new Inky(); -$inky->addAlias('test', 'callout') +$inky->addAlias('test', 'callout'); $inky->releaseTheKraken('123'); //equal to "123" ``` @@ -52,7 +52,7 @@ class TestComponentFactory implements ComponentFactoryInterface { public function getName() { - return 'test' // name of the html tag. + return 'test'; // name of the html tag. } public function parse(HtmlNode $element, Inky $inkyInstance) @@ -66,6 +66,37 @@ $inky->addComponentFactory(new TestComponentFactory()); $inky->releaseTheKraken(''); ``` +### XML Namespace + +If you want to prefix all your inky tags with an XML namespace +you can set this prefix either in constructor or via setter: + +```php +use Hampe\Inky\Inky; + +// Constructor way +$inky = new Inky(12, [], 'inky'); + +// Setter way +$inky2 = new Inky(); +$inky2->setXmlNamespace('inky'); +``` +If you declare your namespace, create a XSD and make +it know to your IDE you can benefit from autocompletion and +validation features of your IDE. Your inky templates might look as +follows: + +```xml + + + + ... + + + +``` + + ## License See the [LICENSE](LICENSE) file for license info (it's the MIT license). diff --git a/phpunit.xml b/phpunit.xml index 826993b..05038a9 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -5,4 +5,9 @@ tests + + + src + + \ No newline at end of file diff --git a/src/Inky.php b/src/Inky.php index 24aa792..871524f 100644 --- a/src/Inky.php +++ b/src/Inky.php @@ -7,8 +7,9 @@ * @filename Inky.php * @package inky-parse * @author Thomas Hampe - * @copyright 2013-2016 Thomas Hampe - * @date 10.01.16 + * @author Felix Althaus + * @copyright 2013-2017 Thomas Hampe + * @date 10.10.17 */ @@ -43,14 +44,20 @@ class Inky protected $componentFactory = array(); + /** + * @var string + */ + protected $xmlNamespace; + /** * @var int */ protected $gridColumns; - public function __construct($gridColumns = 12, $componentFactories = array()) + public function __construct($gridColumns = 12, $componentFactories = array(), $xmlNamespace = null) { $this->setGridColumns($gridColumns); + $this->setXmlNamespace($xmlNamespace); $this->addComponentFactory(new RowFactory()); $this->addComponentFactory(new ContainerFactory()); $this->addComponentFactory(new ButtonFactory()); @@ -218,7 +225,7 @@ protected function parse(Dom $dom) { $parseInComplete = false; foreach($this->getAllComponentFactories() as $tag => $factory) { - $elements = $dom->getElementsByTag($tag); + $elements = $dom->getElementsByTag($this->getXmlNamespacePrefix() . $tag); foreach($elements as $element) { /** @var AbstractNode|Collection $element */ $newElement = $factory->parse($element, $this); @@ -265,4 +272,32 @@ protected function parse(Dom $dom) return $parseInComplete; } + /** + * @param string $xmlNamespace The XML namespace for all inky tags, without colon (:) + * Set to null or empty string to skip XML namespace + * @return $this Inky object for method chaining + */ + public function setXmlNamespace($xmlNamespace) + { + $xmlNamespace = trim($xmlNamespace); + $this->xmlNamespace = $xmlNamespace ?: null; + return $this; + } + + /** + * @return string + */ + public function getXmlNamespace() + { + return $this->xmlNamespace; + } + + /** + * @return string The XML namespace prefix including colon (:), empty string if no namespace is set + */ + protected function getXmlNamespacePrefix() + { + return $this->xmlNamespace ? $this->xmlNamespace . ':' : ''; + } + } \ No newline at end of file diff --git a/tests/InkyTest.php b/tests/InkyTest.php index 455bec0..2c2d7b4 100644 --- a/tests/InkyTest.php +++ b/tests/InkyTest.php @@ -7,8 +7,9 @@ * @filename Test.php * @package inky-parse * @author Thomas Hampe - * @copyright 2013-2016 Thomas Hampe - * @date 13.03.16 + * @author Felix Althaus + * @copyright 2013-2017 Thomas Hampe + * @date 10.10.17 */ use PHPHtmlParser\Dom; @@ -42,6 +43,37 @@ public function testAlias() } + public function testXmlNamespace() + { + $inky = new \Hampe\Inky\Inky(12, [], 'inky'); + $this->assertEquals('
Test
', + $inky->releaseTheKraken('Test')); + $this->assertNotEquals('
Test
', + 'Test'); + $inky->addAlias('test', 'callout'); + $this->assertEquals('
Test
', + $inky->releaseTheKraken('Test')); + $this->assertEquals('Test', $inky->releaseTheKraken('Test')); + } + + public function testXmlNamespaceViaSetter() + { + $inky = new \Hampe\Inky\Inky(); + $inky->setXmlNamespace('inky'); + $this->assertEquals('
Test
', + $inky->releaseTheKraken('Test')); + $inky->setXmlNamespace(null); + $this->assertNotEquals('
Test
', + $inky->releaseTheKraken('Test')); + } + + public function testXmlNamespaceGetterAndSetter() + { + $inky = new Hampe\Inky\Inky(); + $inky->setXmlNamespace('testns'); + $this->assertEquals('testns', $inky->getXmlNamespace()); + } + public function testStyles() { $inky = new \Hampe\Inky\Inky();