PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.3
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.3
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / rosell-dk / htaccess-capability-tester / src / Testers / Helpers / ResponseInterpreter.php

ResponseInterpreter.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.3, at vendor/rosell-dk/htaccess-capability-tester/src/Testers/Helpers/ResponseInterpreter.php

169 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace HtaccessCapabilityTester\Testers\Helpers;
4
5 use \HtaccessCapabilityTester\HttpResponse;
6 use \HtaccessCapabilityTester\TestResult;
7 use \HtaccessCapabilityTester\Testers\AbstractTester;
8
9 /**
10 * Class for interpreting responses using a defined interpretation table.
11 *
12 * @package HtaccessCapabilityTester
13 * @author Bjørn Rosell <[email protected]>
14 * @since Class available since 0.7
15 */
16 class ResponseInterpreter
17 {
18
19 /**
20 * Parse status string (failure | success | inconclusive) to bool|null.
21 *
22 * @param string $statusString (failure | success | inconclusive)
23 * @return bool|null
24 */
25 private static function parseStatusString($statusString)
26 {
27 $status = null;
28 switch ($statusString) {
29 case 'failure':
30 $status = false;
31 break;
32 case 'inconclusive':
33 $status = null;
34 break;
35 case 'success':
36 $status = true;
37 break;
38 }
39 return $status;
40 }
41
42 /**
43 * Interpret headers line
44 *
45 * @param HttpResponse $response
46 * @param string $operator (has-key | )
47 * @param string $fieldName field name of the header
48 * @param string $fieldValue (optional) field value to look for. Only required when
49 * operator is "contains-key-value" or "not-contains-key-value"
50 * @return bool true if the condition matches, false otherwise
51 */
52 private static function evaluateHeadersLine($response, $operator, $fieldName, $fieldValue)
53 {
54 switch ($operator) {
55 case 'contains-key':
56 return $response->hasHeader($fieldName);
57 case 'not-contains-key':
58 return (!($response->hasHeader($fieldName)));
59 case 'contains-key-value':
60 return $response->hasHeaderValue($fieldName, $fieldValue);
61 case 'not-contains-key-value':
62 return (!($response->hasHeaderValue($fieldName, $fieldValue)));
63 }
64 return false;
65 }
66
67 /**
68 * Interpret string line (body or status-code)
69 *
70 * @param HttpResponse $response
71 * @param string $property ("body" or "status-code")
72 * @param string $operator (is-empty | equals | not-equals | begins-with)
73 * @param string $arg1 (only required for some operators)
74 *
75 * @return bool true if the condition matches, false otherwise
76 */
77 private static function evaluateStringLine($response, $property, $operator, $arg1)
78 {
79 $val = '';
80 switch ($property) {
81 case 'status-code':
82 $val = $response->statusCode;
83 break;
84 case 'body':
85 $val = $response->body;
86 break;
87 }
88
89 switch ($operator) {
90 case 'is-empty':
91 return ($val == '');
92 case 'equals':
93 return ($val == $arg1);
94 case 'not-equals':
95 return ($val != $arg1);
96 case 'begins-with':
97 return (strpos($val, $arg1) === 0);
98 }
99 return false;
100 }
101
102
103 /**
104 * Interpret line.
105 *
106 * @param HttpResponse $response
107 * @param array $line
108 *
109 * @return TestResult|null If the condition matches, a TestResult is returned, otherwise null
110 */
111 private static function interpretLine($response, $line)
112 {
113 // ie:
114 // ['inconclusive', 'body', 'is-empty'],
115 // ['failure', 'statusCode', 'equals', '500']
116 // ['success', 'headers', 'contains-key-value', 'X-Response-Header-Test', 'test'],
117
118 $status = self::parseStatusString($line[0]);
119
120 if (!isset($line[1])) {
121 return new TestResult($status, '');
122 }
123
124 $propertyToExamine = $line[1];
125 $operator = $line[2];
126 $arg1 = (isset($line[3]) ? $line[3] : '');
127 $arg2 = (isset($line[4]) ? $line[4] : '');
128
129 if ($propertyToExamine == 'headers') {
130 $match = self::evaluateHeadersLine($response, $operator, $arg1, $arg2);
131 } else {
132 $match = self::evaluateStringLine($response, $propertyToExamine, $operator, $arg1);
133 }
134 if ($match) {
135 $reason = $propertyToExamine . ' ' . $operator;
136 if (isset($line[3])) {
137 $reason .= ' "' . implode('" "', array_slice($line, 3)) . '"';
138 }
139 /*
140 if (($propertyToExamine == 'status-code') && ($operator == 'not-equals') && (gettype($val) == 'string')) {
141 $reason .= ' - it was: ' . $val;
142 }*/
143 return new TestResult($status, $reason);
144 }
145
146 return null;
147 }
148
149 /**
150 * Interpret a response using an interpretation table.
151 *
152 * @param HttpResponse $response
153 * @param array $interpretationTable
154 *
155 * @return TestResult If there is no match, the test result will have status = false and
156 * info = "no-match".
157 */
158 public static function interpret($response, $interpretationTable)
159 {
160 foreach ($interpretationTable as $i => $line) {
161 $testResult = self::interpretLine($response, $line);
162 if (!is_null($testResult)) {
163 return $testResult;
164 }
165 }
166 return new TestResult(null, 'no-match');
167 }
168 }
169