Completed
Push — master ( 2a57e9...d915e7 )
by Marius
10s
created

TrimmingStringNormalizer::normalize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace ValueParsers\Normalizers;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Most simple implementation of a StringNormalizer that does nothing but trimming whitespace, by
9
 * using the definition of "whitespace" in Unicode-enabled Perl regular expressions.
10
 *
11
 * @since 0.3.2
12
 *
13
 * @license GPL-2.0-or-later
14
 * @author Thiemo Kreuz
15
 */
16
class TrimmingStringNormalizer implements StringNormalizer {
17
18
	/**
19
	 * @param string $value
20
	 *
21
	 * @throws InvalidArgumentException if $value is not a string
22
	 * @return string the trimmed string
23
	 */
24 9
	public function normalize( $value ) {
25 9
		if ( !is_string( $value ) ) {
26 4
			throw new InvalidArgumentException( '$value must be a string' );
27
		}
28
29 5
		return preg_replace( '/^\s+|\s+$/u', '', $value );
30
	}
31
32
}
33