Browse Source

added HtmlHelper::encode()

Fabian Peter Hammerle 7 years ago
parent
commit
69dfec6b18
2 changed files with 39 additions and 0 deletions
  1. 11 0
      HtmlHelper.php
  2. 28 0
      tests/HtmlHelperTest.php

+ 11 - 0
HtmlHelper.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace fphammerle\helpers;
+
+class HtmlHelper
+{
+    public static function encode($string)
+    {
+        return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE);
+    }
+}

+ 28 - 0
tests/HtmlHelperTest.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace fphammerle\helpers\tests;
+
+use fphammerle\helpers\HtmlHelper;
+
+class HtmlHelperTest extends \PHPUnit_Framework_TestCase
+{
+    public function encodeProvider()
+    {
+        return [
+            ['abc', 'abc'],
+            ['可以', '可以'],
+            ['⚕', '⚕'],
+            ['<abc>', '&lt;abc&gt;'],
+            ['alert(":-)");', 'alert(&quot;:-)&quot;);'],
+            ['alert(\':-)\');', 'alert(&#039;:-)&#039;);'],
+            ];
+    }
+
+    /**
+     * @dataProvider encodeProvider
+     */
+    public function testEncode($string, $expected)
+    {
+        $this->assertSame($expected, HtmlHelper::encode($string));
+    }
+}