Passed
Push — master ( ff15f0...346eaf )
by Radu
01:46
created

Strings::stripNonDigits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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