Issues (105)

src/AutoMapper/User/RequestMapper.php (1 issue)

1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/AutoMapper/User/RequestMapper.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\AutoMapper\User;
10
11
use App\AutoMapper\RestRequestMapper;
12
use App\Entity\UserGroup;
13
use App\Enum\Language;
14
use App\Enum\Locale;
15
use App\Resource\UserGroupResource;
16
use InvalidArgumentException;
17
use Throwable;
18
use function array_map;
19
20
/**
21
 * Class RequestMapper
22
 *
23
 * @package App\AutoMapper
24
 * @author TLe, Tarmo Leppänen <[email protected]>
25
 */
26
class RequestMapper extends RestRequestMapper
27
{
28
    /**
29
     * @var array<int, non-empty-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, non-empty-string> at position 4 could not be parsed: Unknown type name 'non-empty-string' at position 4 in array<int, non-empty-string>.
Loading history...
30
     */
31
    protected static array $properties = [
32
        'username',
33
        'firstName',
34
        'lastName',
35
        'email',
36
        'language',
37
        'locale',
38
        'timezone',
39
        'userGroups',
40
        'password',
41
    ];
42
43 96
    public function __construct(
44
        private readonly UserGroupResource $userGroupResource,
45
    ) {
46 96
    }
47
48
    /**
49
     * @param array<int, string> $userGroups
50
     *
51
     * @return array<int, UserGroup>
52
     *
53
     * @throws Throwable
54
     */
55 4
    protected function transformUserGroups(array $userGroups): array
56
    {
57 4
        return array_map(
58 4
            fn (string $userGroupUuid): UserGroup => $this->userGroupResource->getReference($userGroupUuid),
59 4
            $userGroups,
60 4
        );
61
    }
62
63 10
    protected function transformLanguage(string $language): Language
64
    {
65 10
        return Language::tryFrom($language) ?? throw new InvalidArgumentException('Invalid language');
66
    }
67
68 3
    protected function transformLocale(string $locale): Locale
69
    {
70 3
        return Locale::tryFrom($locale) ?? throw new InvalidArgumentException('Invalid locale');
71
    }
72
}
73