|
| 1 | +<?php namespace Arcanedev\Gravatar; |
| 2 | + |
| 3 | +use Arcanedev\Gravatar\Exceptions\InvalidProfileFormatException; |
| 4 | + |
| 5 | +/** |
| 6 | + * Class Profile |
| 7 | + * |
| 8 | + * @package Arcanedev\Gravatar |
| 9 | + * @author ARCANEDEV <arcanedev.maroc@gmail.com> |
| 10 | + */ |
| 11 | +class Profile |
| 12 | +{ |
| 13 | + /* ----------------------------------------------------------------- |
| 14 | + | Traits |
| 15 | + | ----------------------------------------------------------------- |
| 16 | + */ |
| 17 | + |
| 18 | + use Concerns\HashEmail; |
| 19 | + |
| 20 | + /* ----------------------------------------------------------------- |
| 21 | + | Constants |
| 22 | + | ----------------------------------------------------------------- |
| 23 | + */ |
| 24 | + |
| 25 | + const BASE_URL = 'http://www.gravatar.com/'; |
| 26 | + const SECURE_URL = 'https://www.gravatar.com/'; |
| 27 | + |
| 28 | + /* ----------------------------------------------------------------- |
| 29 | + | Properties |
| 30 | + | ----------------------------------------------------------------- |
| 31 | + */ |
| 32 | + |
| 33 | + /** |
| 34 | + * Profile's format. |
| 35 | + * |
| 36 | + * @var string |
| 37 | + */ |
| 38 | + protected $format; |
| 39 | + |
| 40 | + /** |
| 41 | + * Supported format. |
| 42 | + * |
| 43 | + * @var array |
| 44 | + */ |
| 45 | + protected static $supportedFormat = ['json', 'xml', 'php', 'vcf', 'qr']; |
| 46 | + |
| 47 | + /* ----------------------------------------------------------------- |
| 48 | + | Getters & Setters |
| 49 | + | ----------------------------------------------------------------- |
| 50 | + */ |
| 51 | + |
| 52 | + /** |
| 53 | + * Get the profile's format. |
| 54 | + * |
| 55 | + * @return string|null |
| 56 | + */ |
| 57 | + public function getFormat() |
| 58 | + { |
| 59 | + return $this->format; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Set the profile's format. |
| 64 | + * |
| 65 | + * @param string $format |
| 66 | + * |
| 67 | + * @return \Arcanedev\Gravatar\Profile |
| 68 | + */ |
| 69 | + public function setFormat($format = null) |
| 70 | + { |
| 71 | + if ( ! is_null($format)) { |
| 72 | + self::checkFormat($format); |
| 73 | + $this->format = $format; |
| 74 | + } |
| 75 | + |
| 76 | + return $this; |
| 77 | + } |
| 78 | + |
| 79 | + /* ----------------------------------------------------------------- |
| 80 | + | Main Methods |
| 81 | + | ----------------------------------------------------------------- |
| 82 | + */ |
| 83 | + |
| 84 | + /** |
| 85 | + * Build the profile URL based on the provided email address. |
| 86 | + * |
| 87 | + * @param string $email |
| 88 | + * @param array $params |
| 89 | + * @param bool $secure |
| 90 | + * |
| 91 | + * @return string |
| 92 | + */ |
| 93 | + public function getUrl($email = null, array $params = [], $secure = true) |
| 94 | + { |
| 95 | + $url = $secure ? static::SECURE_URL : static::BASE_URL; |
| 96 | + $url .= is_null($email) |
| 97 | + ? str_repeat('0', 32) |
| 98 | + : static::hashEmail($email); |
| 99 | + |
| 100 | + if ($this->hasFormat()) |
| 101 | + $url .= ".{$this->getFormat()}"; |
| 102 | + |
| 103 | + if ( ! empty($params)) |
| 104 | + $url .= '?'.http_build_query($params); |
| 105 | + |
| 106 | + return $url; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Get the profile data. |
| 111 | + * |
| 112 | + * @param string $email |
| 113 | + * @param mixed|null $default |
| 114 | + * |
| 115 | + * @return array|mixed |
| 116 | + */ |
| 117 | + public function get($email, $default = null) |
| 118 | + { |
| 119 | + $this->setFormat('php'); |
| 120 | + |
| 121 | + $data = unserialize( |
| 122 | + file_get_contents($this->getUrl($email)) |
| 123 | + ); |
| 124 | + |
| 125 | + return (is_array($data) && isset($data['entry'])) |
| 126 | + ? $data |
| 127 | + : $default; |
| 128 | + } |
| 129 | + |
| 130 | + /* ----------------------------------------------------------------- |
| 131 | + | Check Methods |
| 132 | + | ----------------------------------------------------------------- |
| 133 | + */ |
| 134 | + |
| 135 | + /** |
| 136 | + * Check if the format is not null. |
| 137 | + * |
| 138 | + * @return bool |
| 139 | + */ |
| 140 | + public function hasFormat() |
| 141 | + { |
| 142 | + return ! is_null($this->format); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Check the format. |
| 147 | + * |
| 148 | + * @param string $format |
| 149 | + */ |
| 150 | + private static function checkFormat(&$format) |
| 151 | + { |
| 152 | + $format = strtolower($format); |
| 153 | + |
| 154 | + if ( ! in_array($format, static::$supportedFormat)) { |
| 155 | + throw InvalidProfileFormatException::make($format, static::$supportedFormat); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments