@@ -17,6 +17,13 @@ final class GravatarField extends Field {
17
17
*/
18
18
private const FALLBACK_TYPES = [ '404 ' , 'mp ' , 'identicon ' , 'monsterid ' , 'wavatar ' , 'retro ' , 'robohash ' , 'blank ' ];
19
19
20
+ /**
21
+ * Ratings for the gravatar.
22
+ *
23
+ * @since $ver$
24
+ */
25
+ private const RATING_TYPES = [ 'g ' , 'pg ' , 'r ' , 'x ' ];
26
+
20
27
/**
21
28
* The composed image field.
22
29
*
@@ -44,6 +51,15 @@ final class GravatarField extends Field {
44
51
*/
45
52
private string $ default_image = 'mp ' ;
46
53
54
+ /**
55
+ * The rating to use.
56
+ *
57
+ * @since $ver$
58
+ *
59
+ * @var string
60
+ */
61
+ private string $ rating = 'g ' ;
62
+
47
63
/**
48
64
* Returns the callback used on the image field.
49
65
*
@@ -54,10 +70,11 @@ final class GravatarField extends Field {
54
70
private function generate_callback (): callable {
55
71
return fn ( string $ id , array $ data ) => ( $ data [ $ id ] ?? null )
56
72
? 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 ' ,
58
74
md5 ( $ data [ $ id ] ),
59
75
$ this ->size ,
60
76
$ this ->default_image ,
77
+ $ this ->rating
61
78
)
62
79
: '' ;
63
80
}
@@ -98,11 +115,11 @@ public function resolution( int $size ): self {
98
115
}
99
116
100
117
/**
101
- * Returns an instance of the field with a specific default image fallback
118
+ * Sets the default image fallback.
102
119
*
103
120
* @since $ver$
104
121
*
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").
106
123
*/
107
124
public function default_image ( string $ type ): self {
108
125
$ clone = clone $ this ;
@@ -111,6 +128,20 @@ public function default_image( string $type ): self {
111
128
return $ clone ;
112
129
}
113
130
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
+
114
145
/**
115
146
* Any method calls on the field are forwarded to the ImageField.
116
147
*
0 commit comments