HookDefinition::getDefaultParameters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace ParserHooks;
4
5
use InvalidArgumentException;
6
use ParamProcessor\ParamDefinition;
7
8
/**
9
 * Definition of a parser hooks signature.
10
 *
11
 * @since 1.0
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class HookDefinition {
17
18
	protected $names;
19
	protected $parameters;
20
	protected $defaultParameters;
21
22
	/**
23
	 * @since 1.0
24
	 *
25
	 * @param string|string[] $names
26
	 * The name or the names of your parser hook.
27
	 * 
28
	 * @param ParamDefinition[] $parameters
29
	 * The definitions of the parameters your parser hook accepts.
30
	 * For more information on ParamDefinition, see the ParamProcessor
31
	 * documentation at https://github.com/JeroenDeDauw/ParamProcessor
32
	 * 
33
	 * @param string|string[] $defaultParameters
34
	 * An ordered list of parameter names, denoting which parameters are the defaults.
35
	 * If the user specifies a value without indicating the parameter name, the
36
	 * first default parameter is used.
37
	 *
38
	 * @throws InvalidArgumentException
39
	 */
40 19
	public function __construct( $names, array $parameters = [], $defaultParameters = [] ) {
41 19
		$this->setNames( $names );
42 16
		$this->parameters = $parameters;
43 16
		$this->setDefaultParams( $defaultParameters );
44 14
	}
45
46 19
	protected function setNames( $names ) {
47 19
		$this->names = $this->getStringList( $names );
48
49 17
		if ( $this->names === [] ) {
50 1
			throw new InvalidArgumentException( 'The list of names cannot be empty' );
51
		}
52 16
	}
53
54 16
	protected function setDefaultParams( $defaultParameters ) {
55 16
		$this->defaultParameters = $this->getStringList( $defaultParameters );
56 14
	}
57
58 19
	protected function getStringList( $strings ) {
59 19
		if ( !is_string( $strings ) && !is_array( $strings ) ) {
60 2
			throw new InvalidArgumentException( 'Argument needs to be an array or a string' );
61
		}
62
63 18
		$strings = (array)$strings;
64
65 18
		$this->assertAreStrings( $strings );
66
67 17
		return $strings;
68
	}
69
70 18
	protected function assertAreStrings( array $strings ) {
71 18
		foreach ( $strings as $string ) {
72 17
			if ( !is_string( $string ) ) {
73 2
				throw new InvalidArgumentException( 'All elements must be strings' );
74
			}
75
		}
76 17
	}
77
78
	/**
79
	 * Returns the names of the hook. This returned
80
	 * array contains at least one name.
81
	 *
82
	 * @since 1.0
83
	 *
84
	 * @return string[]
85
	 */
86 7
	public function getNames() {
87 7
		return $this->names;
88
	}
89
90
	/**
91
	 * Returns the definitions of the parameters that the hook accepts.
92
	 *
93
	 * @since 1.0
94
	 *
95
	 * @return ParamDefinition[]
96
	 */
97 2
	public function getParameters() {
98 2
		return $this->parameters;
99
	}
100
101
	/**
102
	 * Returns an ordered list of parameter names for parameters
103
	 * that can be provided without specifying a name. The first
104
	 * parameter in the list is the one to be used for the first
105
	 * unnamed parameter encountered.
106
	 *
107
	 * @since 1.0
108
	 *
109
	 * @return string[]
110
	 */
111 7
	public function getDefaultParameters() {
112 7
		return $this->defaultParameters;
113
	}
114
115
}
116