Passed
Push — master ( f2e1d1...eea237 )
by Divine Niiquaye
03:30
created

CollectionToArrayTransformer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 40
ccs 0
cts 14
cp 0
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 17 4
A reverseTransform() 0 9 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\Database\Doctrine\Form\DataTransformer;
19
20
use Doctrine\Common\Collections\ArrayCollection;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\Collections\ArrayCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Doctrine\Common\Collections\Collection;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\Collections\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Symfony\Component\Form\DataTransformerInterface;
23
use Symfony\Component\Form\Exception\TransformationFailedException;
24
25
/**
26
 * @author Bernhard Schussek <[email protected]>
27
 */
28
class CollectionToArrayTransformer implements DataTransformerInterface
29
{
30
    /**
31
     * Transforms a collection into an array.
32
     *
33
     * @throws TransformationFailedException
34
     */
35
    public function transform(mixed $collection): mixed
36
    {
37
        if (null === $collection) {
38
            return [];
39
        }
40
41
        // For cases when the collection getter returns $collection->toArray()
42
        // in order to prevent modifications of the returned collection
43
        if (\is_array($collection)) {
44
            return $collection;
45
        }
46
47
        if (!$collection instanceof Collection) {
48
            throw new TransformationFailedException('Expected a Doctrine\Common\Collections\Collection object.');
49
        }
50
51
        return $collection->toArray();
52
    }
53
54
    /**
55
     * Transforms choice keys into entities.
56
     *
57
     * @param mixed $array An array of entities
58
     */
59
    public function reverseTransform(mixed $array): Collection
60
    {
61
        if ('' === $array || null === $array) {
62
            $array = [];
63
        } else {
64
            $array = (array) $array;
65
        }
66
67
        return new ArrayCollection($array);
68
    }
69
}
70