Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
17 / 17 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
ImplementsStringHelpers | |
100.00% |
17 / 17 |
|
100.00% |
6 / 6 |
8 | |
100.00% |
1 / 1 |
makeTitle | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
makeSlug | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
normalizeNewlines | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
stripNewlines | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
trimSlashes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
markdown | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Hyde\Foundation\Concerns; |
6 | |
7 | use Hyde\Framework\Services\MarkdownService; |
8 | use Hyde\Markdown\Models\Markdown; |
9 | use Illuminate\Support\HtmlString; |
10 | use Illuminate\Support\Str; |
11 | |
12 | use function str_ireplace; |
13 | use function str_replace; |
14 | use function strtoupper; |
15 | use function trim; |
16 | use function ucfirst; |
17 | |
18 | /** |
19 | * @internal Single-use trait for the HydeKernel class. |
20 | * |
21 | * @see \Hyde\Foundation\HydeKernel |
22 | */ |
23 | trait ImplementsStringHelpers |
24 | { |
25 | public static function makeTitle(string $value): string |
26 | { |
27 | // Don't modify all-uppercase input |
28 | if ($value === strtoupper($value)) { |
29 | return $value; |
30 | } |
31 | |
32 | $alwaysLowercase = ['a', 'an', 'the', 'in', 'on', 'by', 'with', 'of', 'and', 'or', 'but']; |
33 | |
34 | return ucfirst(str_ireplace( |
35 | $alwaysLowercase, |
36 | $alwaysLowercase, |
37 | Str::headline($value) |
38 | )); |
39 | } |
40 | |
41 | public static function makeSlug(string $value): string |
42 | { |
43 | // Expand camelCase and PascalCase to separate words |
44 | $value = preg_replace('/([a-z])([A-Z])/', '$1 $2', $value); |
45 | |
46 | // Transliterate international characters to ASCII |
47 | $value = Str::transliterate($value); |
48 | |
49 | // Todo: In v2.0 we will use the following dictionary: ['@' => 'at', '&' => 'and'] |
50 | |
51 | return Str::slug($value); |
52 | } |
53 | |
54 | public static function normalizeNewlines(string $string): string |
55 | { |
56 | return str_replace("\r\n", "\n", $string); |
57 | } |
58 | |
59 | public static function stripNewlines(string $string): string |
60 | { |
61 | return str_replace(["\r\n", "\n"], '', $string); |
62 | } |
63 | |
64 | public static function trimSlashes(string $string): string |
65 | { |
66 | return trim($string, '/\\'); |
67 | } |
68 | |
69 | public static function markdown(string $text, bool $normalizeIndentation = false): HtmlString |
70 | { |
71 | if ($normalizeIndentation) { |
72 | $text = MarkdownService::normalizeIndentationLevel($text); |
73 | } |
74 | |
75 | return new HtmlString(Markdown::render($text)); |
76 | } |
77 | } |