| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Gravatar; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Gravatar URL Builder. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author Márk Sági-Kazár <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  | final class UrlBuilder | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |      * Gravatar endpoints. | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     const HTTP_ENDPOINT = 'http://www.gravatar.com'; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     const HTTPS_ENDPOINT = 'https://secure.gravatar.com'; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     protected $defaults = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * Whether to use HTTPS endpoint. | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * @var bool | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     protected $secure; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @param array $defaults | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @param bool  $secure | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 16 |  |     public function __construct(array $defaults = [], $secure = true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 16 |  |         $this->defaults = array_filter($defaults); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 16 |  |         $this->secure = (bool) $secure; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 | 16 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * Returns an Avatar URL. | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @param string    $email | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @param array     $options | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * @param bool|null $secure | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 5 |  |     public function avatar($email, array $options = [], $secure = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 5 |  |         $url = 'avatar/'.$this->createEmailHash($email); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 4 |  |         $options = array_merge($this->defaults, array_filter($options)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 4 |  |         if (!empty($options)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |             $url .= '?'.http_build_query($options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 4 |  |         return $this->buildUrl($url, $secure); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * Returns a profile URL. | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * @param string    $email | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * @param bool|null $secure | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 10 |  |     public function profile($email, $secure = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 10 |  |         return $this->buildUrl($this->createEmailHash($email), $secure); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * Returns a vCard URL. | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * @param string    $email | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * @param bool|null $secure | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 3 |  |     public function vcard($email, $secure = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 3 |  |         return $this->profile($email, $secure).'.vcf'; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * Returns a QR Code URL. | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      * @param string    $email | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * @param bool|null $secure | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 3 |  |     public function qrCode($email, $secure = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 3 |  |         return $this->profile($email, $secure).'.qr'; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      * Creates a hash from an email address. | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      * @param string $email | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 15 |  |     private function createEmailHash($email) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 15 |  |         if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 4 |  |             throw new \InvalidArgumentException('Invalid email address'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 11 |  |         return md5(strtolower(trim($email))); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |      * Builds the URL based on the given parameters. | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |      * @param string    $resource | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |      * @param bool|null $secure | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 11 |  |     private function buildUrl($resource, $secure = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 11 |  |         $secure = isset($secure) ? (bool) $secure : $this->secure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 11 |  |         $endpoint = $secure ? self::HTTPS_ENDPOINT : self::HTTP_ENDPOINT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 11 |  |         return sprintf('%s/%s', $endpoint, $resource); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 132 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |  |