@@ -13,390 +13,390 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class DNParser |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * DN string. |
|
| 18 | - * |
|
| 19 | - * @var string |
|
| 20 | - */ |
|
| 21 | - private $_dn; |
|
| 16 | + /** |
|
| 17 | + * DN string. |
|
| 18 | + * |
|
| 19 | + * @var string |
|
| 20 | + */ |
|
| 21 | + private $_dn; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * DN string length. |
|
| 25 | - * |
|
| 26 | - * @var int |
|
| 27 | - */ |
|
| 28 | - private $_len; |
|
| 23 | + /** |
|
| 24 | + * DN string length. |
|
| 25 | + * |
|
| 26 | + * @var int |
|
| 27 | + */ |
|
| 28 | + private $_len; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * RFC 2253 special characters. |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - const SPECIAL_CHARS = ",=+<>#;"; |
|
| 30 | + /** |
|
| 31 | + * RFC 2253 special characters. |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + const SPECIAL_CHARS = ",=+<>#;"; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Parse distinguished name string to name-components. |
|
| 39 | - * |
|
| 40 | - * @param string $dn |
|
| 41 | - * @return array |
|
| 42 | - */ |
|
| 43 | - public static function parseString($dn) |
|
| 44 | - { |
|
| 45 | - $parser = new self($dn); |
|
| 46 | - return $parser->parse(); |
|
| 47 | - } |
|
| 37 | + /** |
|
| 38 | + * Parse distinguished name string to name-components. |
|
| 39 | + * |
|
| 40 | + * @param string $dn |
|
| 41 | + * @return array |
|
| 42 | + */ |
|
| 43 | + public static function parseString($dn) |
|
| 44 | + { |
|
| 45 | + $parser = new self($dn); |
|
| 46 | + return $parser->parse(); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Escape a AttributeValue string conforming to RFC 2253. |
|
| 51 | - * |
|
| 52 | - * @link https://tools.ietf.org/html/rfc2253#section-2.4 |
|
| 53 | - * @param string $str |
|
| 54 | - * @return string |
|
| 55 | - */ |
|
| 56 | - public static function escapeString($str) |
|
| 57 | - { |
|
| 58 | - // one of the characters ",", "+", """, "\", "<", ">" or ";" |
|
| 59 | - $str = preg_replace('/([,\+"\\\<\>;])/u', '\\\\$1', $str); |
|
| 60 | - // a space character occurring at the end of the string |
|
| 61 | - $str = preg_replace('/( )$/u', '\\\\$1', $str); |
|
| 62 | - // a space or "#" character occurring at the beginning of the string |
|
| 63 | - $str = preg_replace('/^([ #])/u', '\\\\$1', $str); |
|
| 64 | - // implementation specific special characters |
|
| 65 | - $str = preg_replace_callback('/([\pC])/u', |
|
| 66 | - function ($m) { |
|
| 67 | - $octets = str_split(bin2hex($m[1]), 2); |
|
| 68 | - return implode("", |
|
| 69 | - array_map( |
|
| 70 | - function ($octet) { |
|
| 71 | - return '\\' . strtoupper($octet); |
|
| 72 | - }, $octets)); |
|
| 73 | - }, $str); |
|
| 74 | - return $str; |
|
| 75 | - } |
|
| 49 | + /** |
|
| 50 | + * Escape a AttributeValue string conforming to RFC 2253. |
|
| 51 | + * |
|
| 52 | + * @link https://tools.ietf.org/html/rfc2253#section-2.4 |
|
| 53 | + * @param string $str |
|
| 54 | + * @return string |
|
| 55 | + */ |
|
| 56 | + public static function escapeString($str) |
|
| 57 | + { |
|
| 58 | + // one of the characters ",", "+", """, "\", "<", ">" or ";" |
|
| 59 | + $str = preg_replace('/([,\+"\\\<\>;])/u', '\\\\$1', $str); |
|
| 60 | + // a space character occurring at the end of the string |
|
| 61 | + $str = preg_replace('/( )$/u', '\\\\$1', $str); |
|
| 62 | + // a space or "#" character occurring at the beginning of the string |
|
| 63 | + $str = preg_replace('/^([ #])/u', '\\\\$1', $str); |
|
| 64 | + // implementation specific special characters |
|
| 65 | + $str = preg_replace_callback('/([\pC])/u', |
|
| 66 | + function ($m) { |
|
| 67 | + $octets = str_split(bin2hex($m[1]), 2); |
|
| 68 | + return implode("", |
|
| 69 | + array_map( |
|
| 70 | + function ($octet) { |
|
| 71 | + return '\\' . strtoupper($octet); |
|
| 72 | + }, $octets)); |
|
| 73 | + }, $str); |
|
| 74 | + return $str; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Constructor. |
|
| 79 | - * |
|
| 80 | - * @param string $dn Distinguised name |
|
| 81 | - */ |
|
| 82 | - protected function __construct($dn) |
|
| 83 | - { |
|
| 84 | - $this->_dn = $dn; |
|
| 85 | - $this->_len = strlen($dn); |
|
| 86 | - } |
|
| 77 | + /** |
|
| 78 | + * Constructor. |
|
| 79 | + * |
|
| 80 | + * @param string $dn Distinguised name |
|
| 81 | + */ |
|
| 82 | + protected function __construct($dn) |
|
| 83 | + { |
|
| 84 | + $this->_dn = $dn; |
|
| 85 | + $this->_len = strlen($dn); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Parse DN to name-components. |
|
| 90 | - * |
|
| 91 | - * @throws \RuntimeException |
|
| 92 | - * @return array |
|
| 93 | - */ |
|
| 94 | - protected function parse() |
|
| 95 | - { |
|
| 96 | - $offset = 0; |
|
| 97 | - $name = $this->_parseName($offset); |
|
| 98 | - if ($offset < $this->_len) { |
|
| 99 | - $remains = substr($this->_dn, $offset); |
|
| 100 | - throw new \UnexpectedValueException( |
|
| 101 | - "Parser finished before the end of string" . |
|
| 102 | - ", remaining: '$remains'."); |
|
| 103 | - } |
|
| 104 | - return $name; |
|
| 105 | - } |
|
| 88 | + /** |
|
| 89 | + * Parse DN to name-components. |
|
| 90 | + * |
|
| 91 | + * @throws \RuntimeException |
|
| 92 | + * @return array |
|
| 93 | + */ |
|
| 94 | + protected function parse() |
|
| 95 | + { |
|
| 96 | + $offset = 0; |
|
| 97 | + $name = $this->_parseName($offset); |
|
| 98 | + if ($offset < $this->_len) { |
|
| 99 | + $remains = substr($this->_dn, $offset); |
|
| 100 | + throw new \UnexpectedValueException( |
|
| 101 | + "Parser finished before the end of string" . |
|
| 102 | + ", remaining: '$remains'."); |
|
| 103 | + } |
|
| 104 | + return $name; |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * Parse 'name'. |
|
| 109 | - * |
|
| 110 | - * name-component *("," name-component) |
|
| 111 | - * |
|
| 112 | - * @param int $offset |
|
| 113 | - * @return array Array of name-components |
|
| 114 | - */ |
|
| 115 | - private function _parseName(&$offset) |
|
| 116 | - { |
|
| 117 | - $idx = $offset; |
|
| 118 | - $names = array(); |
|
| 119 | - while ($idx < $this->_len) { |
|
| 120 | - $names[] = $this->_parseNameComponent($idx); |
|
| 121 | - if ($idx >= $this->_len) { |
|
| 122 | - break; |
|
| 123 | - } |
|
| 124 | - $this->_skipWs($idx); |
|
| 125 | - if ("," != $this->_dn[$idx] && ";" != $this->_dn[$idx]) { |
|
| 126 | - break; |
|
| 127 | - } |
|
| 128 | - $idx++; |
|
| 129 | - $this->_skipWs($idx); |
|
| 130 | - } |
|
| 131 | - $offset = $idx; |
|
| 132 | - return array_reverse($names); |
|
| 133 | - } |
|
| 107 | + /** |
|
| 108 | + * Parse 'name'. |
|
| 109 | + * |
|
| 110 | + * name-component *("," name-component) |
|
| 111 | + * |
|
| 112 | + * @param int $offset |
|
| 113 | + * @return array Array of name-components |
|
| 114 | + */ |
|
| 115 | + private function _parseName(&$offset) |
|
| 116 | + { |
|
| 117 | + $idx = $offset; |
|
| 118 | + $names = array(); |
|
| 119 | + while ($idx < $this->_len) { |
|
| 120 | + $names[] = $this->_parseNameComponent($idx); |
|
| 121 | + if ($idx >= $this->_len) { |
|
| 122 | + break; |
|
| 123 | + } |
|
| 124 | + $this->_skipWs($idx); |
|
| 125 | + if ("," != $this->_dn[$idx] && ";" != $this->_dn[$idx]) { |
|
| 126 | + break; |
|
| 127 | + } |
|
| 128 | + $idx++; |
|
| 129 | + $this->_skipWs($idx); |
|
| 130 | + } |
|
| 131 | + $offset = $idx; |
|
| 132 | + return array_reverse($names); |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * Parse 'name-component'. |
|
| 137 | - * |
|
| 138 | - * attributeTypeAndValue *("+" attributeTypeAndValue) |
|
| 139 | - * |
|
| 140 | - * @param int $offset |
|
| 141 | - * @return array Array of [type, value] tuples |
|
| 142 | - */ |
|
| 143 | - private function _parseNameComponent(&$offset) |
|
| 144 | - { |
|
| 145 | - $idx = $offset; |
|
| 146 | - $tvpairs = array(); |
|
| 147 | - while ($idx < $this->_len) { |
|
| 148 | - $tvpairs[] = $this->_parseAttrTypeAndValue($idx); |
|
| 149 | - $this->_skipWs($idx); |
|
| 150 | - if ($idx >= $this->_len || "+" != $this->_dn[$idx]) { |
|
| 151 | - break; |
|
| 152 | - } |
|
| 153 | - ++$idx; |
|
| 154 | - $this->_skipWs($idx); |
|
| 155 | - } |
|
| 156 | - $offset = $idx; |
|
| 157 | - return $tvpairs; |
|
| 158 | - } |
|
| 135 | + /** |
|
| 136 | + * Parse 'name-component'. |
|
| 137 | + * |
|
| 138 | + * attributeTypeAndValue *("+" attributeTypeAndValue) |
|
| 139 | + * |
|
| 140 | + * @param int $offset |
|
| 141 | + * @return array Array of [type, value] tuples |
|
| 142 | + */ |
|
| 143 | + private function _parseNameComponent(&$offset) |
|
| 144 | + { |
|
| 145 | + $idx = $offset; |
|
| 146 | + $tvpairs = array(); |
|
| 147 | + while ($idx < $this->_len) { |
|
| 148 | + $tvpairs[] = $this->_parseAttrTypeAndValue($idx); |
|
| 149 | + $this->_skipWs($idx); |
|
| 150 | + if ($idx >= $this->_len || "+" != $this->_dn[$idx]) { |
|
| 151 | + break; |
|
| 152 | + } |
|
| 153 | + ++$idx; |
|
| 154 | + $this->_skipWs($idx); |
|
| 155 | + } |
|
| 156 | + $offset = $idx; |
|
| 157 | + return $tvpairs; |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * Parse 'attributeTypeAndValue'. |
|
| 162 | - * |
|
| 163 | - * attributeType "=" attributeValue |
|
| 164 | - * |
|
| 165 | - * @param int $offset |
|
| 166 | - * @throws \UnexpectedValueException |
|
| 167 | - * @return array A tuple of [type, value]. Value may be either a string or |
|
| 168 | - * an Element, if it's encoded as hexstring. |
|
| 169 | - */ |
|
| 170 | - private function _parseAttrTypeAndValue(&$offset) |
|
| 171 | - { |
|
| 172 | - $idx = $offset; |
|
| 173 | - $type = $this->_parseAttrType($idx); |
|
| 174 | - $this->_skipWs($idx); |
|
| 175 | - if ($idx >= $this->_len || "=" != $this->_dn[$idx++]) { |
|
| 176 | - throw new \UnexpectedValueException("Invalid type and value pair."); |
|
| 177 | - } |
|
| 178 | - $this->_skipWs($idx); |
|
| 179 | - // hexstring |
|
| 180 | - if ($idx < $this->_len && "#" == $this->_dn[$idx]) { |
|
| 181 | - ++$idx; |
|
| 182 | - $data = $this->_parseAttrHexValue($idx); |
|
| 183 | - try { |
|
| 184 | - $value = Element::fromDER($data); |
|
| 185 | - } catch (DecodeException $e) { |
|
| 186 | - throw new \UnexpectedValueException( |
|
| 187 | - "Invalid DER encoding from hexstring.", 0, $e); |
|
| 188 | - } |
|
| 189 | - } else { |
|
| 190 | - $value = $this->_parseAttrStringValue($idx); |
|
| 191 | - } |
|
| 192 | - $offset = $idx; |
|
| 193 | - return array($type, $value); |
|
| 194 | - } |
|
| 160 | + /** |
|
| 161 | + * Parse 'attributeTypeAndValue'. |
|
| 162 | + * |
|
| 163 | + * attributeType "=" attributeValue |
|
| 164 | + * |
|
| 165 | + * @param int $offset |
|
| 166 | + * @throws \UnexpectedValueException |
|
| 167 | + * @return array A tuple of [type, value]. Value may be either a string or |
|
| 168 | + * an Element, if it's encoded as hexstring. |
|
| 169 | + */ |
|
| 170 | + private function _parseAttrTypeAndValue(&$offset) |
|
| 171 | + { |
|
| 172 | + $idx = $offset; |
|
| 173 | + $type = $this->_parseAttrType($idx); |
|
| 174 | + $this->_skipWs($idx); |
|
| 175 | + if ($idx >= $this->_len || "=" != $this->_dn[$idx++]) { |
|
| 176 | + throw new \UnexpectedValueException("Invalid type and value pair."); |
|
| 177 | + } |
|
| 178 | + $this->_skipWs($idx); |
|
| 179 | + // hexstring |
|
| 180 | + if ($idx < $this->_len && "#" == $this->_dn[$idx]) { |
|
| 181 | + ++$idx; |
|
| 182 | + $data = $this->_parseAttrHexValue($idx); |
|
| 183 | + try { |
|
| 184 | + $value = Element::fromDER($data); |
|
| 185 | + } catch (DecodeException $e) { |
|
| 186 | + throw new \UnexpectedValueException( |
|
| 187 | + "Invalid DER encoding from hexstring.", 0, $e); |
|
| 188 | + } |
|
| 189 | + } else { |
|
| 190 | + $value = $this->_parseAttrStringValue($idx); |
|
| 191 | + } |
|
| 192 | + $offset = $idx; |
|
| 193 | + return array($type, $value); |
|
| 194 | + } |
|
| 195 | 195 | |
| 196 | - /** |
|
| 197 | - * Parse 'attributeType'. |
|
| 198 | - * |
|
| 199 | - * (ALPHA 1*keychar) / oid |
|
| 200 | - * |
|
| 201 | - * @param int $offset |
|
| 202 | - * @throws \UnexpectedValueException |
|
| 203 | - * @return string |
|
| 204 | - */ |
|
| 205 | - private function _parseAttrType(&$offset) |
|
| 206 | - { |
|
| 207 | - $idx = $offset; |
|
| 208 | - // dotted OID |
|
| 209 | - $type = $this->_regexMatch('/^(?:oid\.)?([0-9]+(?:\.[0-9]+)*)/i', $idx); |
|
| 210 | - if (null === $type) { |
|
| 211 | - // name |
|
| 212 | - $type = $this->_regexMatch('/^[a-z][a-z0-9\-]*/i', $idx); |
|
| 213 | - if (null === $type) { |
|
| 214 | - throw new \UnexpectedValueException("Invalid attribute type."); |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - $offset = $idx; |
|
| 218 | - return $type; |
|
| 219 | - } |
|
| 196 | + /** |
|
| 197 | + * Parse 'attributeType'. |
|
| 198 | + * |
|
| 199 | + * (ALPHA 1*keychar) / oid |
|
| 200 | + * |
|
| 201 | + * @param int $offset |
|
| 202 | + * @throws \UnexpectedValueException |
|
| 203 | + * @return string |
|
| 204 | + */ |
|
| 205 | + private function _parseAttrType(&$offset) |
|
| 206 | + { |
|
| 207 | + $idx = $offset; |
|
| 208 | + // dotted OID |
|
| 209 | + $type = $this->_regexMatch('/^(?:oid\.)?([0-9]+(?:\.[0-9]+)*)/i', $idx); |
|
| 210 | + if (null === $type) { |
|
| 211 | + // name |
|
| 212 | + $type = $this->_regexMatch('/^[a-z][a-z0-9\-]*/i', $idx); |
|
| 213 | + if (null === $type) { |
|
| 214 | + throw new \UnexpectedValueException("Invalid attribute type."); |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + $offset = $idx; |
|
| 218 | + return $type; |
|
| 219 | + } |
|
| 220 | 220 | |
| 221 | - /** |
|
| 222 | - * Parse 'attributeValue' of string type. |
|
| 223 | - * |
|
| 224 | - * @param int $offset |
|
| 225 | - * @throws \UnexpectedValueException |
|
| 226 | - * @return string |
|
| 227 | - */ |
|
| 228 | - private function _parseAttrStringValue(&$offset) |
|
| 229 | - { |
|
| 230 | - $idx = $offset; |
|
| 231 | - if ($idx >= $this->_len) { |
|
| 232 | - return ""; |
|
| 233 | - } |
|
| 234 | - if ('"' == $this->_dn[$idx]) { // quoted string |
|
| 235 | - $val = $this->_parseQuotedAttrString($idx); |
|
| 236 | - } else { // string |
|
| 237 | - $val = $this->_parseAttrString($idx); |
|
| 238 | - } |
|
| 239 | - $offset = $idx; |
|
| 240 | - return $val; |
|
| 241 | - } |
|
| 221 | + /** |
|
| 222 | + * Parse 'attributeValue' of string type. |
|
| 223 | + * |
|
| 224 | + * @param int $offset |
|
| 225 | + * @throws \UnexpectedValueException |
|
| 226 | + * @return string |
|
| 227 | + */ |
|
| 228 | + private function _parseAttrStringValue(&$offset) |
|
| 229 | + { |
|
| 230 | + $idx = $offset; |
|
| 231 | + if ($idx >= $this->_len) { |
|
| 232 | + return ""; |
|
| 233 | + } |
|
| 234 | + if ('"' == $this->_dn[$idx]) { // quoted string |
|
| 235 | + $val = $this->_parseQuotedAttrString($idx); |
|
| 236 | + } else { // string |
|
| 237 | + $val = $this->_parseAttrString($idx); |
|
| 238 | + } |
|
| 239 | + $offset = $idx; |
|
| 240 | + return $val; |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | - /** |
|
| 244 | - * Parse plain 'attributeValue' string. |
|
| 245 | - * |
|
| 246 | - * @param int $offset |
|
| 247 | - * @throws \UnexpectedValueException |
|
| 248 | - * @return string |
|
| 249 | - */ |
|
| 250 | - private function _parseAttrString(&$offset) |
|
| 251 | - { |
|
| 252 | - $idx = $offset; |
|
| 253 | - $val = ""; |
|
| 254 | - $wsidx = null; |
|
| 255 | - while ($idx < $this->_len) { |
|
| 256 | - $c = $this->_dn[$idx]; |
|
| 257 | - // pair (escape sequence) |
|
| 258 | - if ("\\" == $c) { |
|
| 259 | - ++$idx; |
|
| 260 | - $val .= $this->_parsePairAfterSlash($idx); |
|
| 261 | - $wsidx = null; |
|
| 262 | - continue; |
|
| 263 | - } else if ('"' == $c) { |
|
| 264 | - throw new \UnexpectedValueException("Unexpected quotation."); |
|
| 265 | - } else if (false !== strpos(self::SPECIAL_CHARS, $c)) { |
|
| 266 | - break; |
|
| 267 | - } |
|
| 268 | - // keep track of the first consecutive whitespace |
|
| 269 | - if (' ' == $c) { |
|
| 270 | - if (null === $wsidx) { |
|
| 271 | - $wsidx = $idx; |
|
| 272 | - } |
|
| 273 | - } else { |
|
| 274 | - $wsidx = null; |
|
| 275 | - } |
|
| 276 | - // stringchar |
|
| 277 | - $val .= $c; |
|
| 278 | - ++$idx; |
|
| 279 | - } |
|
| 280 | - // if there was non-escaped whitespace in the end of the value |
|
| 281 | - if (null !== $wsidx) { |
|
| 282 | - $val = substr($val, 0, -($idx - $wsidx)); |
|
| 283 | - } |
|
| 284 | - $offset = $idx; |
|
| 285 | - return $val; |
|
| 286 | - } |
|
| 243 | + /** |
|
| 244 | + * Parse plain 'attributeValue' string. |
|
| 245 | + * |
|
| 246 | + * @param int $offset |
|
| 247 | + * @throws \UnexpectedValueException |
|
| 248 | + * @return string |
|
| 249 | + */ |
|
| 250 | + private function _parseAttrString(&$offset) |
|
| 251 | + { |
|
| 252 | + $idx = $offset; |
|
| 253 | + $val = ""; |
|
| 254 | + $wsidx = null; |
|
| 255 | + while ($idx < $this->_len) { |
|
| 256 | + $c = $this->_dn[$idx]; |
|
| 257 | + // pair (escape sequence) |
|
| 258 | + if ("\\" == $c) { |
|
| 259 | + ++$idx; |
|
| 260 | + $val .= $this->_parsePairAfterSlash($idx); |
|
| 261 | + $wsidx = null; |
|
| 262 | + continue; |
|
| 263 | + } else if ('"' == $c) { |
|
| 264 | + throw new \UnexpectedValueException("Unexpected quotation."); |
|
| 265 | + } else if (false !== strpos(self::SPECIAL_CHARS, $c)) { |
|
| 266 | + break; |
|
| 267 | + } |
|
| 268 | + // keep track of the first consecutive whitespace |
|
| 269 | + if (' ' == $c) { |
|
| 270 | + if (null === $wsidx) { |
|
| 271 | + $wsidx = $idx; |
|
| 272 | + } |
|
| 273 | + } else { |
|
| 274 | + $wsidx = null; |
|
| 275 | + } |
|
| 276 | + // stringchar |
|
| 277 | + $val .= $c; |
|
| 278 | + ++$idx; |
|
| 279 | + } |
|
| 280 | + // if there was non-escaped whitespace in the end of the value |
|
| 281 | + if (null !== $wsidx) { |
|
| 282 | + $val = substr($val, 0, -($idx - $wsidx)); |
|
| 283 | + } |
|
| 284 | + $offset = $idx; |
|
| 285 | + return $val; |
|
| 286 | + } |
|
| 287 | 287 | |
| 288 | - /** |
|
| 289 | - * Parse quoted 'attributeValue' string. |
|
| 290 | - * |
|
| 291 | - * @param int $offset Offset to starting quote |
|
| 292 | - * @throws \UnexpectedValueException |
|
| 293 | - * @return string |
|
| 294 | - */ |
|
| 295 | - private function _parseQuotedAttrString(&$offset) |
|
| 296 | - { |
|
| 297 | - $idx = $offset + 1; |
|
| 298 | - $val = ""; |
|
| 299 | - while ($idx < $this->_len) { |
|
| 300 | - $c = $this->_dn[$idx]; |
|
| 301 | - if ("\\" == $c) { // pair |
|
| 302 | - ++$idx; |
|
| 303 | - $val .= $this->_parsePairAfterSlash($idx); |
|
| 304 | - continue; |
|
| 305 | - } else if ('"' == $c) { |
|
| 306 | - ++$idx; |
|
| 307 | - break; |
|
| 308 | - } |
|
| 309 | - $val .= $c; |
|
| 310 | - ++$idx; |
|
| 311 | - } |
|
| 312 | - $offset = $idx; |
|
| 313 | - return $val; |
|
| 314 | - } |
|
| 288 | + /** |
|
| 289 | + * Parse quoted 'attributeValue' string. |
|
| 290 | + * |
|
| 291 | + * @param int $offset Offset to starting quote |
|
| 292 | + * @throws \UnexpectedValueException |
|
| 293 | + * @return string |
|
| 294 | + */ |
|
| 295 | + private function _parseQuotedAttrString(&$offset) |
|
| 296 | + { |
|
| 297 | + $idx = $offset + 1; |
|
| 298 | + $val = ""; |
|
| 299 | + while ($idx < $this->_len) { |
|
| 300 | + $c = $this->_dn[$idx]; |
|
| 301 | + if ("\\" == $c) { // pair |
|
| 302 | + ++$idx; |
|
| 303 | + $val .= $this->_parsePairAfterSlash($idx); |
|
| 304 | + continue; |
|
| 305 | + } else if ('"' == $c) { |
|
| 306 | + ++$idx; |
|
| 307 | + break; |
|
| 308 | + } |
|
| 309 | + $val .= $c; |
|
| 310 | + ++$idx; |
|
| 311 | + } |
|
| 312 | + $offset = $idx; |
|
| 313 | + return $val; |
|
| 314 | + } |
|
| 315 | 315 | |
| 316 | - /** |
|
| 317 | - * Parse 'attributeValue' of binary type. |
|
| 318 | - * |
|
| 319 | - * @param int $offset |
|
| 320 | - * @throws \UnexpectedValueException |
|
| 321 | - * @return string |
|
| 322 | - */ |
|
| 323 | - private function _parseAttrHexValue(&$offset) |
|
| 324 | - { |
|
| 325 | - $idx = $offset; |
|
| 326 | - $hexstr = $this->_regexMatch('/^(?:[0-9a-f]{2})+/i', $idx); |
|
| 327 | - if (null === $hexstr) { |
|
| 328 | - throw new \UnexpectedValueException("Invalid hexstring."); |
|
| 329 | - } |
|
| 330 | - $data = hex2bin($hexstr); |
|
| 331 | - $offset = $idx; |
|
| 332 | - return $data; |
|
| 333 | - } |
|
| 316 | + /** |
|
| 317 | + * Parse 'attributeValue' of binary type. |
|
| 318 | + * |
|
| 319 | + * @param int $offset |
|
| 320 | + * @throws \UnexpectedValueException |
|
| 321 | + * @return string |
|
| 322 | + */ |
|
| 323 | + private function _parseAttrHexValue(&$offset) |
|
| 324 | + { |
|
| 325 | + $idx = $offset; |
|
| 326 | + $hexstr = $this->_regexMatch('/^(?:[0-9a-f]{2})+/i', $idx); |
|
| 327 | + if (null === $hexstr) { |
|
| 328 | + throw new \UnexpectedValueException("Invalid hexstring."); |
|
| 329 | + } |
|
| 330 | + $data = hex2bin($hexstr); |
|
| 331 | + $offset = $idx; |
|
| 332 | + return $data; |
|
| 333 | + } |
|
| 334 | 334 | |
| 335 | - /** |
|
| 336 | - * Parse 'pair' after leading slash. |
|
| 337 | - * |
|
| 338 | - * @param int $offset |
|
| 339 | - * @throws \UnexpectedValueException |
|
| 340 | - * @return string |
|
| 341 | - */ |
|
| 342 | - private function _parsePairAfterSlash(&$offset) |
|
| 343 | - { |
|
| 344 | - $idx = $offset; |
|
| 345 | - if ($idx >= $this->_len) { |
|
| 346 | - throw new \UnexpectedValueException( |
|
| 347 | - "Unexpected end of escape sequence."); |
|
| 348 | - } |
|
| 349 | - $c = $this->_dn[$idx++]; |
|
| 350 | - // special | \ | " | SPACE |
|
| 351 | - if (false !== strpos(self::SPECIAL_CHARS . '\\" ', $c)) { |
|
| 352 | - $val = $c; |
|
| 353 | - } else { // hexpair |
|
| 354 | - if ($idx >= $this->_len) { |
|
| 355 | - throw new \UnexpectedValueException("Unexpected end of hexpair."); |
|
| 356 | - } |
|
| 357 | - $val = @hex2bin($c . $this->_dn[$idx++]); |
|
| 358 | - if (false === $val) { |
|
| 359 | - throw new \UnexpectedValueException("Invalid hexpair."); |
|
| 360 | - } |
|
| 361 | - } |
|
| 362 | - $offset = $idx; |
|
| 363 | - return $val; |
|
| 364 | - } |
|
| 335 | + /** |
|
| 336 | + * Parse 'pair' after leading slash. |
|
| 337 | + * |
|
| 338 | + * @param int $offset |
|
| 339 | + * @throws \UnexpectedValueException |
|
| 340 | + * @return string |
|
| 341 | + */ |
|
| 342 | + private function _parsePairAfterSlash(&$offset) |
|
| 343 | + { |
|
| 344 | + $idx = $offset; |
|
| 345 | + if ($idx >= $this->_len) { |
|
| 346 | + throw new \UnexpectedValueException( |
|
| 347 | + "Unexpected end of escape sequence."); |
|
| 348 | + } |
|
| 349 | + $c = $this->_dn[$idx++]; |
|
| 350 | + // special | \ | " | SPACE |
|
| 351 | + if (false !== strpos(self::SPECIAL_CHARS . '\\" ', $c)) { |
|
| 352 | + $val = $c; |
|
| 353 | + } else { // hexpair |
|
| 354 | + if ($idx >= $this->_len) { |
|
| 355 | + throw new \UnexpectedValueException("Unexpected end of hexpair."); |
|
| 356 | + } |
|
| 357 | + $val = @hex2bin($c . $this->_dn[$idx++]); |
|
| 358 | + if (false === $val) { |
|
| 359 | + throw new \UnexpectedValueException("Invalid hexpair."); |
|
| 360 | + } |
|
| 361 | + } |
|
| 362 | + $offset = $idx; |
|
| 363 | + return $val; |
|
| 364 | + } |
|
| 365 | 365 | |
| 366 | - /** |
|
| 367 | - * Match DN to pattern and extract the last capture group. |
|
| 368 | - * |
|
| 369 | - * Updates offset to fully matched pattern. |
|
| 370 | - * |
|
| 371 | - * @param string $pattern |
|
| 372 | - * @param int $offset |
|
| 373 | - * @return string|null Null if pattern doesn't match |
|
| 374 | - */ |
|
| 375 | - private function _regexMatch($pattern, &$offset) |
|
| 376 | - { |
|
| 377 | - $idx = $offset; |
|
| 378 | - if (!preg_match($pattern, substr($this->_dn, $idx), $match)) { |
|
| 379 | - return null; |
|
| 380 | - } |
|
| 381 | - $idx += strlen($match[0]); |
|
| 382 | - $offset = $idx; |
|
| 383 | - return end($match); |
|
| 384 | - } |
|
| 366 | + /** |
|
| 367 | + * Match DN to pattern and extract the last capture group. |
|
| 368 | + * |
|
| 369 | + * Updates offset to fully matched pattern. |
|
| 370 | + * |
|
| 371 | + * @param string $pattern |
|
| 372 | + * @param int $offset |
|
| 373 | + * @return string|null Null if pattern doesn't match |
|
| 374 | + */ |
|
| 375 | + private function _regexMatch($pattern, &$offset) |
|
| 376 | + { |
|
| 377 | + $idx = $offset; |
|
| 378 | + if (!preg_match($pattern, substr($this->_dn, $idx), $match)) { |
|
| 379 | + return null; |
|
| 380 | + } |
|
| 381 | + $idx += strlen($match[0]); |
|
| 382 | + $offset = $idx; |
|
| 383 | + return end($match); |
|
| 384 | + } |
|
| 385 | 385 | |
| 386 | - /** |
|
| 387 | - * Skip consecutive spaces. |
|
| 388 | - * |
|
| 389 | - * @param int $offset |
|
| 390 | - */ |
|
| 391 | - private function _skipWs(&$offset) |
|
| 392 | - { |
|
| 393 | - $idx = $offset; |
|
| 394 | - while ($idx < $this->_len) { |
|
| 395 | - if (" " != $this->_dn[$idx]) { |
|
| 396 | - break; |
|
| 397 | - } |
|
| 398 | - ++$idx; |
|
| 399 | - } |
|
| 400 | - $offset = $idx; |
|
| 401 | - } |
|
| 386 | + /** |
|
| 387 | + * Skip consecutive spaces. |
|
| 388 | + * |
|
| 389 | + * @param int $offset |
|
| 390 | + */ |
|
| 391 | + private function _skipWs(&$offset) |
|
| 392 | + { |
|
| 393 | + $idx = $offset; |
|
| 394 | + while ($idx < $this->_len) { |
|
| 395 | + if (" " != $this->_dn[$idx]) { |
|
| 396 | + break; |
|
| 397 | + } |
|
| 398 | + ++$idx; |
|
| 399 | + } |
|
| 400 | + $offset = $idx; |
|
| 401 | + } |
|
| 402 | 402 | } |
@@ -63,11 +63,11 @@ |
||
| 63 | 63 | $str = preg_replace('/^([ #])/u', '\\\\$1', $str); |
| 64 | 64 | // implementation specific special characters |
| 65 | 65 | $str = preg_replace_callback('/([\pC])/u', |
| 66 | - function ($m) { |
|
| 66 | + function($m) { |
|
| 67 | 67 | $octets = str_split(bin2hex($m[1]), 2); |
| 68 | 68 | return implode("", |
| 69 | 69 | array_map( |
| 70 | - function ($octet) { |
|
| 70 | + function($octet) { |
|
| 71 | 71 | return '\\' . strtoupper($octet); |
| 72 | 72 | }, $octets)); |
| 73 | 73 | }, $str); |
@@ -11,13 +11,13 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class CaseExactMatch extends StringPrepMatchingRule |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Constructor. |
|
| 16 | - * |
|
| 17 | - * @param int $string_type ASN.1 string type tag |
|
| 18 | - */ |
|
| 19 | - public function __construct($string_type) |
|
| 20 | - { |
|
| 21 | - parent::__construct(StringPreparer::forStringType($string_type)); |
|
| 22 | - } |
|
| 14 | + /** |
|
| 15 | + * Constructor. |
|
| 16 | + * |
|
| 17 | + * @param int $string_type ASN.1 string type tag |
|
| 18 | + */ |
|
| 19 | + public function __construct($string_type) |
|
| 20 | + { |
|
| 21 | + parent::__construct(StringPreparer::forStringType($string_type)); |
|
| 22 | + } |
|
| 23 | 23 | } |
@@ -9,12 +9,12 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class BinaryMatch extends MatchingRule |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * |
|
| 14 | - * {@inheritdoc} |
|
| 15 | - */ |
|
| 16 | - public function compare($assertion, $value) |
|
| 17 | - { |
|
| 18 | - return strcmp($assertion, $value) == 0; |
|
| 19 | - } |
|
| 12 | + /** |
|
| 13 | + * |
|
| 14 | + * {@inheritdoc} |
|
| 15 | + */ |
|
| 16 | + public function compare($assertion, $value) |
|
| 17 | + { |
|
| 18 | + return strcmp($assertion, $value) == 0; |
|
| 19 | + } |
|
| 20 | 20 | } |
@@ -9,13 +9,13 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | abstract class MatchingRule |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Compare attribute value to assertion. |
|
| 14 | - * |
|
| 15 | - * @param mixed $assertion Value to assert |
|
| 16 | - * @param mixed $value Attribute value |
|
| 17 | - * @return bool|null True if value matches. Null shall be returned if match |
|
| 18 | - * evaluates to Undefined. |
|
| 19 | - */ |
|
| 20 | - abstract public function compare($assertion, $value); |
|
| 12 | + /** |
|
| 13 | + * Compare attribute value to assertion. |
|
| 14 | + * |
|
| 15 | + * @param mixed $assertion Value to assert |
|
| 16 | + * @param mixed $value Attribute value |
|
| 17 | + * @return bool|null True if value matches. Null shall be returned if match |
|
| 18 | + * evaluates to Undefined. |
|
| 19 | + */ |
|
| 20 | + abstract public function compare($assertion, $value); |
|
| 21 | 21 | } |
@@ -11,14 +11,14 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class CaseIgnoreMatch extends StringPrepMatchingRule |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Constructor. |
|
| 16 | - * |
|
| 17 | - * @param int $string_type ASN.1 string type tag |
|
| 18 | - */ |
|
| 19 | - public function __construct($string_type) |
|
| 20 | - { |
|
| 21 | - parent::__construct( |
|
| 22 | - StringPreparer::forStringType($string_type)->withCaseFolding(true)); |
|
| 23 | - } |
|
| 14 | + /** |
|
| 15 | + * Constructor. |
|
| 16 | + * |
|
| 17 | + * @param int $string_type ASN.1 string type tag |
|
| 18 | + */ |
|
| 19 | + public function __construct($string_type) |
|
| 20 | + { |
|
| 21 | + parent::__construct( |
|
| 22 | + StringPreparer::forStringType($string_type)->withCaseFolding(true)); |
|
| 23 | + } |
|
| 24 | 24 | } |
@@ -9,31 +9,31 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | abstract class StringPrepMatchingRule extends MatchingRule |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * String preparer. |
|
| 14 | - * |
|
| 15 | - * @var StringPreparer $_prep |
|
| 16 | - */ |
|
| 17 | - protected $_prep; |
|
| 12 | + /** |
|
| 13 | + * String preparer. |
|
| 14 | + * |
|
| 15 | + * @var StringPreparer $_prep |
|
| 16 | + */ |
|
| 17 | + protected $_prep; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Constructor. |
|
| 21 | - * |
|
| 22 | - * @param StringPreparer $preparer |
|
| 23 | - */ |
|
| 24 | - public function __construct(StringPreparer $preparer) |
|
| 25 | - { |
|
| 26 | - $this->_prep = $preparer; |
|
| 27 | - } |
|
| 19 | + /** |
|
| 20 | + * Constructor. |
|
| 21 | + * |
|
| 22 | + * @param StringPreparer $preparer |
|
| 23 | + */ |
|
| 24 | + public function __construct(StringPreparer $preparer) |
|
| 25 | + { |
|
| 26 | + $this->_prep = $preparer; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * |
|
| 31 | - * {@inheritdoc} |
|
| 32 | - */ |
|
| 33 | - public function compare($assertion, $value) |
|
| 34 | - { |
|
| 35 | - $assertion = $this->_prep->prepare($assertion); |
|
| 36 | - $value = $this->_prep->prepare($value); |
|
| 37 | - return strcmp($assertion, $value) == 0; |
|
| 38 | - } |
|
| 29 | + /** |
|
| 30 | + * |
|
| 31 | + * {@inheritdoc} |
|
| 32 | + */ |
|
| 33 | + public function compare($assertion, $value) |
|
| 34 | + { |
|
| 35 | + $assertion = $this->_prep->prepare($assertion); |
|
| 36 | + $value = $this->_prep->prepare($value); |
|
| 37 | + return strcmp($assertion, $value) == 0; |
|
| 38 | + } |
|
| 39 | 39 | } |
@@ -10,34 +10,34 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class MapStep implements PrepareStep |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * Whether to apply case folding. |
|
| 15 | - * |
|
| 16 | - * @var bool $_fold |
|
| 17 | - */ |
|
| 18 | - protected $_fold; |
|
| 13 | + /** |
|
| 14 | + * Whether to apply case folding. |
|
| 15 | + * |
|
| 16 | + * @var bool $_fold |
|
| 17 | + */ |
|
| 18 | + protected $_fold; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Constructor. |
|
| 22 | - * |
|
| 23 | - * @param bool $fold_case Whether to apply case folding |
|
| 24 | - */ |
|
| 25 | - public function __construct($fold_case = false) |
|
| 26 | - { |
|
| 27 | - $this->_fold = $fold_case; |
|
| 28 | - } |
|
| 20 | + /** |
|
| 21 | + * Constructor. |
|
| 22 | + * |
|
| 23 | + * @param bool $fold_case Whether to apply case folding |
|
| 24 | + */ |
|
| 25 | + public function __construct($fold_case = false) |
|
| 26 | + { |
|
| 27 | + $this->_fold = $fold_case; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * |
|
| 32 | - * @param string $string UTF-8 encoded string |
|
| 33 | - * @return string |
|
| 34 | - */ |
|
| 35 | - public function apply($string) |
|
| 36 | - { |
|
| 37 | - // @todo Implement character mappings |
|
| 38 | - if ($this->_fold) { |
|
| 39 | - $string = mb_convert_case($string, MB_CASE_LOWER, "UTF-8"); |
|
| 40 | - } |
|
| 41 | - return $string; |
|
| 42 | - } |
|
| 30 | + /** |
|
| 31 | + * |
|
| 32 | + * @param string $string UTF-8 encoded string |
|
| 33 | + * @return string |
|
| 34 | + */ |
|
| 35 | + public function apply($string) |
|
| 36 | + { |
|
| 37 | + // @todo Implement character mappings |
|
| 38 | + if ($this->_fold) { |
|
| 39 | + $string = mb_convert_case($string, MB_CASE_LOWER, "UTF-8"); |
|
| 40 | + } |
|
| 41 | + return $string; |
|
| 42 | + } |
|
| 43 | 43 | } |
@@ -9,77 +9,77 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class StringPreparer |
| 11 | 11 | { |
| 12 | - const STEP_TRANSCODE = 1; |
|
| 13 | - const STEP_MAP = 2; |
|
| 14 | - const STEP_NORMALIZE = 3; |
|
| 15 | - const STEP_PROHIBIT = 4; |
|
| 16 | - const STEP_CHECK_BIDI = 5; |
|
| 17 | - const STEP_INSIGNIFICANT_CHARS = 6; |
|
| 12 | + const STEP_TRANSCODE = 1; |
|
| 13 | + const STEP_MAP = 2; |
|
| 14 | + const STEP_NORMALIZE = 3; |
|
| 15 | + const STEP_PROHIBIT = 4; |
|
| 16 | + const STEP_CHECK_BIDI = 5; |
|
| 17 | + const STEP_INSIGNIFICANT_CHARS = 6; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Preparation steps. |
|
| 21 | - * |
|
| 22 | - * @var PrepareStep[] $_steps |
|
| 23 | - */ |
|
| 24 | - protected $_steps; |
|
| 19 | + /** |
|
| 20 | + * Preparation steps. |
|
| 21 | + * |
|
| 22 | + * @var PrepareStep[] $_steps |
|
| 23 | + */ |
|
| 24 | + protected $_steps; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Constructor. |
|
| 28 | - * |
|
| 29 | - * @param PrepareStep[] $steps Preparation steps to apply |
|
| 30 | - */ |
|
| 31 | - protected function __construct(array $steps) |
|
| 32 | - { |
|
| 33 | - $this->_steps = $steps; |
|
| 34 | - } |
|
| 26 | + /** |
|
| 27 | + * Constructor. |
|
| 28 | + * |
|
| 29 | + * @param PrepareStep[] $steps Preparation steps to apply |
|
| 30 | + */ |
|
| 31 | + protected function __construct(array $steps) |
|
| 32 | + { |
|
| 33 | + $this->_steps = $steps; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Get default instance for given string type. |
|
| 38 | - * |
|
| 39 | - * @param int $string_type ASN.1 string type tag. |
|
| 40 | - * @return self |
|
| 41 | - */ |
|
| 42 | - public static function forStringType($string_type) |
|
| 43 | - { |
|
| 44 | - $steps = array( |
|
| 45 | - /* @formatter:off */ |
|
| 46 | - self::STEP_TRANSCODE => new TranscodeStep($string_type), |
|
| 47 | - self::STEP_MAP => new MapStep(), |
|
| 48 | - self::STEP_NORMALIZE => new NormalizeStep(), |
|
| 49 | - self::STEP_PROHIBIT => new ProhibitStep(), |
|
| 50 | - self::STEP_CHECK_BIDI => new CheckBidiStep(), |
|
| 51 | - // @todo Vary by string type |
|
| 52 | - self::STEP_INSIGNIFICANT_CHARS => |
|
| 53 | - new InsignificantNonSubstringSpaceStep() |
|
| 54 | - /* @formatter:on */ |
|
| 55 | - ); |
|
| 56 | - return new self($steps); |
|
| 57 | - } |
|
| 36 | + /** |
|
| 37 | + * Get default instance for given string type. |
|
| 38 | + * |
|
| 39 | + * @param int $string_type ASN.1 string type tag. |
|
| 40 | + * @return self |
|
| 41 | + */ |
|
| 42 | + public static function forStringType($string_type) |
|
| 43 | + { |
|
| 44 | + $steps = array( |
|
| 45 | + /* @formatter:off */ |
|
| 46 | + self::STEP_TRANSCODE => new TranscodeStep($string_type), |
|
| 47 | + self::STEP_MAP => new MapStep(), |
|
| 48 | + self::STEP_NORMALIZE => new NormalizeStep(), |
|
| 49 | + self::STEP_PROHIBIT => new ProhibitStep(), |
|
| 50 | + self::STEP_CHECK_BIDI => new CheckBidiStep(), |
|
| 51 | + // @todo Vary by string type |
|
| 52 | + self::STEP_INSIGNIFICANT_CHARS => |
|
| 53 | + new InsignificantNonSubstringSpaceStep() |
|
| 54 | + /* @formatter:on */ |
|
| 55 | + ); |
|
| 56 | + return new self($steps); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Get self with case folding set. |
|
| 61 | - * |
|
| 62 | - * @param bool $fold True to apply case folding |
|
| 63 | - * @return self |
|
| 64 | - */ |
|
| 65 | - public function withCaseFolding($fold) |
|
| 66 | - { |
|
| 67 | - $obj = clone $this; |
|
| 68 | - $obj->_steps[self::STEP_MAP] = new MapStep($fold); |
|
| 69 | - return $obj; |
|
| 70 | - } |
|
| 59 | + /** |
|
| 60 | + * Get self with case folding set. |
|
| 61 | + * |
|
| 62 | + * @param bool $fold True to apply case folding |
|
| 63 | + * @return self |
|
| 64 | + */ |
|
| 65 | + public function withCaseFolding($fold) |
|
| 66 | + { |
|
| 67 | + $obj = clone $this; |
|
| 68 | + $obj->_steps[self::STEP_MAP] = new MapStep($fold); |
|
| 69 | + return $obj; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Prepare string. |
|
| 74 | - * |
|
| 75 | - * @param string $string |
|
| 76 | - * @return string |
|
| 77 | - */ |
|
| 78 | - public function prepare($string) |
|
| 79 | - { |
|
| 80 | - foreach ($this->_steps as $step) { |
|
| 81 | - $string = $step->apply($string); |
|
| 82 | - } |
|
| 83 | - return $string; |
|
| 84 | - } |
|
| 72 | + /** |
|
| 73 | + * Prepare string. |
|
| 74 | + * |
|
| 75 | + * @param string $string |
|
| 76 | + * @return string |
|
| 77 | + */ |
|
| 78 | + public function prepare($string) |
|
| 79 | + { |
|
| 80 | + foreach ($this->_steps as $step) { |
|
| 81 | + $string = $step->apply($string); |
|
| 82 | + } |
|
| 83 | + return $string; |
|
| 84 | + } |
|
| 85 | 85 | } |
@@ -12,22 +12,22 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class InsignificantNonSubstringSpaceStep implements PrepareStep |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * |
|
| 17 | - * @param string $string UTF-8 encoded string |
|
| 18 | - * @return string |
|
| 19 | - */ |
|
| 20 | - public function apply($string) |
|
| 21 | - { |
|
| 22 | - // if value contains no non-space characters |
|
| 23 | - if (preg_match('/^\p{Zs}*$/u', $string)) { |
|
| 24 | - return " "; |
|
| 25 | - } |
|
| 26 | - // trim leading and trailing spaces |
|
| 27 | - $string = preg_replace('/^\p{Zs}+/u', '', $string); |
|
| 28 | - $string = preg_replace('/\p{Zs}+$/u', '', $string); |
|
| 29 | - // convert inner space sequences to two U+0020 characters |
|
| 30 | - $string = preg_replace('/\p{Zs}+/u', " ", $string); |
|
| 31 | - return " $string "; |
|
| 32 | - } |
|
| 15 | + /** |
|
| 16 | + * |
|
| 17 | + * @param string $string UTF-8 encoded string |
|
| 18 | + * @return string |
|
| 19 | + */ |
|
| 20 | + public function apply($string) |
|
| 21 | + { |
|
| 22 | + // if value contains no non-space characters |
|
| 23 | + if (preg_match('/^\p{Zs}*$/u', $string)) { |
|
| 24 | + return " "; |
|
| 25 | + } |
|
| 26 | + // trim leading and trailing spaces |
|
| 27 | + $string = preg_replace('/^\p{Zs}+/u', '', $string); |
|
| 28 | + $string = preg_replace('/\p{Zs}+$/u', '', $string); |
|
| 29 | + // convert inner space sequences to two U+0020 characters |
|
| 30 | + $string = preg_replace('/\p{Zs}+/u', " ", $string); |
|
| 31 | + return " $string "; |
|
| 32 | + } |
|
| 33 | 33 | } |