-
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.
Merge pull request #7 from fr3nch13/dev
Adding the gravatar helper.
- Loading branch information
Showing
2 changed files
with
142 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Based on: | ||
* | ||
* Gravatar Helper (https://github.com/PotatoPowered/gravatar-helper) | ||
*/ | ||
|
||
namespace Fr3nch13\Utilities\View\Helper; | ||
|
||
use Cake\View\Helper; | ||
|
||
/** | ||
* Class GravatarHelper | ||
* | ||
* A helper that provides gravatar images for profile use in CakePHP | ||
*/ | ||
class GravatarHelper extends Helper | ||
{ | ||
/** | ||
* Takes and email address and options and returns a gravatar | ||
* | ||
* This function takes in a users email address and options and then provides either a gravatar image html tag | ||
* or an image tag with the specified default options. | ||
* | ||
* @param string $email The gravatar email address. | ||
* @param array<string, mixed> $options An array specify overrides to the default options | ||
* - size: The width and height of the profile (150 default) | ||
* - default: The default gravatar image (mm default) [List Here](http://en.gravatar.com/site/implement/images/) | ||
* - class: The css class of the image tag (gravatar default) | ||
* @return string The HTML IMG tag for the gravatar | ||
*/ | ||
public function avatar(string $email, array $options = []): string | ||
{ | ||
if (!isset($options['class'])) { | ||
$options['class'] = 'gravatar'; | ||
} | ||
|
||
return '<img alt=" ' . __('Avatar for {0}', [$email]) . | ||
'" class="' . $options['class'] . '" src="' . | ||
$this->url($email, $options) . '"/>'; | ||
} | ||
|
||
/** | ||
* Creates the url for the above image tag, or anywhere else it needs to be used. | ||
* | ||
* @param string $email The gravatar email address. | ||
* @param array<string, mixed> $options An array specify overrides to the default options | ||
* - size: The width and height of the profile (150 default) | ||
* - default: The default gravatar image (mm default) [List Here](http://en.gravatar.com/site/implement/images/) | ||
* @return string The HTML IMG tag for the gravatar | ||
*/ | ||
public function url(string $email, array $options = []): string | ||
{ | ||
// The gravatar base URL | ||
$gravatar = 'https://www.gravatar.com/avatar/'; | ||
|
||
if (!isset($options['size'])) { | ||
$options['size'] = 150; | ||
} | ||
if (!isset($options['default'])) { | ||
$options['default'] = 'mp'; | ||
} | ||
|
||
$size = '?&s=' . $options['size']; | ||
$default = '&d=' . $options['default']; | ||
$emailHash = md5(strtolower(trim($email))); | ||
|
||
return $gravatar . $emailHash . $size . $default; | ||
} | ||
} |
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,70 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Based on: | ||
* Gravatar Helper (https://github.com/PotatoPowered/gravatar-helper) | ||
*/ | ||
|
||
namespace Fr3nch13\Utilities\Test\TestCase\View\Helper; | ||
|
||
use Cake\TestSuite\TestCase; | ||
use Cake\View\View; | ||
use Fr3nch13\Utilities\View\Helper\GravatarHelper; | ||
|
||
/** | ||
* GravatarHelper Test Class | ||
* | ||
* This class contains the main tests for the GravatarHelper Class. | ||
*/ | ||
class GravatarHelperTest extends TestCase | ||
{ | ||
/** | ||
* @var \Fr3nch13\Utilities\View\Helper\GravatarHelper | ||
*/ | ||
public $Gravatar; | ||
|
||
/** | ||
* Setup the application so that we can run the tests. | ||
* | ||
* The setup involves initializing a new CakePHP view and using that to | ||
* get a copy of the GravatarHelper. | ||
*/ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$View = new View(); | ||
$this->Gravatar = new GravatarHelper($View); | ||
} | ||
|
||
/** | ||
* Test the avatar html | ||
* | ||
* @return void | ||
*/ | ||
public function testUrl(): void | ||
{ | ||
$testUrl = $this->Gravatar->url('test@email.com'); | ||
$this->assertEquals('https://www.gravatar.com/avatar/93942e96f5acd83e2e047ad8fe03114d?&s=150&d=mp', $testUrl); | ||
|
||
$testUrl = $this->Gravatar->url('test@email.com', ['size' => '75']); | ||
$this->assertEquals('https://www.gravatar.com/avatar/93942e96f5acd83e2e047ad8fe03114d?&s=75&d=mp', $testUrl); | ||
|
||
$testUrl = $this->Gravatar->url('test@email.com', ['size' => '75', 'default' => 'robohash']); | ||
$this->assertEquals('https://www.gravatar.com/avatar/93942e96f5acd83e2e047ad8fe03114d?&s=75&d=robohash', $testUrl); | ||
} | ||
|
||
/** | ||
* Test the avatar url. | ||
* | ||
* @return void | ||
*/ | ||
public function testAvatar(): void | ||
{ | ||
$testHtml = $this->Gravatar->avatar('test@email.com'); | ||
$this->assertEquals('<img alt=" Avatar for test@email.com" class="gravatar" src="https://www.gravatar.com/avatar/93942e96f5acd83e2e047ad8fe03114d?&s=150&d=mp"/>', $testHtml); | ||
|
||
$testHtml = $this->Gravatar->avatar('test@email.com', ['class' => 'test-class']); | ||
$this->assertEquals('<img alt=" Avatar for test@email.com" class="test-class" src="https://www.gravatar.com/avatar/93942e96f5acd83e2e047ad8fe03114d?&s=150&d=mp"/>', $testHtml); | ||
} | ||
} |