-
-
Notifications
You must be signed in to change notification settings - Fork 46
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 #147 from gsteel/gravatar
Deprecates and replaces the Gravatar helper with a simplified version…
- Loading branch information
Showing
6 changed files
with
220 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
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,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\View\Helper; | ||
|
||
use Laminas\Escaper\Escaper; | ||
use Laminas\View\HtmlAttributesSet; | ||
|
||
use function md5; | ||
use function sprintf; | ||
use function strtolower; | ||
use function trim; | ||
|
||
/** | ||
* @psalm-import-type AttributeSet from HtmlAttributesSet | ||
*/ | ||
final class GravatarImage | ||
{ | ||
private const GRAVATAR_URL = '//www.gravatar.com/avatar'; | ||
|
||
public const RATINGS = [ | ||
'g', | ||
'pg', | ||
'r', | ||
'x', | ||
]; | ||
|
||
public const DEFAULT_IMAGE_VALUES = [ | ||
'404', | ||
'mm', | ||
'identicon', | ||
'monsterid', | ||
'wavatar', | ||
]; | ||
|
||
private Escaper $escaper; | ||
|
||
public function __construct(?Escaper $escaper = null) | ||
{ | ||
$this->escaper = $escaper ?: new Escaper(); | ||
} | ||
|
||
/** | ||
* @param non-empty-string $emailAddress | ||
* @param positive-int $imageSize | ||
* @param AttributeSet $imageAttributes | ||
* @psalm-param value-of<self::DEFAULT_IMAGE_VALUES>|string $defaultImage | ||
* @psalm-param value-of<self::RATINGS> $rating | ||
*/ | ||
public function __invoke( | ||
string $emailAddress, | ||
int $imageSize = 80, | ||
array $imageAttributes = [], | ||
string $defaultImage = 'mm', | ||
string $rating = 'g' | ||
): string { | ||
$imageAttributes['width'] = $imageAttributes['height'] = $imageSize; | ||
$imageAttributes['alt'] = $imageAttributes['alt'] ?? ''; | ||
$imageAttributes['src'] = sprintf( | ||
'%s/%s?s=%d&r=%s&d=%s', | ||
self::GRAVATAR_URL, | ||
md5(strtolower(trim($emailAddress))), | ||
$imageSize, | ||
$rating, | ||
$this->escaper->escapeUrl($defaultImage) | ||
); | ||
|
||
return sprintf( | ||
'<img%s />', | ||
(string) new HtmlAttributesSet($this->escaper, $imageAttributes) | ||
); | ||
} | ||
} |
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
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,140 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaminasTest\View\Helper; | ||
|
||
use Laminas\View\Helper\GravatarImage; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
use function md5; | ||
|
||
class GravatarImageTest extends TestCase | ||
{ | ||
private GravatarImage $helper; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->helper = new GravatarImage(); | ||
} | ||
|
||
public function testThatTheGivenEmailAddressWillBeHashed(): string | ||
{ | ||
$image = ($this->helper)('[email protected]'); | ||
|
||
self::assertStringContainsString( | ||
md5('[email protected]'), | ||
$image | ||
); | ||
|
||
return $image; | ||
} | ||
|
||
/** @depends testThatTheGivenEmailAddressWillBeHashed */ | ||
public function testTheRatingWillDefaultToG(string $markup): void | ||
{ | ||
self::assertStringContainsString( | ||
'r=g&', | ||
$markup | ||
); | ||
} | ||
|
||
/** @depends testThatTheGivenEmailAddressWillBeHashed */ | ||
public function testAnEmptyAltAttributeWillBeAddedByDefault(string $markup): void | ||
{ | ||
self::assertStringContainsString( | ||
'alt=""', | ||
$markup | ||
); | ||
} | ||
|
||
/** @depends testThatTheGivenEmailAddressWillBeHashed */ | ||
public function testTheImageSizeWillBe80(string $markup): void | ||
{ | ||
self::assertStringContainsString( | ||
's=80', | ||
$markup | ||
); | ||
} | ||
|
||
/** @depends testThatTheGivenEmailAddressWillBeHashed */ | ||
public function testWidthAndHeightAttributesWillBePresent(string $markup): void | ||
{ | ||
self::assertStringContainsString( | ||
'width="80"', | ||
$markup | ||
); | ||
self::assertStringContainsString( | ||
'height="80"', | ||
$markup | ||
); | ||
} | ||
|
||
/** @depends testThatTheGivenEmailAddressWillBeHashed */ | ||
public function testTheDefaultFallbackImageImageSizeWillBeMM(string $markup): void | ||
{ | ||
self::assertStringContainsString( | ||
'd=mm', | ||
$markup | ||
); | ||
} | ||
|
||
public function testThatAttributesCanBeAddedToTheImageMarkup(): void | ||
{ | ||
$image = ($this->helper)('[email protected]', 80, ['data-foo' => 'bar']); | ||
self::assertStringContainsString( | ||
'data-foo="bar"', | ||
$image | ||
); | ||
} | ||
|
||
public function testThatTheImageSizeCanBeAltered(): string | ||
{ | ||
$image = ($this->helper)('[email protected]', 123); | ||
self::assertStringContainsString( | ||
's=123', | ||
$image | ||
); | ||
|
||
return $image; | ||
} | ||
|
||
/** @depends testThatTheImageSizeCanBeAltered */ | ||
public function testWidthAndHeightAttributesWillMatchCustomValue(string $markup): void | ||
{ | ||
self::assertStringContainsString( | ||
'width="123"', | ||
$markup | ||
); | ||
self::assertStringContainsString( | ||
'height="123"', | ||
$markup | ||
); | ||
} | ||
|
||
public function testThatTheRatingCanBeAltered(): void | ||
{ | ||
$image = ($this->helper)('[email protected]', 80, [], 'mm', 'x'); | ||
self::assertStringContainsString( | ||
'r=x&', | ||
$image | ||
); | ||
} | ||
|
||
public function testThatTheDefaultImageCanBeAltered(): void | ||
{ | ||
$image = ($this->helper)('[email protected]', 80, [], 'wavatar'); | ||
self::assertStringContainsString( | ||
'd=wavatar', | ||
$image | ||
); | ||
} | ||
|
||
public function testThatTheDefaultImageCanBeAnUrl(): void | ||
{ | ||
$image = ($this->helper)('[email protected]', 80, [], 'https://example.com/someimage'); | ||
$expect = 'https%3A%2F%2Fexample.com%2Fsomeimage'; | ||
self::assertStringContainsString($expect, $image); | ||
} | ||
} |
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