vendor/symfony/symfony/src/Symfony/Component/Form/FormTypeGuesserChain.php line 52

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Form;
  11. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  12. use Symfony\Component\Form\Guess\Guess;
  13. class FormTypeGuesserChain implements FormTypeGuesserInterface
  14. {
  15.     private $guessers = [];
  16.     /**
  17.      * @param FormTypeGuesserInterface[] $guessers
  18.      *
  19.      * @throws UnexpectedTypeException if any guesser does not implement FormTypeGuesserInterface
  20.      */
  21.     public function __construct($guessers)
  22.     {
  23.         if (!\is_array($guessers) && !$guessers instanceof \Traversable) {
  24.             throw new UnexpectedTypeException($guessers'array or Traversable');
  25.         }
  26.         foreach ($guessers as $guesser) {
  27.             if (!$guesser instanceof FormTypeGuesserInterface) {
  28.                 throw new UnexpectedTypeException($guesser'Symfony\Component\Form\FormTypeGuesserInterface');
  29.             }
  30.             if ($guesser instanceof self) {
  31.                 $this->guessers array_merge($this->guessers$guesser->guessers);
  32.             } else {
  33.                 $this->guessers[] = $guesser;
  34.             }
  35.         }
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      */
  40.     public function guessType($class$property)
  41.     {
  42.         return $this->guess(function ($guesser) use ($class$property) {
  43.             return $guesser->guessType($class$property);
  44.         });
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     public function guessRequired($class$property)
  50.     {
  51.         return $this->guess(function ($guesser) use ($class$property) {
  52.             return $guesser->guessRequired($class$property);
  53.         });
  54.     }
  55.     /**
  56.      * {@inheritdoc}
  57.      */
  58.     public function guessMaxLength($class$property)
  59.     {
  60.         return $this->guess(function ($guesser) use ($class$property) {
  61.             return $guesser->guessMaxLength($class$property);
  62.         });
  63.     }
  64.     /**
  65.      * {@inheritdoc}
  66.      */
  67.     public function guessPattern($class$property)
  68.     {
  69.         return $this->guess(function ($guesser) use ($class$property) {
  70.             return $guesser->guessPattern($class$property);
  71.         });
  72.     }
  73.     /**
  74.      * Executes a closure for each guesser and returns the best guess from the
  75.      * return values.
  76.      *
  77.      * @param \Closure $closure The closure to execute. Accepts a guesser
  78.      *                          as argument and should return a Guess instance
  79.      *
  80.      * @return Guess|null The guess with the highest confidence
  81.      */
  82.     private function guess(\Closure $closure)
  83.     {
  84.         $guesses = [];
  85.         foreach ($this->guessers as $guesser) {
  86.             if ($guess $closure($guesser)) {
  87.                 $guesses[] = $guess;
  88.             }
  89.         }
  90.         return Guess::getBestGuess($guesses);
  91.     }
  92. }