| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types = 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Sop\X501\StringPrep; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Implement Internationalized String Preparation as specified by RFC 4518. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @see https://tools.ietf.org/html/rfc4518 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | class StringPreparer | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     const STEP_TRANSCODE = 1; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     const STEP_MAP = 2; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     const STEP_NORMALIZE = 3; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     const STEP_PROHIBIT = 4; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     const STEP_CHECK_BIDI = 5; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     const STEP_INSIGNIFICANT_CHARS = 6; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * Preparation steps. | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @var PrepareStep[] | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     protected $_steps; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * Constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @param PrepareStep[] $steps Preparation steps to apply | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 35 |  |     protected function __construct(array $steps) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 | 35 |  |         $this->_steps = $steps; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 35 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * Get default instance for given string type. | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * @param int $string_type ASN.1 string type tag. | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @return self | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 35 |  |     public static function forStringType(int $string_type): self | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $steps = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 35 |  |             self::STEP_TRANSCODE => new TranscodeStep($string_type), | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 35 |  |             self::STEP_MAP => new MapStep(), | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 35 |  |             self::STEP_NORMALIZE => new NormalizeStep(), | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 35 |  |             self::STEP_PROHIBIT => new ProhibitStep(), | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 35 |  |             self::STEP_CHECK_BIDI => new CheckBidiStep(), | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             // @todo Vary by string type | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 35 |  |             self::STEP_INSIGNIFICANT_CHARS => new InsignificantNonSubstringSpaceStep(), | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 35 |  |         return new self($steps); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * Get self with case folding set. | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * @param bool $fold True to apply case folding | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * @return self | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 27 |  |     public function withCaseFolding(bool $fold): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 27 |  |         $obj = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 27 |  |         $obj->_steps[self::STEP_MAP] = new MapStep($fold); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 | 27 |  |         return $obj; | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      * Prepare string. | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      * @param string $string | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 35 |  |     public function prepare(string $string): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 35 |  |         foreach ($this->_steps as $step) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 35 |  |             $string = $step->apply($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 | 35 |  |         return $string; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  |