Passed
Push — master ( fadf79...5a8e38 )
by Sebastian
03:53
created

Lexer::tokenize()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Linna Filter
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types = 1);
11
12
namespace Linna\Filter;
13
14
/**
15
 * Lexer.
16
 */
17
class Lexer
18
{
19
    /**
20
     * Split period in tokens.
21
     *
22
     * @param string $period
23
     * @return array
24
     */
25 50
    public static function tokenize(string $period) : array
26
    {
27 50
        $chars = str_split(rtrim(ltrim($period)));
28 50
        $words = $temp = [];
29
        
30 50
        foreach ($chars as $char) {
31 50
            if (in_array(ord($char), [32, 44, 58, 59])) {
32 50
                $words[] = implode('', $temp);
33 50
                $temp = [];
34 50
                continue;
35
            }
36
37 50
            $temp[] = $char;
38
        }
39
40 50
        $words[] = implode('', $temp);
41
42 50
        return array_values(array_filter($words, 'trim'));
43
    }
44
}
45