Passed
Push — master ( ce23e4...464bb8 )
by Caen
04:39 queued 13s
created

ImplementsStringHelpers::makeSlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
rs 10
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(
0 ignored issues
show
Bug introduced by
It seems like str_ireplace($alwaysLowe...\Str::headline($value)) can also be of type array; however, parameter $string of ucfirst() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        return ucfirst(/** @scrutinizer ignore-type */ str_ireplace(
Loading history...
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
}
78