Skip to content

Commit 4c97a22

Browse files
committed
Deprecates and replaces the Gravatar helper with a simplified version that achieves the same goal.
Signed-off-by: George Steel <george@net-glue.co.uk>
1 parent 125f5f9 commit 4c97a22

File tree

6 files changed

+220
-0
lines changed

6 files changed

+220
-0
lines changed

src/Helper/Gravatar.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
/**
2323
* Helper for retrieving avatars from gravatar.com
24+
*
25+
* @deprecated This helper has been deprecated in favour of {@link GravatarImage} and will be removed in version 3.0
2426
*/
2527
class Gravatar extends AbstractHtmlElement
2628
{

src/Helper/GravatarImage.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\View\Helper;
6+
7+
use Laminas\Escaper\Escaper;
8+
use Laminas\View\HtmlAttributesSet;
9+
10+
use function md5;
11+
use function sprintf;
12+
use function strtolower;
13+
use function trim;
14+
15+
/**
16+
* @psalm-import-type AttributeSet from HtmlAttributesSet
17+
*/
18+
final class GravatarImage
19+
{
20+
private const GRAVATAR_URL = '//www.gravatar.com/avatar';
21+
22+
public const RATINGS = [
23+
'g',
24+
'pg',
25+
'r',
26+
'x',
27+
];
28+
29+
public const DEFAULT_IMAGE_VALUES = [
30+
'404',
31+
'mm',
32+
'identicon',
33+
'monsterid',
34+
'wavatar',
35+
];
36+
37+
private Escaper $escaper;
38+
39+
public function __construct(?Escaper $escaper = null)
40+
{
41+
$this->escaper = $escaper ?: new Escaper();
42+
}
43+
44+
/**
45+
* @param non-empty-string $emailAddress
46+
* @param positive-int $imageSize
47+
* @param AttributeSet $imageAttributes
48+
* @psalm-param value-of<self::DEFAULT_IMAGE_VALUES>|string $defaultImage
49+
* @psalm-param value-of<self::RATINGS> $rating
50+
*/
51+
public function __invoke(
52+
string $emailAddress,
53+
int $imageSize = 80,
54+
array $imageAttributes = [],
55+
string $defaultImage = 'mm',
56+
string $rating = 'g'
57+
): string {
58+
$imageAttributes['width'] = $imageAttributes['height'] = $imageSize;
59+
$imageAttributes['alt'] = $imageAttributes['alt'] ?? '';
60+
$imageAttributes['src'] = sprintf(
61+
'%s/%s?s=%d&r=%s&d=%s',
62+
self::GRAVATAR_URL,
63+
md5(strtolower(trim($emailAddress))),
64+
$imageSize,
65+
$rating,
66+
$this->escaper->escapeUrl($defaultImage)
67+
);
68+
69+
return sprintf(
70+
'<img%s />',
71+
(string) new HtmlAttributesSet($this->escaper, $imageAttributes)
72+
);
73+
}
74+
}

src/HelperPluginManager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class HelperPluginManager extends AbstractPluginManager
7676
'FlashMessenger' => Helper\FlashMessenger::class,
7777
'Gravatar' => Helper\Gravatar::class,
7878
'gravatar' => Helper\Gravatar::class,
79+
'gravatarImage' => Helper\GravatarImage::class,
7980
'headLink' => Helper\HeadLink::class,
8081
'HeadLink' => Helper\HeadLink::class,
8182
'headlink' => Helper\HeadLink::class,
@@ -259,6 +260,7 @@ class HelperPluginManager extends AbstractPluginManager
259260
Helper\EscapeCss::class => InvokableFactory::class,
260261
Helper\EscapeUrl::class => InvokableFactory::class,
261262
Helper\Gravatar::class => InvokableFactory::class,
263+
Helper\GravatarImage::class => InvokableFactory::class,
262264
Helper\HtmlTag::class => InvokableFactory::class,
263265
Helper\HeadLink::class => InvokableFactory::class,
264266
Helper\HeadMeta::class => InvokableFactory::class,

src/Renderer/PhpRenderer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
* @method \Laminas\View\Helper\Navigation\Links links($container = null)
8787
* @method \Laminas\View\Helper\Navigation\Menu menu($container = null)
8888
* @method \Laminas\View\Helper\Navigation\Sitemap sitemap($container = null)
89+
* @method string gravatarImage(string $emailAddress, int $imageSize = 80, array $imageAttributes = [], string $defaultImage = 'mm', string $rating = 'g')
8990
*/
9091
class PhpRenderer implements Renderer, TreeRendererInterface
9192
{

test/Helper/GravatarImageTest.php

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LaminasTest\View\Helper;
6+
7+
use Laminas\View\Helper\GravatarImage;
8+
use PHPUnit\Framework\TestCase;
9+
10+
use function md5;
11+
12+
class GravatarImageTest extends TestCase
13+
{
14+
private GravatarImage $helper;
15+
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
$this->helper = new GravatarImage();
20+
}
21+
22+
public function testThatTheGivenEmailAddressWillBeHashed(): string
23+
{
24+
$image = ($this->helper)('me@example.com');
25+
26+
self::assertStringContainsString(
27+
md5('me@example.com'),
28+
$image
29+
);
30+
31+
return $image;
32+
}
33+
34+
/** @depends testThatTheGivenEmailAddressWillBeHashed */
35+
public function testTheRatingWillDefaultToG(string $markup): void
36+
{
37+
self::assertStringContainsString(
38+
'r&#x3D;g&amp;',
39+
$markup
40+
);
41+
}
42+
43+
/** @depends testThatTheGivenEmailAddressWillBeHashed */
44+
public function testAnEmptyAltAttributeWillBeAddedByDefault(string $markup): void
45+
{
46+
self::assertStringContainsString(
47+
'alt=""',
48+
$markup
49+
);
50+
}
51+
52+
/** @depends testThatTheGivenEmailAddressWillBeHashed */
53+
public function testTheImageSizeWillBe80(string $markup): void
54+
{
55+
self::assertStringContainsString(
56+
's&#x3D;80',
57+
$markup
58+
);
59+
}
60+
61+
/** @depends testThatTheGivenEmailAddressWillBeHashed */
62+
public function testWidthAndHeightAttributesWillBePresent(string $markup): void
63+
{
64+
self::assertStringContainsString(
65+
'width="80"',
66+
$markup
67+
);
68+
self::assertStringContainsString(
69+
'height="80"',
70+
$markup
71+
);
72+
}
73+
74+
/** @depends testThatTheGivenEmailAddressWillBeHashed */
75+
public function testTheDefaultFallbackImageImageSizeWillBeMM(string $markup): void
76+
{
77+
self::assertStringContainsString(
78+
'd&#x3D;mm',
79+
$markup
80+
);
81+
}
82+
83+
public function testThatAttributesCanBeAddedToTheImageMarkup(): void
84+
{
85+
$image = ($this->helper)('me@example.com', 80, ['data-foo' => 'bar']);
86+
self::assertStringContainsString(
87+
'data-foo="bar"',
88+
$image
89+
);
90+
}
91+
92+
public function testThatTheImageSizeCanBeAltered(): string
93+
{
94+
$image = ($this->helper)('me@example.com', 123);
95+
self::assertStringContainsString(
96+
's&#x3D;123',
97+
$image
98+
);
99+
100+
return $image;
101+
}
102+
103+
/** @depends testThatTheImageSizeCanBeAltered */
104+
public function testWidthAndHeightAttributesWillMatchCustomValue(string $markup): void
105+
{
106+
self::assertStringContainsString(
107+
'width="123"',
108+
$markup
109+
);
110+
self::assertStringContainsString(
111+
'height="123"',
112+
$markup
113+
);
114+
}
115+
116+
public function testThatTheRatingCanBeAltered(): void
117+
{
118+
$image = ($this->helper)('me@example.com', 80, [], 'mm', 'x');
119+
self::assertStringContainsString(
120+
'r&#x3D;x&amp;',
121+
$image
122+
);
123+
}
124+
125+
public function testThatTheDefaultImageCanBeAltered(): void
126+
{
127+
$image = ($this->helper)('me@example.com', 80, [], 'wavatar');
128+
self::assertStringContainsString(
129+
'd&#x3D;wavatar',
130+
$image
131+
);
132+
}
133+
134+
public function testThatTheDefaultImageCanBeAnUrl(): void
135+
{
136+
$image = ($this->helper)('me@example.com', 80, [], 'https://example.com/someimage');
137+
$expect = 'https&#x25;3A&#x25;2F&#x25;2Fexample.com&#x25;2Fsomeimage';
138+
self::assertStringContainsString($expect, $image);
139+
}
140+
}

test/Helper/GravatarTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use function strtoupper;
1515
use function urlencode;
1616

17+
/** @psalm-suppress DeprecatedClass */
1718
class GravatarTest extends TestCase
1819
{
1920
/** @var Gravatar */

0 commit comments

Comments
 (0)