Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UserNameTwigExtension
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 getUserName
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getRealName
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Twig extension to return the login name of a user
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ\Template
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2024-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2024-04-21
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Twig\Extensions;
21
22use phpMyFAQ\Configuration;
23use phpMyFAQ\Core\Exception;
24use phpMyFAQ\User;
25use Twig\Attribute\AsTwigFilter;
26use Twig\Extension\AbstractExtension;
27
28class UserNameTwigExtension extends AbstractExtension
29{
30    /**
31     * @throws Exception
32     */
33    #[AsTwigFilter(name: 'userName')]
34    public static function getUserName(int $userId): string
35    {
36        $user = new User(Configuration::getConfigurationInstance());
37        $user->getUserById($userId);
38        return $user->getLogin();
39    }
40
41    /**
42     * @throws Exception
43     */
44    #[AsTwigFilter(name: 'realName')]
45    public static function getRealName(int $userId): string
46    {
47        $user = new User(Configuration::getConfigurationInstance());
48        $user->getUserById($userId);
49        return (string) $user->getUserData(field: 'display_name');
50    }
51}