Skip to content

Commit 2d9b547

Browse files
Merge pull request #12 from ARCANEDEV/feature-profiles
Adding Gravatar profiles
2 parents 7e98cc7 + d2c05b4 commit 2d9b547

File tree

9 files changed

+357
-21
lines changed

9 files changed

+357
-21
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
language: php
22

3-
sudo: false
4-
53
php:
64
- 7.1.3
75
- 7.1

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
"type": "library",
1717
"license": "MIT",
1818
"require": {
19-
"php" : ">=7.1.3",
20-
"arcanedev/support": "~4.4.0",
21-
"arcanedev/php-html": "^1.0"
19+
"php" : ">=7.1.3",
20+
"ext-curl": "*",
21+
"arcanedev/support": "~4.4.0",
22+
"arcanedev/php-html": "~1.0"
2223
},
2324
"require-dev": {
24-
"phpunit/phpunit": "~7.0",
25+
"orchestra/testbench": "~3.7.0",
2526
"phpunit/phpcov": "~5.0",
26-
"orchestra/testbench": "~3.7.0"
27+
"phpunit/phpunit": "~7.0"
2728
},
2829
"autoload": {
2930
"psr-4": {

src/Concerns/HashEmail.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php namespace Arcanedev\Gravatar\Concerns;
2+
3+
/**
4+
* Trait HashEmail
5+
*
6+
* @package Arcanedev\Gravatar\Concerns
7+
* @author ARCANEDEV <arcanedev.maroc@gmail.com>
8+
*/
9+
trait HashEmail
10+
{
11+
/* -----------------------------------------------------------------
12+
| Main Methods
13+
| -----------------------------------------------------------------
14+
*/
15+
16+
/**
17+
* Get a hashed email.
18+
*
19+
* @param string $email
20+
*
21+
* @return string
22+
*/
23+
public static function hashEmail($email)
24+
{
25+
return hash('md5', strtolower(trim($email)));
26+
}
27+
}

src/Contracts/Gravatar.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ public function get($email, $hash = true);
112112
*/
113113
public function image($email, $alt = null, array $attributes = [], $rating = null);
114114

115+
/**
116+
* Get profile's data.
117+
*
118+
* @param string $email
119+
* @param mixed|null $default
120+
*
121+
* @return array|mixed
122+
*/
123+
public function profile($email, $default = null);
124+
115125
/**
116126
* Enable the use of the secure protocol for image URLs.
117127
*
@@ -142,5 +152,5 @@ public function exists($email);
142152
*
143153
* @return string
144154
*/
145-
public function hashEmail($email);
155+
public static function hashEmail($email);
146156
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php namespace Arcanedev\Gravatar\Exceptions;
2+
3+
/**
4+
* Class InvalidProfileFormatException
5+
*
6+
* @package Arcanedev\Gravatar\Exceptions
7+
* @author ARCANEDEV <arcanedev.maroc@gmail.com>
8+
*/
9+
class InvalidProfileFormatException extends \InvalidArgumentException
10+
{
11+
/**
12+
* Make a new exception.
13+
*
14+
* @param string $format
15+
* @param array $supportedFormat
16+
*
17+
* @return \Arcanedev\Gravatar\Exceptions\InvalidProfileFormatException
18+
*/
19+
public static function make($format, array $supportedFormat)
20+
{
21+
return new static(
22+
sprintf(
23+
'The format [%s] is invalid, the supported formats are: %s',
24+
$format,
25+
implode(', ', $supportedFormat)
26+
)
27+
);
28+
}
29+
}

src/Gravatar.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
*/
1313
class Gravatar implements Contracts\Gravatar
1414
{
15+
/* -----------------------------------------------------------------
16+
| Traits
17+
| -----------------------------------------------------------------
18+
*/
19+
20+
use Concerns\HashEmail;
21+
1522
/* -----------------------------------------------------------------
1623
| Constants
1724
| -----------------------------------------------------------------
@@ -250,7 +257,7 @@ public function get($email, $hash = true)
250257
$url = $this->isSecured() ? static::SECURE_URL : static::BASE_URL;
251258
$url .= empty($email)
252259
? str_repeat('0', 32)
253-
: ($hash ? $this->hashEmail($email) : $email);
260+
: ($hash ? static::hashEmail($email) : $email);
254261

255262
$params = $this->getParams($email);
256263

@@ -283,6 +290,19 @@ public function image($email, $alt = null, array $attributes = [], $rating = nul
283290
->attributes($attributes);
284291
}
285292

293+
/**
294+
* Get profile's data.
295+
*
296+
* @param string $email
297+
* @param mixed|null $default
298+
*
299+
* @return array|mixed
300+
*/
301+
public function profile($email, $default = null)
302+
{
303+
return (new Profile)->get($email, $default);
304+
}
305+
286306
/**
287307
* Enable the use of the secure protocol for image URLs.
288308
*
@@ -323,18 +343,6 @@ public function exists($email)
323343
return strpos($headers[0], '200') ? true : false;
324344
}
325345

326-
/**
327-
* Get a hashed email.
328-
*
329-
* @param string $email
330-
*
331-
* @return string
332-
*/
333-
public function hashEmail($email)
334-
{
335-
return hash('md5', strtolower(trim($email)));
336-
}
337-
338346
/* -----------------------------------------------------------------
339347
| Check Methods
340348
| -----------------------------------------------------------------

src/Profile.php

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
}

tests/GravatarTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ public function it_can_set_size_from_height_or_width_attributes()
281281
static::assertSame(256, $this->gravatar->getSize());
282282
}
283283

284+
/** @test */
285+
public function it_get_profile()
286+
{
287+
$data = $this->gravatar->profile('arcanedev.maroc@gmail.com');
288+
289+
static::assertIsArray($data);
290+
static::assertArrayHasKey('entry', $data);
291+
static::assertCount(1, $data['entry']);
292+
}
293+
284294
/* -----------------------------------------------------------------
285295
| Other Methods
286296
| -----------------------------------------------------------------

0 commit comments

Comments
 (0)