Passed
Push — master ( 18f8d8...938dee )
by Ehsan
03:38
created

StringUtility::findInString()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 4
nop 3
crap 3
1
<?php
2
3
namespace Botonomous\utility;
4
5
/**
6
 * Class StringUtility.
7
 */
8
class StringUtility extends AbstractUtility
9
{
10
    /**
11
     * @param string $json
12
     *
13
     * @throws \Exception
14
     *
15
     * @return array|mixed
16
     */
17 21
    public function jsonToArray($json)
18
    {
19 21
        $array = empty($json) ? [] : json_decode($json, true);
20
21 21
        if ($array === null || !is_array($array) || json_last_error() !== 0) {
22 1
            throw new \Exception('Invalid JSON content');
23
        }
24
25 21
        return $array;
26
    }
27
28
    /**
29
     * @param $toRemove
30
     * @param $subject
31
     *
32
     * @return string
33
     */
34 1
    public function removeStringFromString($toRemove, $subject)
35
    {
36
        // pattern: !\s+! is used to replace multiple spaces with single space
37 1
        return trim(preg_replace('!\s+!', ' ', str_replace($toRemove, '', $subject)));
38
    }
39
40
    /**
41
     * @param $toFind
42
     * @param $subject
43
     * @param bool $wordBoundary If true $toFind is searched with word boundaries
44
     *
45
     * @return bool
46
     */
47 5
    public function findInString($toFind, $subject, $wordBoundary = true)
48
    {
49 5
        $pattern = $wordBoundary === true ? "/\b{$toFind}\b/" : "/{$toFind}/";
50
51 5
        return preg_match($pattern, $subject) ? true : false;
52
    }
53
54
    /**
55
     * Convert snake case to camel case e.g. admin_user becomes AdminUser.
56
     *
57
     * @param $string
58
     *
59
     * @return string
60
     */
61 19
    public function snakeCaseToCamelCase($string)
62
    {
63 19
        return str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
64
    }
65
66
    /**
67
     * Check subject to see whether $string1 is followed by $string2.
68
     *
69
     * @param $subject
70
     * @param $string1
71
     * @param $string2
72
     * @param array $exceptions
73
     *
74
     * @return bool
75
     */
76 1
    public function isString1FollowedByString2($subject, $string1, $string2, array $exceptions = [])
77
    {
78 1
        $exceptionsString = '';
79 1
        if (!empty($exceptions)) {
80 1
            $exceptions = implode('|', $exceptions);
81 1
            $exceptionsString = "(?<!{$exceptions})";
82
        }
83
84 1
        $pattern = '/'.$string1.'(?:\s+\w+'.$exceptionsString.'){0,2}\s+'.$string2.'\b/';
85
86 1
        return preg_match($pattern, $subject) ? true : false;
87
    }
88
89
    /**
90
     * @param $haystack
91
     * @param string $needle
92
     *
93
     * @return bool
94
     */
95 12
    public function endsWith($haystack, $needle)
96
    {
97 12
        $length = strlen($needle);
98
99 12
        if ($length === 0) {
100 1
            return true;
101
        }
102
103 12
        return substr($haystack, -$length) === $needle;
104
    }
105
106
    /**
107
     * Apply replacements in a string
108
     * Replacement key in the string should be like {replacementKey}
109
     *
110
     * @param $string string
111
     * @param $replacements array
112
     * @return string
113
     */
114 105
    public function applyReplacements($string, $replacements)
115
    {
116 105
        if (empty($replacements)) {
117 103
            return $string;
118
        }
119
120 5
        foreach ($replacements as $key => $value) {
121 5
            $string = str_replace('{'.$key.'}', $value, $string);
122
        }
123
124 5
        return $string;
125
    }
126
}
127