Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
82.35% |
14 / 17 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| avatar | |
82.35% |
14 / 17 |
|
50.00% |
2 / 4 |
8.35 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFunctions | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| get_avatar | |
83.33% |
10 / 12 |
|
0.00% |
0 / 1 |
5.12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | namespace phpbb\template\twig\extension; |
| 15 | |
| 16 | use phpbb\avatar\helper; |
| 17 | use phpbb\template\twig\environment; |
| 18 | use Twig\Error\Error; |
| 19 | use Twig\Extension\AbstractExtension; |
| 20 | use Twig\TwigFunction; |
| 21 | |
| 22 | class avatar extends AbstractExtension |
| 23 | { |
| 24 | /** |
| 25 | * @var helper |
| 26 | */ |
| 27 | private $avatar_helper; |
| 28 | |
| 29 | /** |
| 30 | * Constructor for avatar extension |
| 31 | * |
| 32 | * @param helper $avatar_helper |
| 33 | */ |
| 34 | public function __construct(helper $avatar_helper) |
| 35 | { |
| 36 | $this->avatar_helper = $avatar_helper; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get the name of this extension |
| 41 | * |
| 42 | * @return string |
| 43 | */ |
| 44 | public function getName(): string |
| 45 | { |
| 46 | return 'avatar'; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns a list of global functions to add to the existing list. |
| 51 | * |
| 52 | * @return TwigFunction[] An array of global functions |
| 53 | */ |
| 54 | public function getFunctions(): array |
| 55 | { |
| 56 | return [ |
| 57 | new TwigFunction('avatar', [$this, 'get_avatar'], ['needs_environment' => true]), |
| 58 | ]; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get avatar for placing into templates. |
| 63 | * |
| 64 | * How to use in a template: |
| 65 | * - {{ avatar(row, alt, ignore_config, lazy) }} |
| 66 | * |
| 67 | * The mode and row (group_row or user_row) are required. |
| 68 | * The other fields (alt|ignore_config|lazy) are optional. |
| 69 | * |
| 70 | * @return string The avatar HTML for the specified mode |
| 71 | */ |
| 72 | public function get_avatar(environment $environment, array $row): string |
| 73 | { |
| 74 | try |
| 75 | { |
| 76 | // Quickly return if required data is missing |
| 77 | if (!isset($row['src'], $row['width'], $row['height'], $row['title'])) |
| 78 | { |
| 79 | return ''; |
| 80 | } |
| 81 | |
| 82 | return $environment->render('macros/avatar.twig', [ |
| 83 | 'SRC' => !empty($row['lazy']) ? $this->avatar_helper->get_no_avatar_source() : $row['src'], |
| 84 | 'DATA_SRC' => !empty($row['lazy']) ? $row['src'] : '', |
| 85 | 'WIDTH' => $row['width'], |
| 86 | 'HEIGHT' => $row['height'], |
| 87 | 'TITLE' => $row['title'], |
| 88 | 'LAZY' => $row['lazy'] ?? false, |
| 89 | ]); |
| 90 | } |
| 91 | catch (Error $e) |
| 92 | { |
| 93 | return ''; |
| 94 | } |
| 95 | } |
| 96 | } |