Passed
Push — master ( d7fd6a...9124ff )
by Robbie
11:58
created

SynonymValidator   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 19
c 0
b 0
f 0
dl 0
loc 143
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A validateValue() 0 21 4
A validateLine() 0 13 3
A validatePart() 0 15 4
A __construct() 0 4 1
A validateField() 0 7 2
A validateNoSpaces() 0 10 2
A php() 0 7 3
1
<?php
2
3
class SynonymValidator extends Validator {
4
	/**
5
	 * @var array
6
	 */
7
	protected $fieldNames;
8
9
	/**
10
	 * @inheritdoc
11
	 *
12
	 * @param array $fieldNames
13
	 */
14
	public function __construct(array $fieldNames) {
15
		$this->fieldNames = $fieldNames;
16
17
		parent::__construct();
18
	}
19
20
	/**
21
	 * @inheritdoc
22
	 *
23
	 * @param array $data
24
	 *
25
	 * @return mixed
26
	 */
27
	public function php($data) {
28
		foreach($this->fieldNames as $fieldName) {
29
			if(empty($data[$fieldName])) {
30
				return;
31
			}
32
33
			$this->validateField($fieldName, $data[$fieldName]);
34
		}
35
	}
36
37
	/**
38
	 * Validate field values, raising errors if the values are invalid.
39
	 *
40
	 * @param string $fieldName
41
	 * @param mixed $value
42
	 */
43
	protected function validateField($fieldName, $value) {
44
		if(!$this->validateValue($value)) {
45
			$this->validationError(
46
				$fieldName,
47
				_t(
48
					'FullTextSearch.SynonymValidator.InvalidValue',
49
					'Synonyms cannot contain words separated by spaces'
50
				)
51
			);
52
		}
53
	}
54
55
	/**
56
	 * Check field values to see that they doesn't contain spaces between words.
57
	 *
58
	 * @param mixed $value
59
	 *
60
	 * @return bool
61
	 */
62
	protected function validateValue($value) {
63
		// strip empty lines
64
		$lines = array_filter(
65
			explode("\n", $value)
66
		);
67
68
		// strip comments (lines beginning with "#")
69
		$lines = array_filter($lines, function ($line) {
70
			$line = trim($line);
71
72
			return !empty($line) && $line[0] !== '#';
73
		});
74
75
		// validate each line
76
		foreach($lines as $line) {
77
			if(!$this->validateLine($line)) {
78
				return false;
79
			}
80
		}
81
82
		return true;
83
	}
84
85
	/**
86
	 * Check each line to see that it doesn't contain spaces between words.
87
	 *
88
	 * @param string $line
89
	 *
90
	 * @return bool
91
	 */
92
	protected function validateLine($line) {
93
		$line = trim($line);
94
95
		$parts = explode(',', $line);
96
		$parts = array_filter($parts);
97
98
		foreach($parts as $part) {
99
			if(!$this->validatePart($part)) {
100
				return false;
101
			}
102
		}
103
104
		return true;
105
	}
106
107
	/**
108
	 * Check each part of the line doesn't contain spaces between words.
109
	 *
110
	 * @param string $part
111
	 *
112
	 * @return bool
113
	 */
114
	protected function validatePart($part) {
115
		if(strpos($part, '=>') !== false) {
116
			$subs = explode('=>', $part);
117
			$subs = array_filter($subs);
118
119
			foreach($subs as $sub) {
120
				if (!$this->validateNoSpaces($sub)) {
121
					return false;
122
				}
123
			}
124
125
			return true;
126
		}
127
128
		return $this->validateNoSpaces($part);
129
	}
130
131
	/**
132
	 * @param string $value
133
	 *
134
	 * @return bool
135
	 */
136
	protected function validateNoSpaces($value) {
137
		// allow spaces at the beginning and end of the value
138
		$value = trim($value);
139
140
		// does the value contain 1 or more whitespace characters?
141
		if(preg_match('/\s+/', $value)) {
142
			return false;
143
		}
144
145
		return true;
146
	}
147
}
148