-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
354 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar; | ||
|
||
use dpi\ak\AvatarIdentifierInterface; | ||
use dpi\ak\AvatarKit\AvatarServices\AvatarServiceBase; | ||
use dpi\ak_gravatar\GravatarAvatarIdentifier; | ||
use League\Uri\Components\Query; | ||
use League\Uri\Schemes\Http; | ||
|
||
/** | ||
* Common functionality for Gravatar services. | ||
*/ | ||
abstract class GravatarBase extends AvatarServiceBase implements GravatarInterface { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getAvatar(AvatarIdentifierInterface $identifier) : string { | ||
$components = []; | ||
|
||
$components['scheme'] = $this->getConfiguration()->getProtocol(); | ||
// @todo test invalid protocol (this shouldnt happen since factory validates). | ||
$components['host'] = $components['scheme'] == 'http' ? 'gravatar.com' : 'secure.gravatar.com'; | ||
|
||
// Path. | ||
$identifier = $identifier->getHashed(); | ||
$components['path'] = '/avatar/' . $identifier; | ||
|
||
// Query. | ||
$width = $this->getConfiguration()->getWidth(); | ||
$query = []; | ||
if ($width) { | ||
$query['s'] = $width; | ||
} | ||
if ($query) { | ||
$components['query'] = Query::build($query); | ||
} | ||
|
||
return (string) Http::createFromComponents($components); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function createIdentifier() : AvatarIdentifierInterface { | ||
return (new GravatarAvatarIdentifier()) | ||
->setHasher(['\dpi\ak_gravatar\GravatarAvatarIdentifier', 'gravatarHasher']); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
src/AvatarKit/AvatarServices/Gravatar/GravatarIdenticon.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar; | ||
|
||
/** | ||
* Identicon avatar service. | ||
* | ||
* @AvatarService( | ||
* id = "gravatar_identicon", | ||
* name = "Identicon", | ||
* description = "Identicon as provided by Gravatar.", | ||
* protocols = { | ||
* "http", | ||
* "https" | ||
* }, | ||
* mime = { | ||
* "image/jpeg" | ||
* }, | ||
* dimensions = "1x1-2048x2048", | ||
* is_dynamic = FALSE, | ||
* is_fallback = TRUE, | ||
* is_remote = TRUE | ||
* ) | ||
*/ | ||
class GravatarIdenticon extends GravatarStaticBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function defaultId() { | ||
return 'identicon'; | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/AvatarKit/AvatarServices/Gravatar/GravatarInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar; | ||
|
||
use dpi\ak\AvatarKit\AvatarServices\AvatarServiceInterface; | ||
|
||
/** | ||
* Interface ServiceInterface. | ||
*/ | ||
interface GravatarInterface extends AvatarServiceInterface { | ||
|
||
/** | ||
* Default fallback identifier. | ||
* | ||
* This identifier is used when a Universal avatar is not found, or if 'force | ||
* default' argument is set. | ||
* | ||
* @return string | ||
* The API identifier for default provider. | ||
*/ | ||
public function defaultId(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar; | ||
|
||
/** | ||
* Monster avatar service. | ||
* | ||
* @AvatarService( | ||
* id = "gravatar_monster", | ||
* name = "Monster", | ||
* description = "Monster as provided by Gravatar.", | ||
* protocols = { | ||
* "http", | ||
* "https" | ||
* }, | ||
* mime = { | ||
* "image/jpeg" | ||
* }, | ||
* dimensions = "1x1-2048x2048", | ||
* is_dynamic = FALSE, | ||
* is_fallback = TRUE, | ||
* is_remote = TRUE | ||
* ) | ||
*/ | ||
class GravatarMonster extends GravatarStaticBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function defaultId() { | ||
return 'monsterid'; | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
src/AvatarKit/AvatarServices/Gravatar/GravatarMysteryMan.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar; | ||
|
||
/** | ||
* Mystery Man avatar service. | ||
* | ||
* @AvatarService( | ||
* id = "gravatar_mystery_man", | ||
* name = "Mystery Man", | ||
* description = "Mystery Man as provided by Gravatar.", | ||
* protocols = { | ||
* "http", | ||
* "https" | ||
* }, | ||
* mime = { | ||
* "image/jpeg" | ||
* }, | ||
* dimensions = "1x1-2048x2048", | ||
* is_dynamic = FALSE, | ||
* is_fallback = TRUE, | ||
* is_remote = TRUE | ||
* ) | ||
*/ | ||
class GravatarMysteryMan extends GravatarStaticBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function defaultId() { | ||
return 'mm'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar; | ||
|
||
/** | ||
* Retro avatar service. | ||
* | ||
* @AvatarService( | ||
* id = "gravatar_retro", | ||
* name = "Retro", | ||
* description = "Retro as provided by Gravatar.", | ||
* protocols = { | ||
* "http", | ||
* "https" | ||
* }, | ||
* mime = { | ||
* "image/jpeg" | ||
* }, | ||
* dimensions = "1x1-2048x2048", | ||
* is_dynamic = FALSE, | ||
* is_fallback = TRUE, | ||
* is_remote = TRUE | ||
* ) | ||
*/ | ||
class GravatarRetro extends GravatarStaticBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function defaultId() { | ||
return 'retro'; | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/AvatarKit/AvatarServices/Gravatar/GravatarStaticBase.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar; | ||
|
||
use dpi\ak\AvatarIdentifierInterface; | ||
use League\Uri\Components\Query; | ||
use League\Uri\Schemes\Http; | ||
|
||
/** | ||
* Base class for static Gravatars. | ||
*/ | ||
abstract class GravatarStaticBase extends GravatarBase implements GravatarInterface { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getAvatar(AvatarIdentifierInterface $identifier) : string { | ||
$url = Http::createFromString(parent::getAvatar($identifier)); | ||
|
||
$query = new Query($url->getQuery()); | ||
$query = $query->merge(Query::build([ | ||
'f' => 'y', | ||
'd' => $this->defaultId(), | ||
])); | ||
|
||
return (string) $url->withQuery((string) $query); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
src/AvatarKit/AvatarServices/Gravatar/GravatarUniversal.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar; | ||
|
||
/** | ||
* Universal Gravatar service. | ||
* | ||
* @AvatarService( | ||
* id = "gravatar_universal", | ||
* name = "Gravatar", | ||
* description = "Universal Gravatar avatar hosting service.", | ||
* protocols = { | ||
* "http", | ||
* "https" | ||
* }, | ||
* mime = { | ||
* "image/jpeg" | ||
* }, | ||
* dimensions = "1x1-2048x2048", | ||
* is_dynamic = TRUE, | ||
* is_fallback = FALSE, | ||
* is_remote = TRUE | ||
* ) | ||
*/ | ||
class GravatarUniversal extends GravatarBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function defaultId() { | ||
return '404'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar; | ||
|
||
/** | ||
* Wavatar avatar service. | ||
* | ||
* @AvatarService( | ||
* id = "gravatar_wavatar", | ||
* name = "Wavatar", | ||
* description = "Wavatar as provided by Gravatar.", | ||
* protocols = { | ||
* "http", | ||
* "https" | ||
* }, | ||
* mime = { | ||
* "image/jpeg" | ||
* }, | ||
* dimensions = "1x1-2048x2048", | ||
* is_dynamic = FALSE, | ||
* is_fallback = TRUE, | ||
* is_remote = TRUE | ||
* ) | ||
*/ | ||
class GravatarWavatar extends GravatarStaticBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function defaultId() { | ||
return 'wavatar'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace dpi\ak_gravatar; | ||
|
||
use dpi\ak\AvatarIdentifier; | ||
use dpi\ak\Exception\AvatarIdentifierException; | ||
|
||
/** | ||
* Specifies identifier logic specific to Gravatar API. | ||
*/ | ||
class GravatarAvatarIdentifier extends AvatarIdentifier { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setHashed(string $string) { | ||
if (strlen($string) > 32) { | ||
// Avatars will not be unique if longer than 32 characters. | ||
throw new AvatarIdentifierException('Gravatar does not recognise characters after 32nd character.'); | ||
} | ||
return parent::setHashed($string); | ||
} | ||
|
||
/** | ||
* Hashes strings for Gravatar. | ||
* | ||
* @param string $string | ||
* The string to hash. | ||
* | ||
* @return string | ||
* A hashed string. | ||
* | ||
* @see \dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar\GravatarBase::createIdentifier | ||
*/ | ||
public static function gravatarHasher(string $string) { | ||
$string = strtolower($string); | ||
$string = md5($string); | ||
return $string; | ||
} | ||
|
||
} |