Skip to content
This repository was archived by the owner on Mar 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #12 from stof/url_by_hash
Browse files Browse the repository at this point in the history
Added a method to get the gravatar url for a given hash
  • Loading branch information
ornicar committed Jul 19, 2011
2 parents 611405e + 6a3456b commit fc5a035
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
14 changes: 14 additions & 0 deletions GravatarApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ public function getUrl($email, $size = null, $rating = null, $default = null)
{
$hash = md5(strtolower($email));

return $this->getUrlForHash($hash, $size, $rating, $default);
}

/**
* Returns a url for a gravatar for the given hash.
*
* @param string $hash
* @param integer $size
* @param string $rating
* @param string $default
* @return string
*/
public function getUrlForHash($hash, $size = null, $rating = null, $default = null)
{
$map = array(
's' => $size ?: $this->defaults['size'],
'r' => $rating ?: $this->defaults['rating'],
Expand Down
18 changes: 16 additions & 2 deletions Templating/Helper/GravatarHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,27 @@ public function getUrl($email, $size = null, $rating = null, $default = null)
{
return $this->api->getUrl($email, $size, $rating, $default);
}


/**
* Returns a url for a gravatar for a given hash
*
* @param string $hash
* @param integer $size
* @param string $rating
* @param string $default
* @return string
*/
public function getUrlForHash($hash, $size = null, $rating = null, $default = null)
{
return $this->api->getUrlForHash($hash, $size, $rating, $default);
}

public function render($email, array $options = array())
{
$size = isset($options['size'])?$options['size']:null;
$rating = isset($options['rating'])?$options['rating']:null;
$default = isset($options['default'])?$options['default']:null;

return $this->api->getUrl($email, $size, $rating, $default);
}

Expand Down
6 changes: 6 additions & 0 deletions Twig/GravatarExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function getFunctions()
{
return array(
'gravatar' => new \Twig_Function_Method($this, 'getUrl'),
'gravatar_hash' => new \Twig_Function_Method($this, 'getUrlForHash'),
'gravatar_exists' => new \Twig_Function_Method($this, 'exists'),
);
}
Expand All @@ -36,6 +37,11 @@ public function getUrl($email, $size = null, $rating = null, $default = null)
return $this->api->getUrl($email, $size, $rating, $default);
}

public function getUrlForHash($hash, $size = null, $rating = null, $default = null)
{
return $this->api->getUrlForHash($hash, $size, $rating, $default);
}

public function exists($email)
{
return $this->api->exists($email);
Expand Down

0 comments on commit fc5a035

Please sign in to comment.