Passed
Push — master ( 77290b...f3fb78 )
by Marcel
09:09
created

HashToKeyValueArrayTransformer::transform()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/* Copyright (c) 2004-2013 Bart van den Burg
4
5
Permission is hereby granted, free of charge, to any person obtaining a copy
6
of this software and associated documentation files (the "Software"), to deal
7
in the Software without restriction, including without limitation the rights
8
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
copies of the Software, and to permit persons to whom the Software is furnished
10
to do so, subject to the following conditions:
11
12
The above copyright notice and this permission notice shall be included in all
13
copies or substantial portions of the Software.
14
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
THE SOFTWARE. */
22
23
namespace App\Form\DataTransformer;
24
25
use Symfony\Component\Form\DataTransformerInterface;
26
use Symfony\Component\Form\Exception\TransformationFailedException;
27
28
class HashToKeyValueArrayTransformer implements DataTransformerInterface
29
{
30
31
    /**
32
     * @param bool $useContainerObject Whether to return a KeyValueContainer object or simply an array
33
     */
34
    public function __construct(private bool $useContainerObject)
35
    {
36
    }
37
38
    /**
39
     * Doing the transformation here would be too late for the collection type to do it's resizing magic, so
40
     * instead it is done in the forms PRE_SET_DATA listener
41
     */
42
    public function transform($value): mixed
43
    {
44
        return $value;
45
    }
46
47
    /**
48
     * @throws TransformationFailedException
49
     */
50
    public function reverseTransform(mixed $value): KeyValueContainer|array {
51
        $return = $this->useContainerObject ? new KeyValueContainer() : [];
52
53
        foreach ($value as $data) {
54
            if (['key', 'value'] != array_keys($data)) {
55
                throw new TransformationFailedException;
56
            }
57
58
            if (array_key_exists($data['key'], $return)) {
0 ignored issues
show
Bug introduced by
It seems like $return can also be of type App\Form\DataTransformer\KeyValueContainer; however, parameter $array of array_key_exists() does only seem to accept ArrayObject|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
            if (array_key_exists($data['key'], /** @scrutinizer ignore-type */ $return)) {
Loading history...
59
                throw new TransformationFailedException('Duplicate key detected');
60
            }
61
62
            $return[$data['key']] = $data['value'];
63
        }
64
65
        return $return;
66
    }
67
68
}