From 6a4fb3e29135ec16bab8d8099e8df8a9595efd3c Mon Sep 17 00:00:00 2001 From: Benjamin Morel Date: Fri, 6 Oct 2017 16:31:28 +0200 Subject: [PATCH] Allow attributes in Tag constructor The functionality is redundant, but this is for convenience. --- src/Tag.php | 9 +++++++-- tests/TagTest.php | 6 +++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Tag.php b/src/Tag.php index 6aa8873..f85a61a 100644 --- a/src/Tag.php +++ b/src/Tag.php @@ -65,14 +65,19 @@ class Tag /** * Tag constructor. * - * @param string $name The tag name. + * @param string $name The tag name. + * @param array $attributes The tag attributes, as an associative array of names to values. Optional. */ - public function __construct(string $name) + public function __construct(string $name, array $attributes = []) { $name = strtolower($name); $this->name = $name; $this->isVoid = in_array($name, self::VOID_ELEMENTS, true); + + if ($attributes) { + $this->setAttributes($attributes); + } } /** diff --git a/tests/TagTest.php b/tests/TagTest.php index dd59429..7e2f937 100644 --- a/tests/TagTest.php +++ b/tests/TagTest.php @@ -34,14 +34,14 @@ public function testTag() public function testVoidTag() { - $tag = new Tag('IMG'); - $this->assertSame('', $tag->render()); + $tag = new Tag('IMG', ['SRC' => 'TEST.PNG']); + $this->assertSame('', $tag->render()); $tag->setAttributes([ 'ID' => 123, 'Src' => 'IMAGE.PNG', 'Data-Chars' => '"\'<>' ]); - $this->assertSame('', $tag->render()); + $this->assertSame('', $tag->render()); } }