| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace LeKoala\Tin\Algo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use LeKoala\Tin\Util\StringUtil; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * United Kingdom | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * 10 numerals = UTR: Unique Taxpayer Reference | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * 9 chars=  NINO: National Insurance Number | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class UKAlgorithm extends TINAlgorithm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     const LENGTH_1 = 10; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     const PATTERN_1 = "\\d{10}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     const LENGTH_2 = 9; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     const PATTERN_2 = "[a-ceg-hj-pr-tw-zA-CEG-HJ-PR-TW-Z][a-ceg-hj-npr-tw-zA-CEG-HJ-NPR-TW-Z]\\d{6}[abcdABCD ]"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function validate(string $tin) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $normalizedTIN = str_replace("/", "", $tin); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         if (strlen($tin) == 8) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             $normalizedTIN .= " "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         if (!$this->isFollowLength1($normalizedTIN) && !$this->isFollowLength2($normalizedTIN)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             return StatusCode::INVALID_LENGTH; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         if (($this->isFollowLength1($normalizedTIN) && !$this->isFollowPattern1($normalizedTIN)) || ($this->isFollowLength2($normalizedTIN) && !$this->isFollowPattern2($normalizedTIN))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             return StatusCode::INVALID_PATTERN; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         return StatusCode::VALID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function isFollowLength1(string $tin) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return StringUtil::isFollowLength($tin, self::LENGTH_1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     public function isFollowPattern1(string $tin) | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         return StringUtil::isFollowPattern($tin, self::PATTERN_1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function isFollowLength2(string $tin) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return StringUtil::isFollowLength($tin, self::LENGTH_2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function isFollowPattern2(string $tin) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return StringUtil::isFollowPattern($tin, self::PATTERN_2) && $this->isFollowStructureSubRule2($tin); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function isFollowStructureSubRule2(string $tin) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $c1c2 = strtoupper(StringUtil::substring($tin, 0, 2)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         return !("GB" == $c1c2) && !("NK" == $c1c2) && !("TN" == $c1c2) && !("ZZ" == $c1c2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |  |