| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Thinktomorrow\Locale\Parsers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Thinktomorrow\Locale\Locale; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class UrlParser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @var Locale | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private $locale; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $parsed; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $schemeless = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 96 |  |     public function __construct(Locale $locale) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 96 |  |         $this->locale = $locale; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 96 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 90 |  |     public function set($url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 90 |  |         $this->parsed = parse_url($url); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 90 |  |         if(false === $this->parsed) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 57 |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 3 |  |             throw new \InvalidArgumentException('Failed to parse url. Invalid url ['.$url.'] passed as parameter.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         // If a schemeless url is passed, parse_url will ignore this and strip the first tags | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         // so we keep a reminder to explicitly reassemble the 'anonymous scheme' manually | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 87 |  |         $this->schemeless = (0 === strpos($url,'//') && isset($this->parsed['host'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 87 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 87 |  |     public function get() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 87 |  |         $this->assertUrlExists(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 87 |  |         return $this->reassemble($this->parsed); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * Place locale segment in front of url path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * e.g. /foo/bar is transformed into /en/foo/bar | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param null $locale | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 87 |  |     public function localize($locale = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 87 |  |         $this->localizePath($locale); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 84 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 87 |  |     private function localizePath($locale = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 87 |  |         $this->assertUrlExists(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 84 |  |         $this->parsed['path'] = str_replace('//','/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 84 |  |             '/'.$this->locale->getSlug($locale).$this->delocalizePath() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 53 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 84 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 84 |  |     private function delocalizePath() | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 84 |  |         $this->assertUrlExists(); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 84 |  |         if (!isset($this->parsed['path'])) return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 84 |  |         $path_segments = explode('/', trim($this->parsed['path'], '/')); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 84 |  |         if (count($path_segments) < 1) return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 84 |  |         if ($path_segments[0] == $this->locale->getSlug($path_segments[0]) || $this->locale->isHidden($path_segments[0])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 30 |  |             unset($path_segments[0]); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 19 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 84 |  |         return '/' . implode('/', $path_segments); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * Construct a full url with the parsed url elements | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * resulted from a parse_url() function call | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @param array $parsed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 87 |  |     private function reassemble(array $parsed) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 87 |  |             ((isset($parsed['scheme'])) ? $parsed['scheme'] . '://' : ($this->schemeless ? '//' : '')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 87 |  |             .((isset($parsed['user'])) ? $parsed['user'] . ((isset($parsed['pass'])) ? ':' . $parsed['pass'] : '') .'@' : '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 87 |  |             .((isset($parsed['host'])) ? $parsed['host'] : '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 87 |  |             .((isset($parsed['port'])) ? ':' . $parsed['port'] : '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 87 |  |             .((isset($parsed['path'])) ? $parsed['path'] : '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 87 |  |             .((isset($parsed['query'])) ? '?' . $parsed['query'] : '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 87 |  |             .((isset($parsed['fragment'])) ? '#' . $parsed['fragment'] : ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 90 |  |     private function assertUrlExists() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 90 |  |         if (!$this->parsed) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 3 |  |             throw new \LogicException('Url is required. Please run UrlParser::set($url) first.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 117 |  |  | } | 
            
                        
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.