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