-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathS.php
More file actions
57 lines (50 loc) · 1.7 KB
/
Copy pathS.php
File metadata and controls
57 lines (50 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php declare(strict_types=1);
namespace DCSG\StringyTemplate;
use StringTemplate\SprintfEngine;
use Stringy\StaticStringy;
/**
* @author Daniel Gomes <danielcesargomes@gmail.com>
*/
class S extends StaticStringy
{
/**
* @var SprintfEngine
*/
private static $engine;
/**
* Returns a Strings that renders the template placeholders, delimited by {}.
* The engined used is the String Template Sprintf Engine.
*
* e.g.: 'Hello my name is {name}', where ['name' => 'Daniel'], will render 'Hello my name is Daniel'.
*
* Notes: All placeholders must have a value otherwise they will be render.
*
* @param string $template The template with placeholders to be replaced
* @param array $values The 'placeholder' => 'value' map
* @return string The String with the placeholders replaced with their values.
*/
public static function render(string $template, array $values): string
{
return self::getEngine()->render($template, $values);
}
/**
* Returns a String that concatenates the array contented with the defined "glue".
*
* e.g.: ['one', 'two'] with a glue being ', ' it will render 'one, two'.
*
* @param array $pieces The array to be concatenated
* @param string $glue The glue to concatenate the array content
* @return string The concatenated String
*/
public static function join(array $pieces, $glue = ' '): string
{
return implode($glue, $pieces);
}
private static function getEngine(): SprintfEngine
{
if (self::$engine instanceof SprintfEngine) {
return self::$engine;
}
return self::$engine = new SprintfEngine();
}
}