Skip to content

Commit

Permalink
Allow attributes in Tag constructor
Browse files Browse the repository at this point in the history
The functionality is redundant, but this is for convenience.
  • Loading branch information
BenMorel committed Oct 6, 2017
1 parent d4e68f9 commit 6a4fb3e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/TagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public function testTag()

public function testVoidTag()
{
$tag = new Tag('IMG');
$this->assertSame('<img>', $tag->render());
$tag = new Tag('IMG', ['SRC' => 'TEST.PNG']);
$this->assertSame('<img src="TEST.PNG">', $tag->render());

$tag->setAttributes([
'ID' => 123,
'Src' => 'IMAGE.PNG',
'Data-Chars' => '"\'<>'
]);
$this->assertSame('<img id="123" src="IMAGE.PNG" data-chars="&quot;\'&lt;&gt;">', $tag->render());
$this->assertSame('<img src="IMAGE.PNG" id="123" data-chars="&quot;\'&lt;&gt;">', $tag->render());
}
}

0 comments on commit 6a4fb3e

Please sign in to comment.