Passed
Branch php72 (a3eac9)
by Joni
03:37
created

InsignificantNonSubstringSpaceStep   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 7
dl 0
loc 19
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A apply() 0 12 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\X501\StringPrep;
6
7
/**
8
 * Implements 'Insignificant Space Handling' step of the Internationalized
9
 * String Preparation as specified by RFC 4518.
10
 *
11
 * This variant handles input strings that are non-substring assertion values.
12
 *
13
 * @see https://tools.ietf.org/html/rfc4518#section-2.6.1
14
 */
15
class InsignificantNonSubstringSpaceStep implements PrepareStep
16
{
17
    /**
18
     * @param string $string UTF-8 encoded string
19
     *
20
     * @return string
21
     */
22 42
    public function apply(string $string): string
23
    {
24
        // if value contains no non-space characters
25 42
        if (preg_match('/^\p{Zs}*$/u', $string)) {
26 7
            return '  ';
27
        }
28
        // trim leading and trailing spaces
29 35
        $string = preg_replace('/^\p{Zs}+/u', '', $string);
30 35
        $string = preg_replace('/\p{Zs}+$/u', '', $string);
31
        // convert inner space sequences to two U+0020 characters
32 35
        $string = preg_replace('/\p{Zs}+/u', '  ', $string);
33 35
        return " {$string} ";
34
    }
35
}
36