Completed
Pull Request — master (#35)
by no
04:18 queued 02:47
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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
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+
14
 * @author Thiemo Mättig
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