| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Mguinea\Robots; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Mguinea\Robots\Contracts\Robots as RobotsContract; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  | class Robots implements RobotsContract | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |     const ALLOW = 'Allow'; | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |     const DISALLOW = 'Disallow'; | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     const HOST = 'Host'; | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     const SITEMAP = 'Sitemap'; | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     const USER_AGENT = 'User-agent'; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     const COMMENT = '#'; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     const CRAWL_DELAY = 'crawl-delay'; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * The rows of for the robots. | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     protected $rows = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * Array with configuration set in constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     protected $parameters = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * Robots constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @param array $parameters | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     public function __construct(array $parameters = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $this->parameters = $parameters; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $this->composeFromParameters(); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * Create Robots object from array. | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     private function composeFromParameters(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         foreach ($this->parameters as $key => $values) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             if ($key === 'allows') { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |                 foreach ($values as $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                     $this->addAllow($value); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             } elseif ($key === 'disallows') { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |                 foreach ($values as $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                     $this->addDisallow($value); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             } elseif ($key === 'hosts') { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                 foreach ($values as $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                     $this->addHost($value); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             } elseif ($key === 'sitemaps') { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                 foreach ($values as $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                     $this->addSitemap($value); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             } elseif ($key === 'userAgents') { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                 foreach ($values as $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                     $this->addUserAgent($value); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             } elseif ($key === 'crawlDelay') { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |                 foreach ($values as $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |                     $this->addCrawlDelay($value); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * Add a allow rule to the robots. | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * @param string|array $directories | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * @return RobotsContract; | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     public function addAllow($directories): RobotsContract | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         $this->addRuleLine($directories, self::ALLOW); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      * Add a comment to the robots. | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * @param string $comment | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      * @return RobotsContract; | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     public function addComment(string $comment): RobotsContract | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         $this->addLine(self::COMMENT." $comment"); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      * Add a disallow rule to the robots. | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      * @param string|array $directories | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      * @return RobotsContract; | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     public function addDisallow($directories): RobotsContract | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |         $this->addRuleLine($directories, self::DISALLOW); | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |      * Add a Host to the robots. | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |      * @param string|array $host | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |      * @return RobotsContract; | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |     public function addHost($hosts): RobotsContract | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |         $this->addRuleLine($hosts, self::HOST); | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |      * Add a row to the robots. | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |      * @param string $row | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |     protected function addLine($row) | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |         $this->rows[] = (string) $row; | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |      * Add multiple rows to the robots. | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |      * @param string|array $rows | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |     protected function addRows($rows) | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |         foreach ((array) $rows as $row) { | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |             $this->addLine($row); | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |      * Add a rule to the robots. | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |      * @param string|array $directories | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |      * @param string       $rule | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |     protected function addRuleLine($directories, $rule) | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |         foreach ((array) $directories as $directory) { | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |             $this->addLine("$rule: $directory"); | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  |      * Add a Sitemap to the robots. | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |      * @param string|array $sitemap | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |      * @return RobotsContract; | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |     public function addSitemap($sitemaps): RobotsContract | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |         $this->addRuleLine($sitemaps, self::SITEMAP); | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |      * Add a spacer to the robots. | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  |      * @return RobotsContract; | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  |     public function addSpacer(): RobotsContract | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |         $this->addLine(''); | 
            
                                                                        
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 184 |  |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 185 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 187 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 188 |  |  |      * Add a User-agent to the robots. | 
            
                                                                        
                            
            
                                    
            
            
                | 189 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 190 |  |  |      * @param string|array $userAgent | 
            
                                                                        
                            
            
                                    
            
            
                | 191 |  |  |      * @return RobotsContract; | 
            
                                                                        
                            
            
                                    
            
            
                | 192 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 193 |  |  |     public function addUserAgent($userAgents): RobotsContract | 
            
                                                                        
                            
            
                                    
            
            
                | 194 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 195 |  |  |         $this->addRuleLine($userAgents, self::USER_AGENT); | 
            
                                                                        
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 197 |  |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 198 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 200 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 201 |  |  |      * Add a allow rule to the robots. | 
            
                                                                        
                            
            
                                    
            
            
                | 202 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 203 |  |  |      * @param string|array $time | 
            
                                                                        
                            
            
                                    
            
            
                | 204 |  |  |      * @return RobotsContract; | 
            
                                                                        
                            
            
                                    
            
            
                | 205 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |     public function addCrawlDelay($time): RobotsContract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |         $this->addRuleLine($time, self::CRAWL_DELAY); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 210 |  |  |         return $this; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 211 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 212 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 213 |  |  |     /** | 
            
                                                                        
                                                                
            
                                    
            
            
                | 214 |  |  |      * Generate the robots data. | 
            
                                                                        
                                                                
            
                                    
            
            
                | 215 |  |  |      * | 
            
                                                                        
                                                                
            
                                    
            
            
                | 216 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 217 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |     public function generate(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 220 |  |  |         return implode(PHP_EOL, $this->rows); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 221 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 222 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 223 |  |  |     /** | 
            
                                                                        
                                                                
            
                                    
            
            
                | 224 |  |  |      * Reset the rows. | 
            
                                                                        
                            
            
                                    
            
            
                | 225 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 226 |  |  |      * @return RobotsContract; | 
            
                                                                        
                            
            
                                    
            
            
                | 227 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |     public function reset(): RobotsContract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |         $this->rows = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 232 |  |  |         return $this; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 233 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 234 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 235 |  |  |  |