Passed
Push — master ( 9eeeb5...ff15f0 )
by Radu
02:55
created

Strings::getSlug()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 15
rs 9.9
cc 2
nc 2
nop 1
1
<?php
2
namespace WebServCo\Framework\Utils;
3
4
final class Strings
5
{
6
    public static function contains($haystack, $needle, $ignoreCase = true)
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($haystack, $needle)
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
    public static function getSlug($string)
26
    {
27
        $transliterator = \Transliterator::createFromRules(
28
            ':: Any-Latin;'
29
            . ':: NFD;'
30
            . ':: [:Nonspacing Mark:] Remove;'
31
            . ':: NFC;'
32
            . ':: [:Punctuation:] Remove;'
33
            . ':: Lower();'
34
            . '[:Separator:] > \'-\''
35
        );
36
        if (!($transliterator instanceof \Transliterator)) {
0 ignored issues
show
introduced by
$transliterator is always a sub-type of Transliterator.
Loading history...
37
            throw new \WebServCo\Framework\Exceptions\ApplicationException('Transliterator error.');
38
        }
39
        return $transliterator->transliterate($string);
40
    }
41
42
    public static function startsWith($haystack, $needle, $ignoreCase = true)
43
    {
44
        if (false !== $ignoreCase) {
45
            $function = function_exists('mb_stripos') ? 'mb_stripos' : 'stripos';
46
        } else {
47
            $function = function_exists('mb_strpos') ? 'mb_strpos' : 'strpos';
48
        }
49
50
        return 0 === $function($haystack, $needle);
51
    }
52
53
    public static function stripNonDigits($haystack)
54
    {
55
        return preg_replace("/\D+/", '', $haystack);
56
    }
57
}
58