Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
Validator | |
0.00% |
0 / 33 |
|
0.00% |
0 / 7 |
342 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
validate | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
printDebugCheckConfig | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
checkConfig | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
56 | |||
normalizeConfigValue | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
debugCheckConfig | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
output | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
outputIndented | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace CirrusSearch\Maintenance\Validators; |
4 | |
5 | use CirrusSearch\Maintenance\Printer; |
6 | |
7 | abstract class Validator { |
8 | /** |
9 | * @var Printer |
10 | */ |
11 | protected $out; |
12 | |
13 | /** |
14 | * @var bool |
15 | */ |
16 | protected $printDebugCheckConfig = false; |
17 | |
18 | /** |
19 | * @param Printer $out to relay output to. |
20 | */ |
21 | public function __construct( Printer $out ) { |
22 | $this->out = $out; |
23 | } |
24 | |
25 | /** |
26 | * @return \Status |
27 | */ |
28 | abstract public function validate(); |
29 | |
30 | /** |
31 | * @param bool $print |
32 | */ |
33 | public function printDebugCheckConfig( $print = true ) { |
34 | $this->printDebugCheckConfig = (bool)$print; |
35 | } |
36 | |
37 | /** |
38 | * @param mixed $actual |
39 | * @param array $required |
40 | * @param string|null $indent |
41 | * @return bool |
42 | */ |
43 | protected function checkConfig( $actual, array $required, $indent = null ) { |
44 | foreach ( $required as $key => $value ) { |
45 | $this->debugCheckConfig( "\n$indent$key: " ); |
46 | if ( !array_key_exists( $key, $actual ) ) { |
47 | $this->debugCheckConfig( "not found..." ); |
48 | return false; |
49 | } |
50 | if ( is_array( $value ) ) { |
51 | $this->debugCheckConfig( "descend..." ); |
52 | if ( !is_array( $actual[ $key ] ) ) { |
53 | $this->debugCheckConfig( "other not array..." ); |
54 | return false; |
55 | } |
56 | if ( !$this->checkConfig( $actual[ $key ], $value, $indent . "\t" ) ) { |
57 | return false; |
58 | } |
59 | continue; |
60 | } |
61 | |
62 | $actual[ $key ] = $this->normalizeConfigValue( $actual[ $key ] ); |
63 | $value = $this->normalizeConfigValue( $value ); |
64 | $this->debugCheckConfig( $actual[ $key ] . " ?? $value..." ); |
65 | // Note that I really mean !=, not !==. Coercion is cool here. |
66 | // print $actual[ $key ] . " $value\n"; |
67 | if ( $actual[ $key ] != $value ) { |
68 | $this->debugCheckConfig( 'different...' ); |
69 | return false; |
70 | } |
71 | } |
72 | return true; |
73 | } |
74 | |
75 | /** |
76 | * Normalize a config value for comparison. Elasticsearch will accept all kinds |
77 | * of config values but it tends to through back 'true' for true and 'false' for |
78 | * false so we normalize everything. Sometimes, oddly, it'll through back false |
79 | * for false.... |
80 | * |
81 | * @param mixed $value |
82 | * @return mixed |
83 | */ |
84 | private function normalizeConfigValue( $value ) { |
85 | if ( $value === true ) { |
86 | return 'true'; |
87 | } elseif ( $value === false ) { |
88 | return 'false'; |
89 | } |
90 | return $value; |
91 | } |
92 | |
93 | /** |
94 | * @param string $string |
95 | */ |
96 | private function debugCheckConfig( $string ) { |
97 | if ( $this->printDebugCheckConfig ) { |
98 | $this->output( $string ); |
99 | } |
100 | } |
101 | |
102 | /** |
103 | * @param string $message |
104 | * @param mixed|null $channel |
105 | */ |
106 | protected function output( $message, $channel = null ) { |
107 | if ( $this->out ) { |
108 | $this->out->output( $message, $channel ); |
109 | } |
110 | } |
111 | |
112 | /** |
113 | * @param string $message |
114 | */ |
115 | protected function outputIndented( $message ) { |
116 | if ( $this->out ) { |
117 | $this->out->outputIndented( $message ); |
118 | } |
119 | } |
120 | } |