1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @file |
4
|
|
|
* |
5
|
|
|
* Options management. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace QueryPath; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Manage default options. |
13
|
|
|
* |
14
|
|
|
* This class stores the default options for QueryPath. When a new |
15
|
|
|
* QueryPath object is constructed, options specified here will be |
16
|
|
|
* used. |
17
|
|
|
* |
18
|
|
|
* <b>Details</b> |
19
|
|
|
* This class defines no options of its own. Instead, it provides a |
20
|
|
|
* central tool for developers to override options set by QueryPath. |
21
|
|
|
* When a QueryPath object is created, it will evaluate options in the |
22
|
|
|
* following order: |
23
|
|
|
* |
24
|
|
|
* - Options passed into qp() have highest priority. |
25
|
|
|
* - Options in QueryPath::Options (this class) have the next highest priority. |
26
|
|
|
* - If the option is not specified elsewhere, QueryPath will use its own defaults. |
27
|
|
|
* |
28
|
|
|
* @see qp() |
29
|
|
|
* @see QueryPath::Options::set() |
30
|
|
|
* @ingroup querypath_util |
31
|
|
|
*/ |
32
|
|
|
class Options |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* This is the static options array. |
37
|
|
|
* |
38
|
|
|
* Use the {@link set()}, {@link get()}, and {@link merge()} to |
39
|
|
|
* modify this array. |
40
|
|
|
*/ |
41
|
|
|
static $options = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set the default options. |
45
|
|
|
* |
46
|
|
|
* The passed-in array will be used as the default options list. |
47
|
|
|
* |
48
|
|
|
* @param array $array |
49
|
|
|
* An associative array of options. |
50
|
|
|
*/ |
51
|
|
|
static function set($array) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
self::$options = $array; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the default options. |
58
|
|
|
* |
59
|
|
|
* Get all options currently set as default. |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
* An array of options. Note that only explicitly set options are |
63
|
|
|
* returned. {@link QueryPath} defines default options which are not |
64
|
|
|
* stored in this object. |
65
|
|
|
*/ |
66
|
|
|
static function get() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
return self::$options; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Merge the provided array with existing options. |
73
|
|
|
* |
74
|
|
|
* On duplicate keys, the value in $array will overwrite the |
75
|
|
|
* value stored in the options. |
76
|
|
|
* |
77
|
|
|
* @param array $array |
78
|
|
|
* Associative array of options to merge into the existing options. |
79
|
|
|
*/ |
80
|
|
|
static function merge($array) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
self::$options = $array + self::$options; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns true of the specified key is already overridden in this object. |
87
|
|
|
* |
88
|
|
|
* @param string $key |
89
|
|
|
* The key to search for. |
90
|
|
|
*/ |
91
|
|
|
static function has($key) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
return array_key_exists($key, self::$options); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.