Skip to content

Commit

Permalink
feat: Add option to put lines in random order (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
irineujunior authored Oct 10, 2023
1 parent eac6d06 commit 2dcc068
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/demo/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ function gtag() {
<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">
Expand Down
8 changes: 8 additions & 0 deletions src/models/RendererModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -74,6 +77,7 @@ class RendererModel
"pause" => "0",
"repeat" => "true",
"separator" => ";",
"random" => "false",
];

/**
Expand All @@ -86,6 +90,7 @@ public function __construct($template, $params)
{
$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");
Expand Down Expand Up @@ -118,6 +123,9 @@ private function checkLines($lines)
$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);
}
Expand Down
29 changes: 29 additions & 0 deletions tests/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,33 @@ public function testSeparator(): void
$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);
}
}
22 changes: 22 additions & 0 deletions tests/RendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,26 @@ public function testSeparator(): void
$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);
}
}
}

3 comments on commit 2dcc068

@sharathsharath143
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sharathsharath143
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sharathsharath143
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pllzz open dolowda

Please sign in to comment.