Passed
Push — master ( ff83e3...5f4a5c )
by Ehsan
03:54
created

StringUtility::jsonToArray()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 5
nc 4
nop 1
crap 5
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 $subject mixed
111
     * @param $replacements array
112
     *
113
     * @return mixed
114
     */
115 105
    public function applyReplacements($subject, $replacements)
116
    {
117 105
        if (empty($replacements) || !is_string($subject)) {
118 103
            return $subject;
119
        }
120
121 5
        foreach ($replacements as $key => $value) {
122 5
            $subject = str_replace('{'.$key.'}', $value, $subject);
123
        }
124
125 5
        return $subject;
126
    }
127
}
128