Completed
Push — master ( 6205c9...57b7f2 )
by Jordi Sala
03:16 queued 12s
created

TypeGuesserChain::guess()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Guesser;
15
16
use Sonata\AdminBundle\Model\ModelManagerInterface;
17
use Symfony\Component\Form\Exception\UnexpectedTypeException;
18
use Symfony\Component\Form\Guess\TypeGuess;
19
20
/**
21
 * The code is based on Symfony2 Form Components.
22
 *
23
 * @final since sonata-project/admin-bundle 3.52
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 * @author Thomas Rabaix <[email protected]>
27
 */
28
class TypeGuesserChain implements TypeGuesserInterface
29
{
30
    /**
31
     * @var array
32
     */
33
    protected $guessers = [];
34
35
    public function __construct(array $guessers)
36
    {
37
        foreach ($guessers as $guesser) {
38
            if (!$guesser instanceof TypeGuesserInterface) {
39
                throw new UnexpectedTypeException($guesser, TypeGuesserInterface::class);
40
            }
41
42
            if ($guesser instanceof self) {
43
                $this->guessers = array_merge($this->guessers, $guesser->guessers);
44
            } else {
45
                $this->guessers[] = $guesser;
46
            }
47
        }
48
    }
49
50
    public function guessType($class, $property, ModelManagerInterface $modelManager)
51
    {
52
        $guesses = [];
53
54
        foreach ($this->guessers as $guesser) {
55
            $guess = $guesser->guessType($class, $property, $modelManager);
56
57
            if (null !== $guess) {
58
                $guesses[] = $guess;
59
            }
60
        }
61
62
        return TypeGuess::getBestGuess($guesses);
63
    }
64
}
65