Passed
Push — develop ( 169afe...f2bd80 )
by Jens
02:39
created

Tokenizer::tokenize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: jensk
4
 * Date: 21-2-2017
5
 * Time: 16:23
6
 */
7
8
namespace library\search;
9
10
/**
11
 * Class Tokenizer
12
 * @package library\search
13
 */
14
class Tokenizer
15
{
16
	protected $inputString;
17
	protected $tokenVector = array();
18
19
	/**
20
	 * Tokenizer constructor.
21
	 *
22
	 * @param string $string Should preferably be parsed wit \library\search\CharacterFilter
23
	 * @see \library\search\CharacterFilter
24
	 */
25
	public function __construct($string)
26
	{
27
		$this->inputString = $string;
28
		$this->tokenize();
29
	}
30
31
	protected function tokenize()
32
	{
33
		$tokens = explode(' ', $this->inputString);
34
		foreach ($tokens as $token) {
35
			$this->addTokenToVector($token);
36
		}
37
	}
38
39
	protected function addTokenToVector($token)
40
	{
41
		if (!empty($token)) {
42
			if (isset($this->tokenVector[$token])) {
43
				$this->tokenVector[$token] += 1;
44
			} else {
45
				$this->tokenVector[$token] = 1;
46
			}
47
		}
48
	}
49
50
	/**
51
	 * @return array
52
	 */
53
	public function getTokenVector()
54
	{
55
		return $this->tokenVector;
56
	}
57
58
59
}