Passed
Push — master ( fd70ec...9b67bd )
by Radu
02:34
created

Strings   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 16
eloc 34
c 9
b 0
f 0
dl 0
loc 72
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A endsWith() 0 6 3
A contains() 0 9 4
A getSlug() 0 15 2
A getContextAsString() 0 5 1
A startsWith() 0 9 4
A linkify() 0 6 1
A stripNonDigits() 0 3 1
1
<?php
2
namespace WebServCo\Framework\Utils;
3
4
final class Strings
5
{
6
    public static function contains(string $haystack, string $needle, bool $ignoreCase = true) : bool
7
    {
8
        if (false !== $ignoreCase) {
9
            $function = function_exists('mb_stripos') ? 'mb_stripos' : 'stripos';
10
        } else {
11
            $function = function_exists('mb_strpos') ? 'mb_strpos' : 'strpos';
12
        }
13
14
        return false !== $function($haystack, $needle);
15
    }
16
17
    public static function endsWith(string $haystack, string $needle) : bool
18
    {
19
        $functionSubstr = function_exists('mb_substr') ? 'mb_substr' : 'substr';
20
        $functionStrlen = function_exists('mb_strlen') ? 'mb_strlen' : 'strlen';
21
        $check = $functionSubstr($haystack, $functionStrlen($haystack) - ($functionStrlen($needle)));
22
        return $check == $needle;
23
    }
24
25
    /**
26
    * @param mixed $context
27
    * @return string
28
    */
29
    public static function getContextAsString($context) : string
30
    {
31
        ob_start();
32
        var_dump($context);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($context) looks like debug code. Are you sure you do not want to remove it?
Loading history...
33
        return ob_get_clean();
34
    }
35
36
    public static function getSlug(string $string) : string
37
    {
38
        $transliterator = \Transliterator::createFromRules(
39
            ':: Any-Latin;'
40
            . ':: NFD;'
41
            . ':: [:Nonspacing Mark:] Remove;'
42
            . ':: NFC;'
43
            . ':: [:Punctuation:] Remove;'
44
            . ':: Lower();'
45
            . '[:Separator:] > \'-\''
46
        );
47
        if (!($transliterator instanceof \Transliterator)) {
0 ignored issues
show
introduced by
$transliterator is always a sub-type of Transliterator.
Loading history...
48
            throw new \WebServCo\Framework\Exceptions\ApplicationException('Transliterator error.');
49
        }
50
        return $transliterator->transliterate($string);
51
    }
52
53
    public static function linkify($string)
54
    {
55
        return preg_replace(
56
            "~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~",
57
            "<a href=\"\\0\">\\0</a>",
58
            $string
59
        );
60
    }
61
62
    public static function startsWith(string $haystack, string $needle, bool $ignoreCase = true) : bool
63
    {
64
        if (false !== $ignoreCase) {
65
            $function = function_exists('mb_stripos') ? 'mb_stripos' : 'stripos';
66
        } else {
67
            $function = function_exists('mb_strpos') ? 'mb_strpos' : 'strpos';
68
        }
69
70
        return 0 === $function($haystack, $needle);
71
    }
72
73
    public static function stripNonDigits(string $haystack) : string
74
    {
75
        return preg_replace("/\D+/", '', $haystack);
76
    }
77
}
78