|
29 | 29 | * <code> |
30 | 30 | * <blog:gravatar emailAddress="[email protected]" |
31 | 31 | * size="40" |
32 | | - * defaultImageUri="someDefaultImage" |
| 32 | + * defaultImage="retro" |
33 | 33 | * alt="Gravator icon of {comment.author}" |
34 | 34 | * data-name="{comment.author}" /> |
35 | 35 | * </code> |
36 | 36 | * <output> |
37 | | - * <img src="http://www.gravatar.com/avatar/4a28b782cade3dbcd6e306fa4757849d?d=someDefaultImage&s=40" /> |
| 37 | + * <img src="http://www.gravatar.com/avatar/4a28b782cade3dbcd6e306fa4757849d?d=retro&s=40" /> |
38 | 38 | * </output> |
39 | 39 | */ |
40 | | -class GravatarViewHelper extends AbstractTagBasedViewHelper |
| 40 | +final class GravatarViewHelper extends AbstractTagBasedViewHelper |
41 | 41 | { |
42 | | - /** |
43 | | - * @var string |
44 | | - */ |
45 | 42 | protected $tagName = 'img'; |
46 | | - public function __construct(private readonly UriFactory $uriFactory) |
47 | | - { |
| 43 | + |
| 44 | + public function __construct( |
| 45 | + private readonly UriFactory $uriFactory, |
| 46 | + ) { |
48 | 47 | parent::__construct(); |
49 | 48 | } |
50 | 49 |
|
51 | 50 | public function initializeArguments(): void |
52 | 51 | { |
53 | | - $this->registerArgument('emailAddress', 'string', '', true); |
54 | | - $this->registerArgument('defaultImageUri', 'string', ''); |
55 | | - $this->registerArgument('size', 'int', ''); |
| 52 | + $this->registerArgument( |
| 53 | + 'emailAddress', |
| 54 | + 'string', |
| 55 | + 'Email address used to generate the Gravatar hash', |
| 56 | + true, |
| 57 | + ); |
| 58 | + |
| 59 | + $this->registerArgument( |
| 60 | + 'defaultImage', |
| 61 | + 'string', |
| 62 | + 'Gravatar default image (mp, identicon, retro, monsterid, wavatar, robohash, blank)', |
| 63 | + false, |
| 64 | + 'mp', |
| 65 | + ); |
| 66 | + |
| 67 | + $this->registerArgument( |
| 68 | + 'size', |
| 69 | + 'int', |
| 70 | + 'Size of the avatar in pixels', |
| 71 | + false, |
| 72 | + 40, |
| 73 | + ); |
56 | 74 | } |
57 | 75 |
|
58 | 76 | public function render(): string |
59 | 77 | { |
60 | | - $gravatarUri = $this->uriFactory->createUri( |
61 | | - 'https://gravatar.com/avatar/' . md5((string)$this->arguments['emailAddress']), |
| 78 | + $emailHash = $this->normalizeEmailToHash( |
| 79 | + (string)$this->arguments['emailAddress'], |
62 | 80 | ); |
63 | 81 |
|
64 | | - $queryArguments = []; |
65 | | - if ($this->arguments['defaultImageUri'] !== null) { |
66 | | - $queryArguments['d'] = urlencode($this->arguments['defaultImageUri']); |
67 | | - } |
| 82 | + $defaultImage = $this->normalizeDefaultImage( |
| 83 | + (string)$this->arguments['defaultImage'], |
| 84 | + ); |
68 | 85 |
|
69 | | - if ($this->arguments['size'] !== null) { |
70 | | - $queryArguments['s'] = MathUtility::forceIntegerInRange((int)$this->arguments['size'], 1, 2048); |
71 | | - } |
| 86 | + $size = $this->normalizeSize( |
| 87 | + (int)$this->arguments['size'], |
| 88 | + ); |
72 | 89 |
|
73 | 90 | $this->tag->addAttribute( |
74 | 91 | 'src', |
75 | | - (string)$gravatarUri->withQuery(HttpUtility::buildQueryString($queryArguments, '', true)), |
| 92 | + $this->buildGravatarUri($emailHash, $defaultImage, $size), |
76 | 93 | ); |
77 | 94 |
|
78 | 95 | return $this->tag->render(); |
79 | 96 | } |
| 97 | + |
| 98 | + private function normalizeEmailToHash(string $email): string |
| 99 | + { |
| 100 | + return md5(strtolower(trim($email))); |
| 101 | + } |
| 102 | + |
| 103 | + private function normalizeDefaultImage(string $default): string |
| 104 | + { |
| 105 | + $allowed = [ |
| 106 | + 'mp', |
| 107 | + 'identicon', |
| 108 | + 'retro', |
| 109 | + 'monsterid', |
| 110 | + 'wavatar', |
| 111 | + 'robohash', |
| 112 | + 'blank', |
| 113 | + ]; |
| 114 | + |
| 115 | + return in_array($default, $allowed, true) ? $default : 'mp'; |
| 116 | + } |
| 117 | + |
| 118 | + private function normalizeSize(int $size): int |
| 119 | + { |
| 120 | + return MathUtility::forceIntegerInRange($size, 1, 2048); |
| 121 | + } |
| 122 | + |
| 123 | + private function buildGravatarUri(string $hash, string $default, int $size): string |
| 124 | + { |
| 125 | + return (string)$this->uriFactory |
| 126 | + ->createUri('https://www.gravatar.com/avatar/' . $hash) |
| 127 | + ->withQuery( |
| 128 | + HttpUtility::buildQueryString([ |
| 129 | + 'd' => $default, |
| 130 | + 's' => $size, |
| 131 | + ]), |
| 132 | + ); |
| 133 | + } |
80 | 134 | } |
0 commit comments