Completed
Push — master ( 87ab33...75849f )
by Ehsan
03:23
created

StringUtility::findPositionInString()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 4
nop 3
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
     * @param      $toFind
56
     * @param      $subject
57
     * @param bool $wordBoundary
58
     *
59
     * @return mixed
60
     */
61 3
    public function findPositionInString($toFind, $subject, $wordBoundary = true)
62
    {
63 3
        $pattern = $wordBoundary === true ? "/\b{$toFind}\b/" : "/{$toFind}/";
64 3
        $result = preg_match_all($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
65
66 3
        $positions = [];
67 3
        if ($result && !empty($matches[0])) {
68 3
            foreach ($matches[0] as $match) {
69 3
                $positions[] = $match[1];
70
            }
71
        }
72
73 3
        return $positions;
74
    }
75
76
    /**
77
     * Convert snake case to camel case e.g. admin_user becomes AdminUser.
78
     *
79
     * @param $string
80
     *
81
     * @return string
82
     */
83 19
    public function snakeCaseToCamelCase($string)
84
    {
85 19
        return str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
86
    }
87
88
    /**
89
     * Check subject to see whether $string1 is followed by $string2.
90
     *
91
     * @param $subject
92
     * @param $string1
93
     * @param $string2
94
     * @param array $exceptions
95
     *
96
     * @return bool
97
     */
98 1
    public function isString1FollowedByString2($subject, $string1, $string2, array $exceptions = [])
99
    {
100 1
        $exceptionsString = '';
101 1
        if (!empty($exceptions)) {
102 1
            $exceptions = implode('|', $exceptions);
103 1
            $exceptionsString = "(?<!{$exceptions})";
104
        }
105
106 1
        $pattern = '/'.$string1.'(?:\s+\w+'.$exceptionsString.'){0,2}\s+'.$string2.'\b/';
107
108 1
        return preg_match($pattern, $subject) ? true : false;
109
    }
110
111
    /**
112
     * @param $haystack
113
     * @param string $needle
114
     *
115
     * @return bool
116
     */
117 12
    public function endsWith($haystack, $needle)
118
    {
119 12
        $length = strlen($needle);
120
121 12
        if ($length === 0) {
122 1
            return true;
123
        }
124
125 12
        return substr($haystack, -$length) === $needle;
126
    }
127
128
    /**
129
     * Apply replacements in a string
130
     * Replacement key in the string should be like {replacementKey}.
131
     *
132
     * @param $subject mixed
133
     * @param $replacements array
134
     *
135
     * @return mixed
136
     */
137 106
    public function applyReplacements($subject, $replacements)
138
    {
139 106
        if (empty($replacements) || !is_string($subject)) {
140 104
            return $subject;
141
        }
142
143 5
        foreach ($replacements as $key => $value) {
144 5
            $subject = str_replace('{'.$key.'}', $value, $subject);
145
        }
146
147 5
        return $subject;
148
    }
149
}
150