Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
RoleTransformer
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 transform
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 reverseTransform
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * This file is part of the MADIS - RGPD Management application.
5 *
6 * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22declare(strict_types=1);
23
24namespace App\Domain\User\Form\DataTransformer;
25
26use Symfony\Component\Form\DataTransformerInterface;
27use Symfony\Component\Form\Exception\TransformationFailedException;
28
29class RoleTransformer implements DataTransformerInterface
30{
31    /**
32     * Transform Role given as array to string value.
33     *
34     * @param array $value The array of roles
35     *
36     * @return string|null The parsed array to string
37     */
38    public function transform($value): ?string
39    {
40        if (\is_null($value)) {
41            return null;
42        }
43
44        if (!\is_array($value)) {
45            throw new TransformationFailedException("Value of type array expected, '{$value}' given");
46        }
47
48        return \implode(', ', $value);
49    }
50
51    /**
52     * Transform Role given as string to array value.
53     *
54     * @param mixed $value The string of roles
55     *
56     * @return array The parsed string to array
57     */
58    public function reverseTransform($value): array
59    {
60        if (\is_null($value)) {
61            return [];
62        }
63
64        return [$value];
65    }
66}