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