Skip to content

Commit

Permalink
Update String helper:
Browse files Browse the repository at this point in the history
- add shortTag parameter
  • Loading branch information
webeweb committed Mar 11, 2020
1 parent f272561 commit bbc70b4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Argument/Helper/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ class StringHelper {
* @param string $name The name.
* @param string $value The value.
* @param array $attributes The attributes.
* @param bool $shortTag Short tag ?
* @return string Returns the DOM node.
*/
public static function domNode($name, $value, array $attributes = []) {
public static function domNode($name, $value, array $attributes = [], $shortTag = false) {

$template = "<%name%%attributes%>%text%</%name%>";

Expand All @@ -40,6 +41,9 @@ public static function domNode($name, $value, array $attributes = []) {
}

$text = null !== $value ? trim($value, " ") : "";
if ("" === $text && true === $shortTag) {
$template = str_replace(">%text%</%name%>", "/>", $template);
}

return str_replace(["%name%", "%attributes%", "%text%"], [trim($name), $attr, $text], $template);
}
Expand Down
14 changes: 13 additions & 1 deletion tests/Argument/Helper/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use WBW\Library\Core\Tests\AbstractTestCase;

/**
* String utility test.
* String helper test.
*
* @author webeweb <https://github.com/webeweb/>
* @package WBW\Library\Core\Tests\Argument\Helper
Expand All @@ -34,6 +34,18 @@ public function testDomNode() {
$this->assertEquals($res, StringHelper::domNode("script", "\n $(document).ready(function() {});\n", $arg) . "\n");
}

/**
* Tests the domNode() method.
*
* @return void
*/
public function testDomNodeWithShortTag() {

$arg = ["type" => "text/javascript"];
$res = '<script type="text/javascript"/>';
$this->assertEquals($res, StringHelper::domNode("script", null, $arg, true));
}

/**
* Tests the parseArray() method.
*
Expand Down

0 comments on commit bbc70b4

Please sign in to comment.