| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace LeKoala\Tin\Algo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use LeKoala\Tin\Util\StringUtil; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Cyprus | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class CYAlgorithm extends TINAlgorithm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     const LENGTH = 9; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     const PATTERN = "[0,9]\\d{7}[A-Z]"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public function validate(string $tin) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         if (!$this->isFollowLength($tin)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             return StatusCode::INVALID_LENGTH; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         if (!$this->isFollowPattern($tin)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             return StatusCode::INVALID_PATTERN; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         if (!$this->isFollowRules($tin)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             return StatusCode::INVALID_SYNTAX; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         return StatusCode::VALID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function isFollowLength(string $tin) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         return StringUtil::isFollowLength($tin, self::LENGTH); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function isFollowPattern(string $tin) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return StringUtil::isFollowPattern($tin, self::PATTERN); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     public function isFollowRules(string $tin) | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $c1 = StringUtil::digitAt($tin, 0); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $c2 = StringUtil::digitAt($tin, 1); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $c3 = StringUtil::digitAt($tin, 2); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $c4 = StringUtil::digitAt($tin, 3); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $c5 = StringUtil::digitAt($tin, 4); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $c6 = StringUtil::digitAt($tin, 5); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $c7 = StringUtil::digitAt($tin, 6); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $c8 = StringUtil::digitAt($tin, 7); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $c9 = ord($tin[8]); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $evenPositionNumbersSum = $c2 + $c4 + $c6 + $c8; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         $recodedSum = $this->recodeValue($c1) + $this->recodeValue($c3) + $this->recodeValue($c5) + $this->recodeValue($c7); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $remainderBy26 = ($evenPositionNumbersSum + $recodedSum) % 26; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         return $remainderBy26 + 65 == $c9; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param integer $x | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @return integer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function recodeValue($x) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         switch ($x) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             case 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 return 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             case 1: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             case 2: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 return 5; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             case 3: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 return 7; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             case 4: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 return 9; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             case 5: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 return 13; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             case 6: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 return 15; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             case 7: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 return 17; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             case 8: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 return 19; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             case 9: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 return 21; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 return -1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  |