Repository: DenverCoder1/readme-typing-svg (php)

Issue:
Am i the first to suggest random order ?
it would be nice if there was a boolean for whether the list show be displayed in order, or in random order.

thanks a lot! 

Pull request:
feat: Add option to put lines in random order
## Summary

Add random option

Fixes #179

## Type of change

- [X] New feature

## How Has This Been Tested?

- [X] Ran tests with `composer test`
- [X] Added test cases to test new features


Patch:
diff --git a/src/demo/index.php b/src/demo/index.php
index c4db521..5c9184c 100644
--- a/src/demo/index.php
+++ b/src/demo/index.php
@@ -106,6 +106,12 @@
                     <option>false</option>
                 </select>
 
+                <label for="random">Random</label>
+                <select class="param" id="random" name="random" alt="Random">
+                    <option>false</option>
+                    <option>true</option>
+                </select>
+
                 <label for="dimensions" title="Width ✕ Height">Width ✕ Height</label>
                 <span id="dimensions">
                     <input class="param inline" type="number" id="width" name="width" alt="Width (px)" placeholder="435" value="435">
diff --git a/src/models/RendererModel.php b/src/models/RendererModel.php
index 3ca813a..64e08d0 100644
--- a/src/models/RendererModel.php
+++ b/src/models/RendererModel.php
@@ -52,6 +52,9 @@ class RendererModel
     /** @var string $separator Line separator */
     public $separator;
 
+    /** @var bool $random True = Sort lines in random order */
+    public $random;
+
     /** @var string $fontCSS CSS required for displaying the selected font */
     public $fontCSS;
 
@@ -74,6 +77,7 @@ class RendererModel
         "pause" => "0",
         "repeat" => "true",
         "separator" => ";",
+        "random" => "false",
     ];
 
     /**
@@ -86,6 +90,7 @@ class RendererModel
     {
         $this->template = $template;
         $this->separator = $params["separator"] ?? $this->DEFAULTS["separator"];
+        $this->random = $this->checkBoolean($params["random"] ?? $this->DEFAULTS["random"]);
         $this->lines = $this->checkLines($params["lines"] ?? "");
         $this->font = $this->checkFont($params["font"] ?? $this->DEFAULTS["font"]);
         $this->weight = $this->checkNumberPositive($params["weight"] ?? $this->DEFAULTS["weight"], "Font weight");
@@ -118,6 +123,9 @@ class RendererModel
             $lines = rtrim($lines, $this->separator);
         }
         $exploded = explode($this->separator, $lines);
+        if ($this->random) {
+            shuffle($exploded);
+        }
         // escape special characters to prevent code injection
         return array_map("htmlspecialchars", $exploded);
     }


Test patch:
diff --git a/tests/OptionsTest.php b/tests/OptionsTest.php
index 697b8fe..48ff687 100644
--- a/tests/OptionsTest.php
+++ b/tests/OptionsTest.php
@@ -338,4 +338,33 @@ final class OptionsTest extends TestCase
         $this->assertEquals(";;", $model->separator);
         $this->assertEquals(["text", "tex;t2"], $model->lines);
     }
+
+    /**
+     * Test random order
+     */
+    public function testRandom(): void
+    {
+        // not set
+        $params = [
+            "lines" => "text",
+        ];
+        $model = new RendererModel("src/templates/main.php", $params);
+        $this->assertEquals(false, $model->random);
+
+        // true
+        $params = [
+            "lines" => "text",
+            "random" => "true",
+        ];
+        $model = new RendererModel("src/templates/main.php", $params);
+        $this->assertEquals(true, $model->random);
+
+        // other / false
+        $params = [
+            "lines" => "text",
+            "random" => "other",
+        ];
+        $model = new RendererModel("src/templates/main.php", $params);
+        $this->assertEquals(false, $model->random);
+    }
 }
diff --git a/tests/RendererTest.php b/tests/RendererTest.php
index 7d122db..6093aaf 100644
--- a/tests/RendererTest.php
+++ b/tests/RendererTest.php
@@ -304,4 +304,26 @@ final class RendererTest extends TestCase
         $actualSVG = $controller->render();
         $this->compareNoCommentsOrWhitespace($expectedSVG, $actualSVG);
     }
+
+    /**
+     * Test random
+     */
+    public function testRandom(): void
+    {
+        $lines = [
+            "Full-stack web and app developer",
+            "Self-taught UI/UX Designer",
+            "10+ years of coding experience",
+            "Always learning new things",
+        ];
+        $params = [
+            "lines" => implode(";", $lines),
+            "random" => "true",
+        ];
+        $controller = new RendererController($params);
+        $actualSVG = preg_replace("/\s+/", " ", $controller->render());
+        foreach ($lines as $line) {
+            $this->assertStringContainsString("> $line </textPath>", $actualSVG);
+        }
+    }
 }