4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form
;
14 use Symfony\Component\Form\Guess\Guess
;
15 use Symfony\Component\Form\Exception\UnexpectedTypeException
;
17 class FormTypeGuesserChain
implements FormTypeGuesserInterface
19 private $guessers = array();
24 * @param array $guessers Guessers as instances of FormTypeGuesserInterface
26 * @throws UnexpectedTypeException if any guesser does not implement FormTypeGuesserInterface
28 public function __construct(array $guessers)
30 foreach ($guessers as $guesser) {
31 if (!$guesser instanceof FormTypeGuesserInterface
) {
32 throw new UnexpectedTypeException($guesser, 'Symfony\Component\Form\FormTypeGuesserInterface');
35 if ($guesser instanceof self
) {
36 $this->guessers
= array_merge($this->guessers
, $guesser->guessers
);
38 $this->guessers
[] = $guesser;
46 public function guessType($class, $property)
48 return $this->guess(function ($guesser) use ($class, $property) {
49 return $guesser->guessType($class, $property);
56 public function guessRequired($class, $property)
58 return $this->guess(function ($guesser) use ($class, $property) {
59 return $guesser->guessRequired($class, $property);
66 public function guessMaxLength($class, $property)
68 return $this->guess(function ($guesser) use ($class, $property) {
69 return $guesser->guessMaxLength($class, $property);
76 public function guessPattern($class, $property)
78 return $this->guess(function ($guesser) use ($class, $property) {
79 return $guesser->guessPattern($class, $property);
84 * Executes a closure for each guesser and returns the best guess from the
87 * @param \Closure $closure The closure to execute. Accepts a guesser
88 * as argument and should return a Guess instance
90 * @return Guess The guess with the highest confidence
92 private function guess(\Closure
$closure)
96 foreach ($this->guessers
as $guesser) {
97 if ($guess = $closure($guesser)) {
102 return Guess
::getBestGuess($guesses);