Completed
Push — master ( f3b91b...5eec2b )
by Jonathan
02:32
created

Strings::coerce()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 2
nop 1
crap 4
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015 LibreWorks contributors
19
 * @license   http://opensource.org/licenses/Apache-2.0 Apache 2.0 License
20
 */
21
namespace Caridea\Filter;
22
23
/**
24
 * A loaded six-string on my back.
25
 */
26
class Strings
27
{
28
    /**
29
     * Coerces a value into a string or throws an exception.
30
     *
31
     * @param mixed $var - The value to coerce
32
     * @return string The converted string
33
     * @throws \InvalidArgumentException if the value could not be coerced
34
     */
35 2
    public static function coerce($var): string
36
    {
37 2
        if ($var === null || is_scalar($var) || is_callable([$var, '__toString'])) {
38 1
            return (string) $var;
39
        }
40 1
        throw new \InvalidArgumentException("Could not convert to string: " . gettype($var));
41
    }
42
43
    /**
44
     * Returns a filter that coerces a value to a string.
45
     *
46
     * @return callable The created filter
47
     */
48 1
    public static function toString(): callable
49
    {
50 1
        return [__CLASS__, 'coerce'];
51
    }
52
53
    /**
54
     * Returns a new lowercasing filter.
55
     *
56
     * @return \Closure The created filter
57
     */
58 1
    public static function lowerCase(): \Closure
59
    {
60
        return function ($value) {
61 1
            return mb_convert_case(Strings::coerce($value), MB_CASE_LOWER, 'UTF-8');
62 1
        };
63
    }
64
65
    /**
66
     * Returns a new uppercasing filter.
67
     *
68
     * @return \Closure The created filter
69
     */
70 1
    public static function upperCase(): \Closure
71
    {
72
        return function ($value) {
73 1
            return mb_convert_case(Strings::coerce($value), MB_CASE_UPPER, 'UTF-8');
74 1
        };
75
    }
76
77
    /**
78
     * Returns a new uppercasing filter.
79
     *
80
     * @return \Closure The created filter
81
     */
82 1
    public static function titleCase(): \Closure
83
    {
84
        return function ($value) {
85 1
            return mb_convert_case(Strings::coerce($value), MB_CASE_TITLE, 'UTF-8');
86 1
        };
87
    }
88
89
    /**
90
     * Returns a new whitespace trimming filter.
91
     *
92
     * @return \Closure The created filter
93
     */
94 1
    public static function trim(): \Closure
95
    {
96
        return function ($value) {
97 1
            return trim(Strings::coerce($value));
98 1
        };
99
    }
100
101
    /**
102
     * Returns a new substring filter.
103
     *
104
     * @param int $length The maximum string length
105
     * @return \Closure The created filter
106
     */
107 1
    public static function cut(int $length): \Closure
108
    {
109
        return function ($value) use ($length) {
110 1
            return substr(Strings::coerce($value), 0, $length);
111 1
        };
112
    }
113
114
    /**
115
     * Creates a new search and replace filter.
116
     *
117
     * @param string $search The value to find
118
     * @param string $replacement The value to use as a replacement
119
     * @return \Closure The created filter
120
     */
121 1
    public static function replace(string $search, string $replacement): \Closure
122
    {
123
        return function ($value) use ($search, $replacement) {
124 1
            return str_replace($search, $replacement, Strings::coerce($value));
125 1
        };
126
    }
127
128
    /**
129
     * Creates a new regular expression filter.
130
     *
131
     * @param string $pattern The search pattern
132
     * @param string $replacement The value to use as a replacement
133
     * @return \Closure The created filter
134
     */
135
    public static function regex(string $pattern, string $replacement): \Closure
136
    {
137 1
        return function ($value) use ($pattern, $replacement) {
138 1
            return preg_replace($pattern, $replacement, Strings::coerce($value));
139 1
        };
140
    }
141
142
    /**
143
     * Creates a new filter that removes non-alphanumeric characters.
144
     *
145
     * @return \Closure The created filter
146
     */
147 1
    public static function alnum(): \Closure
148
    {
149 1
        return self::regex('/[^\p{L}\p{Nd}]/u', '');
150
    }
151
152
    /**
153
     * Creates a new filter that removes non-alphabetic characters.
154
     *
155
     * @return \Closure The created filter
156
     */
157 1
    public static function alpha(): \Closure
158
    {
159 1
        return self::regex('/[^\p{L}]/u', '');
160
    }
161
162
    /**
163
     * Creates a new filter that removes non-digit characters.
164
     *
165
     * @return \Closure The created filter
166
     */
167 1
    public static function numeric(): \Closure
168
    {
169 1
        return self::regex('/[^\p{N}]/u', '');
170
    }
171
172
    /**
173
     * Creates a new filter that turns any newlines into UNIX-style newlines.
174
     *
175
     * @return \Closure The created filter
176
     */
177 1
    public static function unixNewlines(): \Closure
178
    {
179 1
        return self::regex("/\R/", "\n");
180
    }
181
182
    /**
183
     * Creates a new filter that turns multiple newlines into two.
184
     *
185
     * Meant to be run after `unixNewlines`.
186
     *
187
     * @return \Closure The created filter
188
     */
189 1
    public static function compactNewlines(): \Closure
190
    {
191 1
        return self::regex("/\n\n+/", "\n\n");
192
    }
193
}
194