| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace GilDrd\NameGenerator\Tools; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use GilDrd\NameGenerator\Exceptions\IntegrityException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class IntegrityChecker | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @throws IntegrityException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     public static function check(Parameter $parameter, string $name): void | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |         self::checkMinLength($parameter, $name); | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |         self::checkMaxLength($parameter, $name); | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         self::checkTripleLetters($parameter, $name); | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         self::checkNoVowels($parameter, $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @throws IntegrityException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private static function checkMinLength(Parameter $parameter, string $name): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         if (strlen($name) < $parameter->getMinLength()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             throw new IntegrityException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                 sprintf('Generated name \'%s\' is shorter than expected (%s for %s)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                     $name, strlen($name), $parameter->getMinLength() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @throws IntegrityException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     private static function checkMaxLength(Parameter $parameter, string $name): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         if (strlen($name) > $parameter->getMaxLength()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             throw new IntegrityException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 sprintf('Generated name \'%s\' is longer than expected (%s for %s)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                     $name, strlen($name), $parameter->getMaxLength() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @throws IntegrityException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     private static function checkTripleLetters(Parameter $parameter, string $name): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         if ($parameter->getLetterInTriple()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $maxCheck = strlen($name) - 2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         for ($i = 0; $i < strlen($name); $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             if ($i === $maxCheck) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             if (strtolower($name[$i]) === strtolower($name[$i+1]) && strtolower($name[$i]) === strtolower($name[$i+2])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 throw new IntegrityException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     sprintf('Letter \'%s\' can\'t be three times in a row in name %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                         $name[$i], $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @throws IntegrityException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     private static function checkNoVowels(Parameter $parameter, string $name): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         if ($parameter->getNoVowelsInName()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         if (!preg_match('/[aeiouy]+/', $name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             throw new IntegrityException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                     sprintf('There are no vowels in name %s', $name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 89 |  |  | } |