Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
ImplementsStringHelpers
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
1 / 1
 makeTitle
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 normalizeNewlines
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 stripNewlines
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 trimSlashes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 markdown
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Foundation\Concerns;
6
7use Hyde\Framework\Services\MarkdownService;
8use Hyde\Markdown\Models\Markdown;
9use Illuminate\Support\HtmlString;
10use Illuminate\Support\Str;
11
12use function str_ireplace;
13use function str_replace;
14use function strtoupper;
15use function trim;
16use function ucfirst;
17
18/**
19 * @internal Single-use trait for the HydeKernel class.
20 *
21 * @see \Hyde\Foundation\HydeKernel
22 */
23trait 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 normalizeNewlines(string $string): string
42    {
43        return str_replace("\r\n", "\n", $string);
44    }
45
46    public static function stripNewlines(string $string): string
47    {
48        return str_replace(["\r\n", "\n"], '', $string);
49    }
50
51    public static function trimSlashes(string $string): string
52    {
53        return trim($string, '/\\');
54    }
55
56    public static function markdown(string $text, bool $normalizeIndentation = false): HtmlString
57    {
58        if ($normalizeIndentation) {
59            $text = MarkdownService::normalizeIndentationLevel($text);
60        }
61
62        return new HtmlString(Markdown::render($text));
63    }
64}