Dropped Gravatar class

Gone but not forgottten, the class now lives in
https://github.com/joshtronic/php-gravatar
This commit is contained in:
Josh Sherman 2014-09-23 20:58:12 -04:00
parent 2f2dd0d8b8
commit 67be4e0889
2 changed files with 0 additions and 195 deletions

View file

@ -1,98 +0,0 @@
<?php
/**
* Gravatar Class File for PICKLES
*
* PHP version 5
*
* Licensed under The MIT License
* Redistribution of these files must retain the above copyright notice.
*
* @author Josh Sherman <josh@gravityblvd.com>
* @copyright Copyright 2007-2014, Josh Sherman
* @license http://www.opensource.org/licenses/mit-license.html
* @package PICKLES
* @link https://github.com/joshtronic/pickles
*/
/**
* Gravatar API Interface
*
* @link http://en.gravatar.com/site/implement/
*/
class API_Gravatar
{
/**
* Hash
*
* Generates a hash from the passed string that can then be used for
* fetching an image or profile from Gravatar.com
*
* @static
* @param string $string string to hash, should be an email address
* @return string resulting hash
*/
public static function hash($string)
{
// Trims whitespace, lowers the case then applies MD5
return md5(strtolower(trim($string)));
}
/**
* img
*
* Generates an img tag requesting a Gravatar based on the parameters.
*
* @static
* @param string $email address to use for the hash
* @param integer $size optional size of the image requested
* @param string $default optional default style or image to generate
* @param string $rating optional filter by a certain rating
* @param boolean $force optional force the default avatar
* @param boolean $secure optional whether to use the SSL URL
* @param array $attributes optional any additional parameters to include
* @return string an img tag requesting a Gravatar
*/
public static function img($email, $size = 80, $default = 'gravatar', $rating = 'g', $force = false, $secure = false, $attributes = false)
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
throw new Exception('Invalid email address.');
}
elseif ($size < 1 || $size > 2048)
{
throw new Exception('Invalid size parameter, expecting an integer between 1 and 2048.');
}
elseif (!in_array($default, ['gravatar', '404', 'mm', 'identicon', 'monsterid', 'wavatar', 'retro', 'blank'])
&& !filter_var($default, FILTER_VALIDATE_URL))
{
throw new Exception('Invalid default parameter, expecting gravatar, 404, mm, identicon, monsterid, wavatar, retro, blank or a valid URL.');
}
elseif (!in_array($rating, ['g', 'pg', 'r', 'x']))
{
throw new Exception('Invalid rating parameter, expecting g, pg, r or x.');
}
else
{
$default = $default == 'gravatar' ? false : urlencode($default);
$html = '<img src="'
. ($secure ? 'https://secure' : 'http://www') . '.gravatar.com/avatar/' . self::hash($email)
. sprintf('?s=%s&d=%s&r=%s', $size, urlencode($default), $rating, $force)
. ($force ? '&f=y' : '') . '"';
if (is_array($attributes))
{
foreach ($attributes as $attribute => $value)
{
$html .= sprintf(' %s="%s"', $attribute, $value);
}
}
$html .= '>';
return $html;
}
}
}

View file

@ -1,97 +0,0 @@
<?php
class API_Gravatar_Test extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider providerHash
*/
public function testHash($a, $b)
{
$this->assertEquals($b, API_Gravatar::hash($a));
}
public function providerHash()
{
return [
['foo@bar.com', 'f3ada405ce890b6f8204094deb12d8a8'],
['FOO@BAR.COM', 'f3ada405ce890b6f8204094deb12d8a8'],
];
}
/**
* @expectedException Exception
* @expectedExceptionMessage Invalid email address.
*/
public function testImgInvalidEmail()
{
API_Gravatar::img('invalidemail');
}
/**
* @expectedException Exception
* @expectedExceptionMessage Invalid size parameter, expecting an integer between 1 and 2048.
*/
public function testImgInvalidSize()
{
API_Gravatar::img('foo@bar.com', 2050);
}
/**
* @expectedException Exception
* @expectedExceptionMessage Invalid default parameter, expecting gravatar, 404, mm, identicon, monsterid, wavatar, retro, blank or a valid URL.
*/
public function testImgInvalidDefault()
{
API_Gravatar::img('foo@bar.com', 80, 'invalid');
}
/**
* @expectedException Exception
* @expectedExceptionMessage Invalid rating parameter, expecting g, pg, r or x.
*/
public function testImgInvalidRating()
{
API_Gravatar::img('foo@bar.com', 80, 'gravatar', 'sexytime');
}
public function testURLDefault()
{
$this->assertEquals(
'<img src="http://www.gravatar.com/avatar/f3ada405ce890b6f8204094deb12d8a8?s=80&d=http%253A%252F%252Fexample.org%252Ficon&r=g">',
API_Gravatar::img('foo@bar.com', 80, 'http://example.org/icon')
);
}
public function testForce()
{
$this->assertEquals(
'<img src="http://www.gravatar.com/avatar/f3ada405ce890b6f8204094deb12d8a8?s=80&d=&r=g&f=y">',
API_Gravatar::img('foo@bar.com', 80, 'gravatar', 'g', true)
);
}
public function testSecure()
{
$this->assertEquals(
'<img src="https://secure.gravatar.com/avatar/f3ada405ce890b6f8204094deb12d8a8?s=80&d=&r=g">',
API_Gravatar::img('foo@bar.com', 80, 'gravatar', 'g', false, true)
);
}
public function testImg()
{
$this->assertEquals(
'<img src="http://www.gravatar.com/avatar/f3ada405ce890b6f8204094deb12d8a8?s=80&d=&r=g">',
API_Gravatar::img('foo@bar.com')
);
}
public function testImgWithParameters()
{
$this->assertEquals(
'<img src="http://www.gravatar.com/avatar/f3ada405ce890b6f8204094deb12d8a8?s=80&d=&r=g" class="gravatar">',
API_Gravatar::img('foo@bar.com', 80, 'gravatar', 'g', false, false, ['class' => 'gravatar'])
);
}
}