+ /**
+ * Automatically set the https option depending on the current connection
+ * value.
+ *
+ * If the current connection is HTTPS, the https options is activated.
+ * If it is not HTTPS, the https option is deactivated.
+ *
+ * @return self
+ */
+ public function detectHttps()
+ {
+ $this->setHttps(
+ isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']
+ );
+
+ return $this;
+ }
+
+ /**
+ * Verify and cast the email address hashing algorithm to use.
+ *
+ * @param string $algorithm Algorithm to use, "sha256" or "md5".
+ *
+ * @return string Algorithm
+ *
+ * @throws InvalidArgumentException When an unsupported algorithm is given
+ */
+ protected function processAlgorithm($algorithm)
+ {
+ $algorithm = (string)$algorithm;
+ if ($algorithm !== 'md5' && $algorithm !== 'sha256') {
+ throw new InvalidArgumentException(
+ 'Only md5 and sha256 hashing supported'
+ );
+ }
+
+ return $algorithm;
+ }
+
+ /**
+ * Verify and cast the default URL to use when no avatar image can be found.
+ * If none is set, the libravatar logo is returned.
+ *
+ * @param string $url Full URL to use OR one of the following:
+ * - "404" - give a "404 File not found" instead of an image
+ * - "mm"
+ * - "identicon"
+ * - "monsterid"
+ * - "wavatar"
+ * - "retro"
+ *
+ * @return string Default URL
+ *
+ * @throws InvalidArgumentException When an invalid URL is given
+ */
+ protected function processDefault($url)
+ {
+ if ($url === null) {
+ return $url;
+ }
+
+ $url = (string)$url;
+
+ switch ($url) {
+ case '404':
+ case 'mm':
+ case 'identicon':
+ case 'monsterid':
+ case 'wavatar':
+ case 'retro':
+ case 'robohash':
+ case 'pagan':
+ break;
+ default:
+ $valid = filter_var($url, FILTER_VALIDATE_URL);
+ if (!$valid) {
+ throw new InvalidArgumentException('Invalid default avatar URL');
+ }
+ break;
+ }
+
+ return $url;
+ }
+
+ /**
+ * Verify and cast the required size of the images.
+ *
+ * @param integer $size Size (width and height in pixels) of the image.
+ * NULL for the default width.
+ *
+ * @return integer Size
+ *
+ * @throws InvalidArgumentException When a size <= 0 is given
+ */
+ protected function processSize($size)
+ {
+ if ($size === null) {
+ return $size;
+ }
+
+ $size = (int)$size;
+ if ($size <= 0) {
+ throw new InvalidArgumentException('Size has to be larger than 0');
+ }
+
+ return (int)$size;
+ }
+
+
+ /**
+ * Set the email address hashing algorithm to use.
+ * To keep gravatar compatibility, use "md5".
+ *
+ * @param string $algorithm Algorithm to use, "sha256" or "md5".
+ *
+ * @return self
+ * @throws InvalidArgumentException When an unsupported algorithm is given
+ */
+ public function setAlgorithm($algorithm)
+ {
+ $this->algorithm = $this->processAlgorithm($algorithm);
+
+ return $this;
+ }
+
+ /**
+ * Set the default URL to use when no avatar image can be found.
+ * If none is set, the gravatar logo is returned.
+ *
+ * @param string $url Full URL to use OR one of the following:
+ * - "404" - give a "404 File not found" instead of an image
+ * - "mm"
+ * - "identicon"
+ * - "monsterid"
+ * - "wavatar"
+ * - "retro"
+ *
+ * @return self
+ * @throws InvalidArgumentException When an invalid URL is given
+ */
+ public function setDefault($url)
+ {
+ $this->default = $this->processDefault($url);
+
+ return $this;
+ }
+
+ /**
+ * Set if HTTPS URLs shall be returned.
+ *
+ * @param boolean $useHttps If HTTPS url shall be returned
+ *
+ * @return self
+ *
+ * @see detectHttps()
+ */
+ public function setHttps($useHttps)
+ {
+ $this->https = (bool)$useHttps;
+
+ return $this;
+ }
+
+ /**
+ * Set the required size of the images.
+ * Every avatar image is square sized, which means you need to set only number.
+ *
+ * @param integer $size Size (width and height) of the image
+ *
+ * @return self
+ * @throws InvalidArgumentException When a size <= 0 is given
+ */
+ public function setSize($size)
+ {
+ $this->size = $this->processSize($size);
+
+ return $this;
+ }
+