summaryrefslogtreecommitdiff
path: root/www/wiki/vendor/param-processor/param-processor/src/Processor.php
blob: 73a8d67a4d66e9beb96de330a85be14444ad2747 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<?php

namespace ParamProcessor;

use ParamProcessor\PackagePrivate\Param;

/**
 * Class for parameter validation of a single parser hook or other parametrized construct.
 *
 * @since 0.1
 *
 * @licence GNU GPL v2+
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 * @author Daniel Werner
 */
class Processor {

	/**
	 * Flag for unnamed default parameters used in Processor::setFunctionParams() to determine that
	 * a parameter should not have a named fallback.
	 *
	 * @since 0.4.13
	 */
	const PARAM_UNNAMED = 1;

	/**
	 * @var Param[]
	 */
	private $params;

	/**
	 * Associative array containing parameter names (keys) and their user-provided data (values).
	 * This list is needed because additional parameter definitions can be added to the $parameters
	 * field during validation, so we can't determine in advance if a parameter is unknown.
	 * @var string[]
	 */
	private $rawParameters = [];

	/**
	 * Array containing the names of the parameters to handle, ordered by priority.
	 * @var string[]
	 */
	private $paramsToHandle = [];

	/**
	 * @var ParamDefinition[]
	 */
	private $paramDefinitions = [];

	/**
	 * @var ProcessingError[]
	 */
	private $errors = [];

	private $options;

	public function __construct( Options $options ) {
		$this->options = $options;
	}

	/**
	 * Constructs and returns a Validator object based on the default options.
	 */
	public static function newDefault(): self {
		return new Processor( new Options() );
	}

	/**
	 * Constructs and returns a Validator object based on the provided options.
	 */
	public static function newFromOptions( Options $options ): self {
		return new Processor( $options );
	}

	/**
	 * Returns the options used by this Validator object.
	 */
	public function getOptions(): Options {
		return $this->options;
	}

	/**
	 * Determines the names and values of all parameters. Also takes care of default parameters.
	 * After that the resulting parameter list is passed to Processor::setParameters
	 *
	 * @since 0.4
	 *
	 * @param string[] $rawParams
	 * @param ParamDefinition[]|array[] $parameterDefinitions DEPRECATED! Use @see setParameterDefinitions instead
	 * @param array $defaultParams array of strings or array of arrays to define which parameters can be used unnamed.
	 *        The second value in array-form is reserved for flags. Currently, Processor::PARAM_UNNAMED determines that
	 *        the parameter has no name which can be used to set it. Therefore all these parameters must be set before
	 *        any named parameter. The effect is, that '=' within the string won't confuse the parameter anymore like
	 *        it would happen with default parameters that still have a name as well.
	 */
	public function setFunctionParams( array $rawParams, array $parameterDefinitions = [], array $defaultParams = [] ) {
		$lastUnnamedDefaultNr = -1;

		/*
		 * Find last parameter with self::PARAM_UNNAMED set. Tread all parameters in front as
		 * the flag were set for them as well to ensure that there can't be any unnamed params
		 * after the first named param. Wouldn't be possible to determine which unnamed value
		 * belongs to which parameter otherwise.
		 */
		for( $i = count( $defaultParams ) - 1; $i >= 0; $i-- ) {
			$dflt = $defaultParams[$i];
			if( is_array( $dflt ) && !empty( $dflt[1] ) && ( $dflt[1] | self::PARAM_UNNAMED ) ) {
				$lastUnnamedDefaultNr = $i;
				break;
			}
		}

		$parameters = [];
		$nr = 0;
		$defaultNr = 0;

		foreach ( $rawParams as $arg ) {
			// Only take into account strings. If the value is not a string,
			// it is not a raw parameter, and can not be parsed correctly in all cases.
			if ( is_string( $arg ) ) {
				$parts = explode( '=', $arg, ( $nr <= $lastUnnamedDefaultNr ? 1 : 2 ) );

				// If there is only one part, no parameter name is provided, so try default parameter assignment.
				// Default parameters having self::PARAM_UNNAMED set for having no name alias go here in any case.
				if ( count( $parts ) == 1 ) {
					// Default parameter assignment is only possible when there are default parameters!
					if ( count( $defaultParams ) > 0 ) {
						$defaultParam = array_shift( $defaultParams );
						if( is_array( $defaultParam ) ) {
							$defaultParam = $defaultParam[0];
						}
						$defaultParam = strtolower( $defaultParam );

						$parameters[$defaultParam] = [
							'original-value' => trim( $parts[0] ),
							'default' => $defaultNr,
							'position' => $nr
						];
						$defaultNr++;
					}
				} else {
					$paramName = trim( strtolower( $parts[0] ) );

					$parameters[$paramName] = [
						'original-value' => trim( $parts[1] ),
						'default' => false,
						'position' => $nr
					];

					// Let's not be evil, and remove the used parameter name from the default parameter list.
					// This code is basically a remove array element by value algorithm.
					$newDefaults = [];

					foreach( $defaultParams as $defaultParam ) {
						if ( $defaultParam != $paramName ) {
							$newDefaults[] = $defaultParam;
						}
					}

					$defaultParams = $newDefaults;
				}
			}

			$nr++;
		}

		$this->setParameters( $parameters, $parameterDefinitions );
	}

