Passed
Push — master ( f69240...d8314b )
by Morris
10:11
created

Validator   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A getRequiredParameters() 0 13 4
A validateParameter() 0 11 3
B validate() 0 22 7
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Joas Schilling <[email protected]>
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 * @author Roeland Jago Douma <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
namespace OC\RichObjectStrings;
27
28
29
use OCP\RichObjectStrings\Definitions;
30
use OCP\RichObjectStrings\InvalidObjectExeption;
31
use OCP\RichObjectStrings\IValidator;
32
33
/**
34
 * Class Validator
35
 *
36
 * @package OCP\RichObjectStrings
37
 * @since 11.0.0
38
 */
39
class Validator implements IValidator {
40
41
	/** @var Definitions */
42
	protected $definitions;
43
44
	/** @var array[] */
45
	protected $requiredParameters = [];
46
47
	/**
48
	 * Constructor
49
	 *
50
	 * @param Definitions $definitions
51
	 */
52
	public function __construct(Definitions $definitions) {
53
		$this->definitions = $definitions;
54
	}
55
56
	/**
57
	 * @param string $subject
58
	 * @param array[] $parameters
59
	 * @throws InvalidObjectExeption
60
	 * @since 11.0.0
61
	 */
62
	public function validate($subject, array $parameters) {
63
		$matches = [];
64
		$result = preg_match_all('/\{([a-z0-9]+)\}/i', $subject, $matches);
65
66
		if ($result === false) {
67
			throw new InvalidObjectExeption();
68
		}
69
70
		if (!empty($matches[1])) {
71
			foreach ($matches[1] as $parameter) {
72
				if (!isset($parameters[$parameter])) {
73
					throw new InvalidObjectExeption('Parameter is undefined');
74
				}
75
			}
76
		}
77
78
		foreach ($parameters as $parameter) {
79
			if (!\is_array($parameter)) {
80
				throw new InvalidObjectExeption('Parameter is malformed');
81
			}
82
83
			$this->validateParameter($parameter);
84
		}
85
	}
86
87
	/**
88
	 * @param array $parameter
89
	 * @throws InvalidObjectExeption
90
	 */
91
	protected function validateParameter(array $parameter) {
92
		if (!isset($parameter['type'])) {
93
			throw new InvalidObjectExeption('Object type is undefined');
94
		}
95
96
		$definition = $this->definitions->getDefinition($parameter['type']);
97
		$requiredParameters = $this->getRequiredParameters($parameter['type'], $definition);
98
99
		$missingKeys = array_diff($requiredParameters, array_keys($parameter));
100
		if (!empty($missingKeys)) {
101
			throw new InvalidObjectExeption('Object is invalid');
102
		}
103
	}
104
105
	/**
106
	 * @param string $type
107
	 * @param array $definition
108
	 * @return string[]
109
	 */
110
	protected function getRequiredParameters($type, array $definition) {
111
		if (isset($this->requiredParameters[$type])) {
112
			return $this->requiredParameters[$type];
113
		}
114
115
		$this->requiredParameters[$type] = [];
116
		foreach ($definition['parameters'] as $parameter => $data) {
117
			if ($data['required']) {
118
				$this->requiredParameters[$type][] = $parameter;
119
			}
120
		}
121
122
		return $this->requiredParameters[$type];
123
	}
124
}
125