Skip to content

Commit 65a3e1f

Browse files
committed
Adds rating to Gravatar fields
Resolves #38
1 parent ecb921c commit 65a3e1f

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/Field/GravatarField.php

+34-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ final class GravatarField extends Field {
1717
*/
1818
private const FALLBACK_TYPES = [ '404', 'mp', 'identicon', 'monsterid', 'wavatar', 'retro', 'robohash', 'blank' ];
1919

20+
/**
21+
* Ratings for the gravatar.
22+
*
23+
* @since $ver$
24+
*/
25+
private const RATING_TYPES = [ 'g', 'pg', 'r', 'x' ];
26+
2027
/**
2128
* The composed image field.
2229
*
@@ -44,6 +51,15 @@ final class GravatarField extends Field {
4451
*/
4552
private string $default_image = 'mp';
4653

54+
/**
55+
* The rating to use.
56+
*
57+
* @since $ver$
58+
*
59+
* @var string
60+
*/
61+
private string $rating = 'g';
62+
4763
/**
4864
* Returns the callback used on the image field.
4965
*
@@ -54,10 +70,11 @@ final class GravatarField extends Field {
5470
private function generate_callback(): callable {
5571
return fn( string $id, array $data ) => ( $data[ $id ] ?? null )
5672
? sprintf(
57-
'https://gravatar.com/avatar/%s?size=%d&default=%s',
73+
'https://gravatar.com/avatar/%1$s?size=%2$d&default=%3$s&rating=%4$s',
5874
md5( $data[ $id ] ),
5975
$this->size,
6076
$this->default_image,
77+
$this->rating
6178
)
6279
: '';
6380
}
@@ -98,11 +115,11 @@ public function resolution( int $size ): self {
98115
}
99116

100117
/**
101-
* Returns an instance of the field with a specific default image fallback
118+
* Sets the default image fallback.
102119
*
103120
* @since $ver$
104121
*
105-
* @return self A field instance with the provided default image.
122+
* @return self A field instance with the provided default image. Default is 'mp' ("mystery person").
106123
*/
107124
public function default_image( string $type ): self {
108125
$clone = clone $this;
@@ -111,6 +128,20 @@ public function default_image( string $type ): self {
111128
return $clone;
112129
}
113130

131+
/**
132+
* Sets the rating for the gravatar.
133+
*
134+
* @since $ver$
135+
*
136+
* @return self A field instance with the provided rating. Default is 'g'.
137+
*/
138+
public function rating( string $type ): self {
139+
$clone = clone $this;
140+
$clone->rating = in_array( $type, self::RATING_TYPES, true ) ? $type : 'g';
141+
142+
return $clone;
143+
}
144+
114145
/**
115146
* Any method calls on the field are forwarded to the ImageField.
116147
*

0 commit comments

Comments
 (0)