-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
357 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
language: php | ||
|
||
sudo: false | ||
|
||
php: | ||
- 7.1.3 | ||
- 7.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php namespace Arcanedev\Gravatar\Concerns; | ||
|
||
/** | ||
* Trait HashEmail | ||
* | ||
* @package Arcanedev\Gravatar\Concerns | ||
* @author ARCANEDEV <[email protected]> | ||
*/ | ||
trait HashEmail | ||
{ | ||
/* ----------------------------------------------------------------- | ||
| Main Methods | ||
| ----------------------------------------------------------------- | ||
*/ | ||
|
||
/** | ||
* Get a hashed email. | ||
* | ||
* @param string $email | ||
* | ||
* @return string | ||
*/ | ||
public static function hashEmail($email) | ||
{ | ||
return hash('md5', strtolower(trim($email))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php namespace Arcanedev\Gravatar\Exceptions; | ||
|
||
/** | ||
* Class InvalidProfileFormatException | ||
* | ||
* @package Arcanedev\Gravatar\Exceptions | ||
* @author ARCANEDEV <[email protected]> | ||
*/ | ||
class InvalidProfileFormatException extends \InvalidArgumentException | ||
{ | ||
/** | ||
* Make a new exception. | ||
* | ||
* @param string $format | ||
* @param array $supportedFormat | ||
* | ||
* @return \Arcanedev\Gravatar\Exceptions\InvalidProfileFormatException | ||
*/ | ||
public static function make($format, array $supportedFormat) | ||
{ | ||
return new static( | ||
sprintf( | ||
'The format [%s] is invalid, the supported formats are: %s', | ||
$format, | ||
implode(', ', $supportedFormat) | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
<?php namespace Arcanedev\Gravatar; | ||
|
||
use Arcanedev\Gravatar\Exceptions\InvalidProfileFormatException; | ||
|
||
/** | ||
* Class Profile | ||
* | ||
* @package Arcanedev\Gravatar | ||
* @author ARCANEDEV <[email protected]> | ||
*/ | ||
class Profile | ||
{ | ||
/* ----------------------------------------------------------------- | ||
| Traits | ||
| ----------------------------------------------------------------- | ||
*/ | ||
|
||
use Concerns\HashEmail; | ||
|
||
/* ----------------------------------------------------------------- | ||
| Constants | ||
| ----------------------------------------------------------------- | ||
*/ | ||
|
||
const BASE_URL = 'http://www.gravatar.com/'; | ||
const SECURE_URL = 'https://www.gravatar.com/'; | ||
|
||
/* ----------------------------------------------------------------- | ||
| Properties | ||
| ----------------------------------------------------------------- | ||
*/ | ||
|
||
/** | ||
* Profile's format. | ||
* | ||
* @var string | ||
*/ | ||
protected $format; | ||
|
||
/** | ||
* Supported format. | ||
* | ||
* @var array | ||
*/ | ||
protected static $supportedFormat = ['json', 'xml', 'php', 'vcf', 'qr']; | ||
|
||
/* ----------------------------------------------------------------- | ||
| Getters & Setters | ||
| ----------------------------------------------------------------- | ||
*/ | ||
|
||
/** | ||
* Get the profile's format. | ||
* | ||
* @return string|null | ||
*/ | ||
public function getFormat() | ||
{ | ||
return $this->format; | ||
} | ||
|
||
/** | ||
* Set the profile's format. | ||
* | ||
* @param string $format | ||
* | ||
* @return \Arcanedev\Gravatar\Profile | ||
*/ | ||
public function setFormat($format = null) | ||
{ | ||
if ( ! is_null($format)) { | ||
self::checkFormat($format); | ||
$this->format = $format; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/* ----------------------------------------------------------------- | ||
| Main Methods | ||
| ----------------------------------------------------------------- | ||
*/ | ||
|
||
/** | ||
* Build the profile URL based on the provided email address. | ||
* | ||
* @param string $email | ||
* @param array $params | ||
* @param bool $secure | ||
* | ||
* @return string | ||
*/ | ||
public function getUrl($email = null, array $params = [], $secure = true) | ||
{ | ||
$url = $secure ? static::SECURE_URL : static::BASE_URL; | ||
$url .= is_null($email) | ||
? str_repeat('0', 32) | ||
: static::hashEmail($email); | ||
|
||
if ($this->hasFormat()) | ||
$url .= ".{$this->getFormat()}"; | ||
|
||
if ( ! empty($params)) | ||
$url .= '?'.http_build_query($params); | ||
|
||
return $url; | ||
} | ||
|
||
/** | ||
* Get the profile data. | ||
* | ||
* @param string $email | ||
* @param mixed|null $default | ||
* | ||
* @return array|mixed | ||
*/ | ||
public function get($email, $default = null) | ||
{ | ||
$this->setFormat('php'); | ||
|
||
$data = unserialize( | ||
file_get_contents($this->getUrl($email)) | ||
); | ||
|
||
return (is_array($data) && isset($data['entry'])) | ||
? $data | ||
: $default; | ||
} | ||
|
||
/* ----------------------------------------------------------------- | ||
| Check Methods | ||
| ----------------------------------------------------------------- | ||
*/ | ||
|
||
/** | ||
* Check if the format is not null. | ||
* | ||
* @return bool | ||
*/ | ||
public function hasFormat() | ||
{ | ||
return ! is_null($this->format); | ||
} | ||
|
||
/** | ||
* Check the format. | ||
* | ||
* @param string $format | ||
*/ | ||
private static function checkFormat(&$format) | ||
{ | ||
$format = strtolower($format); | ||
|
||
if ( ! in_array($format, static::$supportedFormat)) { | ||
throw InvalidProfileFormatException::make($format, static::$supportedFormat); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -281,6 +281,16 @@ public function it_can_set_size_from_height_or_width_attributes() | |
static::assertSame(256, $this->gravatar->getSize()); | ||
} | ||
|
||
/** @test */ | ||
public function it_get_profile() | ||
{ | ||
$data = $this->gravatar->profile('[email protected]'); | ||
|
||
static::assertIsArray($data); | ||
static::assertArrayHasKey('entry', $data); | ||
static::assertCount(1, $data['entry']); | ||
} | ||
|
||
/* ----------------------------------------------------------------- | ||
| Other Methods | ||
| ----------------------------------------------------------------- | ||
|
Oops, something went wrong.