lib/3rdParty/composer/spomky-labs/pki-framework/src/X501/StringPrep/InsignificantNonSubstringSpaceStep.php
raw symbols SCIP| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace SpomkyLabs\Pki\X501\StringPrep; |
| 6 | |
| 7 | /** |
| 8 | * Implements 'Insignificant Space Handling' step of the Internationalized String Preparation as specified by RFC 4518. |
| 9 | * |
| 10 | * This variant handles input strings that are non-substring assertion values. |
| 11 | * |
| 12 | * @see https://tools.ietf.org/html/rfc4518#section-2.6.1 |
| 13 | */ |
| 14 | final class InsignificantNonSubstringSpaceStep implements PrepareStep |
| 15 | { |
| 16 | /** |
| 17 | * @param string $string UTF-8 encoded string |
| 18 | */ |
| 19 | public function apply(string $string): string |
| 20 | { |
| 21 | // if value contains no non-space characters |
| 22 | if (preg_match('/^\p{Zs}*$/u', $string) === 1) { |
| 23 | return ' '; |
| 24 | } |
| 25 | // trim leading and trailing spaces |
| 26 | $string = preg_replace('/^\p{Zs}+/u', '', $string); |
| 27 | $string = preg_replace('/\p{Zs}+$/u', '', (string) $string); |
| 28 | // convert inner space sequences to two U+0020 characters |
| 29 | $string = preg_replace('/\p{Zs}+/u', ' ', (string) $string); |
| 30 | return " {$string} "; |
| 31 | } |
| 32 | } |