| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Jtotty\Steps; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Port\Steps\Step; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class CheckPupilNamesStep implements Step | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     public function process($item, callable $next) | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         // Forename empty && Surname contains two words | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         if ($item['Forename'] === '' && str_word_count($item['Surname']) > 1) { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |             $name             = preg_split('/\s+/', $item['Surname']); | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |             $item['Forename'] = $name[0]; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |             $item['Surname']  = end($name); // In-case of middle names | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         // Surname empty && forename contains two words | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         if ($item['Surname'] === '' && str_word_count($item['Forename']) > 1) { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |             $name             = preg_split('/\s+/', $item['Forename']); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |             $item['Forename'] = $name[0]; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |             $item['Surname']  = end($name); // In-case of middle names | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         // Remove any whitespace from names | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         // Only first letter of name uppercase (inc. double barrelled names) | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $item['Forename'] = $this->formatName($item['Forename']); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $item['Surname']  = $this->formatName($item['Surname']); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         return $next($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function formatName($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         // Remove any whitespace from beginning and end. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $name = trim($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         // Format double barrelled names. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if (strpos($name, '-') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $words = explode('-', $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             foreach ($words as $index => $word) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 $words[$index] = ucfirst(strtolower($word)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             return implode('-', $words); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         // Standard first letter uppercase for each word | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return ucwords(strtolower($name)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |  |