summaryrefslogtreecommitdiff
path: root/www/wiki/vendor/param-processor/param-processor/src/ProcessingError.php
blob: 034186614e1954b36a9e3f0e8304d7129285555e (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
<?php

namespace ParamProcessor;

/**
 * @since 1.0
 *
 * @licence GNU GPL v2+
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class ProcessingError {

	const SEVERITY_MINOR = 0;	// Minor error. ie a deprecation notice
	const SEVERITY_LOW = 1;		// Lower-then-normal severity. ie an unknown parameter
	const SEVERITY_NORMAL = 2;	// Normal severity. ie an invalid value provided
	const SEVERITY_HIGH = 3;	// Higher-then-normal severity. ie an invalid value for a significant parameter
	const SEVERITY_FATAL = 4;	// Fatal error. Either a missing or an invalid required parameter

	const ACTION_IGNORE = 0;	// Ignore the error
	const ACTION_LOG = 1;		// Log the error
	const ACTION_WARN = 2;		// Warn that there is an error
	const ACTION_SHOW = 3;		// Show the error
	const ACTION_DEMAND = 4;	// Show the error and don't render output

	public $message;
	public $severity;

	/**
	 * List of 'tags' for the error. This is mainly meant for indicating an error
	 * type, such as 'missing parameter' or 'invalid value', but allows for multiple
	 * such indications.
	 *
	 * @since 0.4
	 *
	 * @var string[]
	 */
	private $tags;

	/**
	 * Where the error occurred.
	 *
	 * @since 0.4
	 *
	 * @var string|bool
	 */
	public $element;

	/**
	 * @param string $message
	 * @param integer $severity
	 * @param string|bool $element
	 * @param string[] $tags
	 */
	public function __construct( string $message, int $severity = self::SEVERITY_NORMAL, $element = false, array $tags = [] ) {
		$this->message = $message;
		$this->severity = $severity;
		$this->element = $element;
		$this->tags = $tags;
	}

	/**
	 * Adds one or more tags.
	 *
	 * @since 0.4.1
	 *
	 * @param string|string[] $criteria
	 */
	public function addTags() {
		$args = func_get_args();
		$this->tags = array_merge( $this->tags, is_array( $args[0] ) ? $args[0] : $args );
	}

	public function getMessage(): string {
		return $this->message;
	}

	/**
	 * Returns the element this error occurred at, or 'unknown' when i's unknown.
	 */
	public function getElement(): string {
		return ( $this->element === false || $this->element === '' ) ? 'unknown' : $this->element;
	}

	/**
	 * Returns the severity of the error.
	 * @return integer Element of the ProcessingError::SEVERITY_ enum
	 */
	public function getSeverity(): int {
		return $this->severity;
	}

	/**
	 * Returns if the severity is equal to or bigger then the provided one.
	 */
	public function hasSeverity( int $severity ): bool {
		return $this->severity >= $severity;
	}

	/**
	 * Returns if the error has a certain tag.
	 */
	public function hasTag( string $tag ): bool {
		return in_array( $tag, $this->tags );
	}

	/**
	 * @return string[]
	 */
	public function getTags(): array {
		return $this->tags;
	}

	/**
	 * Returns the action associated with the errors severity.
	 *
	 * @return integer Element of the ProcessingError::ACTION_ enum
	 * @throws \Exception
	 */
	public function getAction(): int {
		// TODO: as option
		$errorActions = [
			ProcessingError::SEVERITY_MINOR => ProcessingError::ACTION_LOG,
			ProcessingError::SEVERITY_LOW => ProcessingError::ACTION_WARN,
			ProcessingError::SEVERITY_NORMAL => ProcessingError::ACTION_SHOW,
			ProcessingError::SEVERITY_HIGH => ProcessingError::ACTION_DEMAND,
		];

		if ( $this->severity === self::SEVERITY_FATAL ) {
			// This action should not be configurable, as lowering it would break in the Validator class.
			return self::ACTION_DEMAND;
		}
		elseif ( array_key_exists( $this->severity, $errorActions ) ) {
			return $errorActions[$this->severity];
		}
		else {
			throw new \Exception( "No action associated with error severity '$this->severity'" );
		}
	}

	/**
	 * Returns if the action associated with the severity is equal to or bigger then the provided one.
	 */
	public function hasAction( int $action ): bool {
		return $this->getAction() >= $action;
	}

	/**
	 * Returns if the error is fatal.
	 */
	public function isFatal(): bool {
		return $this->hasSeverity( self::SEVERITY_FATAL );
	}

	/**
	 * Returns if the error should be logged.
	 */
	public function shouldLog(): bool {
		return $this->hasAction( self::ACTION_LOG );
	}

	/**
	 * Returns if there should be a warning that errors are present.
	 */
	public function shouldWarn(): bool {
		return $this->hasAction( self::ACTION_WARN );
	}

	/**
	 * Returns if the error message should be shown.
	 */
	public function shouldShow(): bool {
		return $this->hasAction( self::ACTION_SHOW );
	}

	/**
	 * Returns if the error message should be shown, and the output not be rendered.
	 */
	public function shouldDemand(): bool {
		return $this->hasAction( self::ACTION_DEMAND );
	}

}