-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GravatarExtension. Update the documentation.
- Loading branch information
1 parent
df65fd0
commit 291faa5
Showing
9 changed files
with
160 additions
and
32 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
File renamed without changes.
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 |
---|---|---|
|
@@ -8,7 +8,7 @@ It contains some useful functions, tests and filters missing in the original lib | |
|
||
## Installation | ||
|
||
Firstly, install via Composer: | ||
Firstly, install the package via Composer: | ||
|
||
``` | ||
composer require susina/twig-extensions | ||
|
@@ -25,6 +25,15 @@ $twig = new \Twig\Environment($loader); | |
$twig->addExtension(new \Susina\TwigExtensions\VariablesExtension()); | ||
``` | ||
|
||
Or, if you are working with Symfony, register the extensions you want as services and tag them as `twig.extension`: | ||
|
||
```yaml | ||
// In your `services.yml` file | ||
services: | ||
Susina\TwigExtensions\GravatarExtension: | ||
tags: [twig.extension] | ||
``` | ||
## VariablesExtension | ||
VariablesExtension contains some tests and functions useful for manipulating variables. | ||
|
@@ -68,14 +77,22 @@ behaves in the same way. It can be useful if you want to generate some valid php | |
|
||
### Filters | ||
|
||
`bool_to_string` filter return the string 'true' if the variable filtered can be evaluated as _true_, otherwise | ||
`bool_to_string` filter returns the string 'true' if the variable filtered can be evaluated as _true_, otherwise | ||
it returns the string _false_: | ||
|
||
```twig | ||
The "boolVariable" is {{ boolVariable|bool_to_string }}. | ||
``` | ||
it returns `The "boolVariable" is true`. | ||
|
||
You can customize the _true/false_ strings by passing two variables to the filter: the first one represents the | ||
_true_ value, the second one the _false_ value, i.e.: | ||
|
||
```twig | ||
The "boolVariable" is {{ boolVariable|bool_to_string('yes', 'no' }}. | ||
``` | ||
it returns `The "boolVariable" is yes`. | ||
|
||
## StringExtension | ||
|
||
### Filters | ||
|
@@ -97,6 +114,23 @@ By default, the filter applies single quotes `'` but you can pass any character | |
``` | ||
then it returns `"Donald Duck"`. | ||
|
||
|
||
## Gravatar Extension | ||
|
||
Gravatar extension contain a filter to retrieve the [Gravatar](https://www.gravatar.com) image from a given email. | ||
`gravatar` filter returns the uri for the avatar and you can easily use it in your html: | ||
|
||
```twig | ||
<img src="{{ [email protected] | gravatar }}" alt="My avatar" /> | ||
``` | ||
|
||
You can also pass some options to the filter, i.e.: | ||
```twig | ||
<img src="{{ [email protected] | gravatar({ size: 200, default: mp }) }}" alt="My avatar" /> | ||
``` | ||
|
||
For a full options description, please see [https://en.gravatar.com/site/implement/images/](https://en.gravatar.com/site/implement/images/). | ||
|
||
## Issues | ||
|
||
We manage issues and feature requests via [Github repository issues](https://github.com/susina/twig-extensions/issues). | ||
|
@@ -109,8 +143,8 @@ This library includes some useful composer scripts for developers: | |
|
||
- `composer test` to run the test suite | ||
- `composer analytics` to run [Psalm](https://psalm.dev/) static analysis tool | ||
- `composer cs-fix` to fix coding standard | ||
- `composer cs` to check the coding standard (see https://github.com/susina/coding-standard for details) | ||
- `composer cs:fix` to fix coding standard | ||
- `composer cs:check` to check the coding standard (see https://github.com/susina/coding-standard for details) | ||
- `composer coverage:html` to generate code coverage report in html format (into `/coverage` directory) | ||
- `composer coverage:clover` to generate code coverage report in xml format | ||
- `composer check` runs the first three commands | ||
|
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
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,71 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Susina\TwigExtensions; | ||
|
||
use Symfony\Component\OptionsResolver\Options; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFilter; | ||
|
||
/** | ||
* Class GravatarExtension | ||
*/ | ||
class GravatarExtension extends AbstractExtension | ||
{ | ||
public const AVATAR_URL = "https://secure.gravatar.com/avatar"; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getFilters(): array | ||
{ | ||
return [ | ||
new TwigFilter('gravatar', [$this ,'gravatarFilter']), | ||
]; | ||
} | ||
|
||
/** | ||
* @param string $email | ||
* @param array $params | ||
* | ||
* @return string | ||
*/ | ||
public function gravatarFilter(string $email, array $params = []): string | ||
{ | ||
$resolver = new OptionsResolver(); | ||
$this->configureOptions($resolver); | ||
|
||
$params = $resolver->resolve($params); | ||
$hash = md5(strtolower(trim($email))); | ||
|
||
return self::AVATAR_URL . "/$hash" . ($params !== [] ? "?" . http_build_query($params) : ''); | ||
} | ||
|
||
/** | ||
* @psalm-suppress UnusedClosureParam $options param is mandatory even if unused. | ||
*/ | ||
private function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver->setDefined(['size', 'forcedefault', 'default', 'rating']); | ||
|
||
$resolver->setAllowedTypes('size', 'int'); | ||
$resolver->setAllowedTypes('forcedefault', 'bool'); | ||
$resolver->setAllowedTypes('default', 'string'); | ||
$resolver->setAllowedTypes('rating', 'string'); | ||
|
||
$resolver->setAllowedValues('size', fn (int $value): bool => $value >= 1 && $value <= 2048); | ||
$resolver->setAllowedValues('default', function (string $image): bool { | ||
return str_contains($image, '://') ? | ||
(bool) filter_var($image, FILTER_VALIDATE_URL) : | ||
in_array($image, ['404', 'mp', 'identicon', 'monsterid', 'wavatar', 'retro', 'robohash', 'blank']) | ||
; | ||
}); | ||
$resolver->addAllowedValues('rating', ['g', 'pg', 'r', 'x']); | ||
|
||
$resolver->setNormalizer('default', fn (Options $options, string $image): string => urlencode($image)); | ||
$resolver->setNormalizer( | ||
'forcedefault', | ||
fn (Options $options, bool $value): string => $value === true ? 'y' : 'n' | ||
); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,10 +1,14 @@ | ||
--TEST-- | ||
"bool_to_string" filter test | ||
--TEMPLATE-- | ||
{{ bool1|bool_to_string }} | ||
{{ bool2|bool_to_string }} | ||
{{ bool1 | bool_to_string }} | ||
{{ bool2 | bool_to_string }} | ||
{{ bool2 | bool_to_string('yes', 'no') }} | ||
{{ bool1 | bool_to_string('light', 'darkness') }} | ||
--DATA-- | ||
return ['bool1' => true, 'bool2' => false] | ||
--EXPECT-- | ||
true | ||
false | ||
false | ||
no | ||
light |
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,20 @@ | ||
--TEST-- | ||
"gravatar" filter test | ||
--TEMPLATE-- | ||
{{ email | gravatar }} | ||
{{ email | gravatar({"size": 500}) }} | ||
{{ email | gravatar({"forcedefault": true}) }} | ||
{{ email | gravatar({"default": "mp"}) }} | ||
{{ email | gravatar({"default": "https://domain.org/image.jpg"}) }} | ||
{{ email | gravatar({"rating": "pg"}) }} | ||
{{ email | gravatar({"size": 500, "forcedefault": true, "default": "mp", "rating": "pg"}) }} | ||
--DATA-- | ||
return ['email' => "[email protected]"] | ||
--EXPECT-- | ||
https://secure.gravatar.com/avatar/8fbf4bd0581c9ccc67c560dea9931a1b | ||
https://secure.gravatar.com/avatar/8fbf4bd0581c9ccc67c560dea9931a1b?size=500 | ||
https://secure.gravatar.com/avatar/8fbf4bd0581c9ccc67c560dea9931a1b?forcedefault=y | ||
https://secure.gravatar.com/avatar/8fbf4bd0581c9ccc67c560dea9931a1b?default=mp | ||
https://secure.gravatar.com/avatar/8fbf4bd0581c9ccc67c560dea9931a1b?default=https%253A%252F%252Fdomain.org%252Fimage.jpg | ||
https://secure.gravatar.com/avatar/8fbf4bd0581c9ccc67c560dea9931a1b?rating=pg | ||
https://secure.gravatar.com/avatar/8fbf4bd0581c9ccc67c560dea9931a1b?size=500&forcedefault=y&default=mp&rating=pg |
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