| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Sfneal\Helpers\Strings\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | trait StaticMethods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * Implode values that are not null. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @param $glue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @param array $pieces | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @return string|null | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 15 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     public static function implodeFiltered($glue, array $pieces): ?string | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |         if (! empty($pieces) && count($pieces) > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |             return implode($glue, array_filter($pieces, function ($attr) { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |                 return isset($attr); | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |             })); | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * Concatenate directory and file path arrays. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param mixed ...$paths | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public static function joinPaths(...$paths): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         if (count($paths) === count($paths, COUNT_RECURSIVE)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             $paths = array_values($paths); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         return implode(DIRECTORY_SEPARATOR, $paths); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * Retrieve a website domain without prefixes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public static function extractWebsiteDomain(string $url): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return isset($url) ? str_replace('www.', '', parse_url($url)['host']) : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * Only retrieve an integer value if the $value is greater than zero. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Uses $return as the $return value instead of $value if not null. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * Return original $value or $return if it is greater than zero. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * Return $substitute string if it is less. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param string $substitute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param mixed $return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public static function zeroReplace($value, $substitute = '-', $return = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         // Return $substitute if the $value is not greater than the 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         return $value > 0 ? ($return ?? (int) $value) : $substitute; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * Pretty implode an array by using a different glue for the last piece. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * Examples: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      *  - implodePretty([1, 2, 3]) --> '1, 2 & 3' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      *  - implodePretty([A, B, D]) --> 'A, B & D' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param array $pieces | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param string $glue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param string $and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public static function implodePretty(array $pieces, string $glue = ',', string $and = '&'): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         // Pop off the last item so that it's | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         // imploded with the $and char | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $last = array_pop($pieces); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         // Do nothing and return $last if there | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         // are no remaining $pieces | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if (! count($pieces)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             return $last; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         // Implode all bust the last $piece | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         // using the $glue char | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $start = implode(whitespacePadBack(trim($glue)), $pieces); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         // Implode comma separated $start with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         // & separated $last | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         return implode(whitespacePad(trim($and)), [$start, $last]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  |