	/**
	 * @since 1.6.0
	 * @param ParamDefinition[] $paramDefinitions
	 */
	public function setParameterDefinitions( array $paramDefinitions ) {
		$this->paramDefinitions = $paramDefinitions;
	}

	/**
	 * Loops through a list of provided parameters, resolves aliasing and stores errors
	 * for unknown parameters and optionally for parameter overriding.
	 *
	 * @param array $parameters Parameter name as key, parameter value as value
	 * @param ParamDefinition[]|array[] $paramDefinitions DEPRECATED! Use @see setParameterDefinitions instead
	 */
	public function setParameters( array $parameters, array $paramDefinitions = [] ) {
		$this->paramDefinitions = ParamDefinition::getCleanDefinitions( $paramDefinitions );

		// Loop through all the user provided parameters, and distinguish between those that are allowed and those that are not.
		foreach ( $parameters as $paramName => $paramData ) {
			if ( $this->options->lowercaseNames() ) {
				$paramName = strtolower( $paramName );
			}

			if ( $this->options->trimNames() ) {
				$paramName = trim( $paramName );
			}

			$paramValue = is_array( $paramData ) ? $paramData['original-value'] : $paramData;

			$this->rawParameters[$paramName] = $paramValue;
		}
	}

	/**
	 * @param string $message
	 * @param string[] $tags
	 * @param integer $severity
	 */
	private function registerNewError( string $message, array $tags = [], int $severity = ProcessingError::SEVERITY_NORMAL ) {
		$this->registerError(
			new ProcessingError(
				$message,
				$severity,
				$this->options->getName(),
				(array)$tags
			)
		);
	}

	private function registerError( ProcessingError $error ) {
		$error->element = $this->options->getName();
		$this->errors[] = $error;
		ProcessingErrorHandler::addError( $error );
	}

	/**
	 * Validates and formats all the parameters (but aborts when a fatal error occurs).
	 *
	 * @since 0.4
	 * @deprecated since 1.0, use processParameters
	 */
	public function validateParameters() {
		$this->doParamProcessing();
	}

	public function processParameters(): ProcessingResult {
		$this->doParamProcessing();

		if ( !$this->hasFatalError() && $this->options->unknownIsInvalid() ) {
			// Loop over the remaining raw parameters.
			// These are unrecognized parameters, as they where not used by any parameter definition.
			foreach ( $this->rawParameters as $paramName => $paramValue ) {
				$this->registerNewError(
					$paramName . ' is not a valid parameter', // TODO
					[ $paramName ]
				);
			}
		}

		return $this->newProcessingResult();
	}

	private function newProcessingResult(): ProcessingResult {
		$parameters = [];

		if ( !is_array( $this->params ) ) {
			$this->params = [];
		}

		foreach ( $this->params as $parameter ) {
			// TODO
			$processedParam = new ProcessedParam(
				$parameter->getName(),
				$parameter->getValue(),
				$parameter->wasSetToDefault()
			);

			// TODO: it is possible these values where set even when the value defaulted,
			// so this logic is not correct and could be improved
			if ( !$parameter->wasSetToDefault() ) {
				$processedParam->setOriginalName( $parameter->getOriginalName() );
				$processedParam->setOriginalValue( $parameter->getOriginalValue() );
			}

			$parameters[$processedParam->getName()] = $processedParam;
		}

		return new ProcessingResult(
			$parameters,
			$this->getErrors()
		);
	}

	private function doParamProcessing() {
		$this->errors = [];

		$this->getParamsToProcess( [], $this->paramDefinitions );

		while ( $this->paramsToHandle !== [] && !$this->hasFatalError() ) {
			$this->processOneParam();
		}
	}

	private function processOneParam() {
		$paramName = array_shift( $this->paramsToHandle );
		$definition = $this->paramDefinitions[$paramName];

		$param = new Param( $definition );

		$setUserValue = $this->attemptToSetUserValue( $param );

		// If the parameter is required but not provided, register a fatal error and stop processing.
		if ( !$setUserValue && $param->isRequired() ) {
			$this->registerNewError(
				"Required parameter '$paramName' is missing", // FIXME: i18n validator_error_required_missing
				[ $paramName, 'missing' ],
				ProcessingError::SEVERITY_FATAL
			);
			return;
		}

		$this->params[$param->getName()] = $param;

		$initialSet = $this->paramDefinitions;

		$param->process( $this->paramDefinitions, $this->params, $this->options );

		foreach ( $param->getErrors() as $error ) {
			$this->registerError( $error );
		}

		if ( $param->hasFatalError() ) {
			return;
		}

		$this->getParamsToProcess( $initialSet, $this->paramDefinitions );
	}

	/**
	 * Gets an ordered list of parameters to process.
	 * @throws \UnexpectedValueException
	 */
	private function getParamsToProcess( array $initialParamSet, array $resultingParamSet ) {
		if ( $initialParamSet === [] ) {
			$this->paramsToHandle = array_keys( $resultingParamSet );
		}
		else {
			if ( !is_array( $this->paramsToHandle ) ) {
				$this->paramsToHandle = [];
			}

			foreach ( $resultingParamSet as $paramName => $parameter ) {
				if ( !array_key_exists( $paramName, $initialParamSet ) ) {
					$this->paramsToHandle[] = $paramName;
				}
			}
		}

		$this->paramsToHandle = $this->getParameterNamesInEvaluationOrder( $this->paramDefinitions, $this->paramsToHandle );
	}

	/**
	 * @param ParamDefinition[] $paramDefinitions
	 * @param string[] $paramsToHandle
	 *
	 * @return array
	 */
	private function getParameterNamesInEvaluationOrder( array $paramDefinitions, array $paramsToHandle ): array {
		$dependencyList = [];

		foreach ( $paramsToHandle as $paramName ) {
			$dependencies = [];

			if ( !array_key_exists( $paramName, $paramDefinitions ) ) {
				throw new \UnexpectedValueException( 'Unexpected parameter name "' . $paramName . '"' );
			}

			if ( !is_object( $paramDefinitions[$paramName] ) || !( $paramDefinitions[$paramName] instanceof ParamDefinition ) ) {
				throw new \UnexpectedValueException( 'Parameter "' . $paramName . '" is not a ParamDefinition' );
			}

			// Only include dependencies that are in the list of parameters to handle.
			foreach ( $paramDefinitions[$paramName]->getDependencies() as $dependency ) {
				if ( in_array( $dependency, $paramsToHandle ) ) {
					$dependencies[] = $dependency;
				}
			}

			$dependencyList[$paramName] = $dependencies;
		}

		$sorter = new TopologicalSort( $dependencyList, true );

		return $sorter->doSort();
	}

	/**
	 * Tries to find a matching user provided value and, when found, assigns it
	 * to the parameter, and removes it from the raw values. Returns a boolean
	 * indicating if there was any user value set or not.
	 */
	private function attemptToSetUserValue( Param $param ): bool {
		if ( array_key_exists( $param->getName(), $this->rawParameters ) ) {
			$param->setUserValue( $param->getName(), $this->rawParameters[$param->getName()], $this->options );
			unset( $this->rawParameters[$param->getName()] );
			return true;
		}
		else {
			foreach ( $param->getDefinition()->getAliases() as $alias ) {
				if ( array_key_exists( $alias, $this->rawParameters ) ) {
					$param->setUserValue( $alias, $this->rawParameters[$alias], $this->options );
					unset( $this->rawParameters[$alias] );
					return true;
				}
			}
		}

		return false;
	}

	/**
	 * @deprecated since 1.0
	 * @return Param[]
	 */
	public function getParameters(): array {
		return $this->params;
	}

	/**
	 * @deprecated since 1.0
	 */
	public function getParameter( string $parameterName ): Param {
		return $this->params[$parameterName];
	}

	/**
	 * Returns an associative array with the parameter names as key and their
	 * corresponding values as value.
	 * @deprecated since 1.7 - use processParameters() return value
	 */
	public function getParameterValues(): array {
		$parameters = [];

		foreach ( $this->params as $parameter ) {
			$parameters[$parameter->getName()] = $parameter->getValue();
		}

		return $parameters;
	}

	/**
	 * @deprecated since 1.7 - use processParameters() return value
	 * @return ProcessingError[]
	 */
	public function getErrors(): array {
		return $this->errors;
	}

	/**
	 * @deprecated since 1.7 - use processParameters() return value
	 * @return string[]
	 */
	public function getErrorMessages(): array {
		$errors = [];

		foreach ( $this->errors as $error ) {
			$errors[] = $error->getMessage();
		}

		return $errors;
	}

	/**
	 * @deprecated since 1.7 - use processParameters() return value
	 */
	public function hasErrors(): bool {
		return !empty( $this->errors );
	}

	/**
	 * @deprecated since 1.7 - use processParameters() return value
	 * @return ProcessingError|boolean false
	 */
	public function hasFatalError() {
		foreach ( $this->errors as $error ) {
			if ( $error->isFatal() ) {
				return $error;
			}
		}

		return false;
	}

}