From 4f5b44bd3bd490309eb2ba7b44df4769816ba729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Sat, 3 Aug 2013 19:26:54 +0200 Subject: twig implementation --- .../Component/Form/FormTypeGuesserChain.php | 104 +++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 vendor/symfony/form/Symfony/Component/Form/FormTypeGuesserChain.php (limited to 'vendor/symfony/form/Symfony/Component/Form/FormTypeGuesserChain.php') diff --git a/vendor/symfony/form/Symfony/Component/Form/FormTypeGuesserChain.php b/vendor/symfony/form/Symfony/Component/Form/FormTypeGuesserChain.php new file mode 100644 index 00000000..c7f8ece3 --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/FormTypeGuesserChain.php @@ -0,0 +1,104 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form; + +use Symfony\Component\Form\Guess\Guess; +use Symfony\Component\Form\Exception\UnexpectedTypeException; + +class FormTypeGuesserChain implements FormTypeGuesserInterface +{ + private $guessers = array(); + + /** + * Constructor. + * + * @param array $guessers Guessers as instances of FormTypeGuesserInterface + * + * @throws UnexpectedTypeException if any guesser does not implement FormTypeGuesserInterface + */ + public function __construct(array $guessers) + { + foreach ($guessers as $guesser) { + if (!$guesser instanceof FormTypeGuesserInterface) { + throw new UnexpectedTypeException($guesser, 'Symfony\Component\Form\FormTypeGuesserInterface'); + } + + if ($guesser instanceof self) { + $this->guessers = array_merge($this->guessers, $guesser->guessers); + } else { + $this->guessers[] = $guesser; + } + } + } + + /** + * {@inheritDoc} + */ + public function guessType($class, $property) + { + return $this->guess(function ($guesser) use ($class, $property) { + return $guesser->guessType($class, $property); + }); + } + + /** + * {@inheritDoc} + */ + public function guessRequired($class, $property) + { + return $this->guess(function ($guesser) use ($class, $property) { + return $guesser->guessRequired($class, $property); + }); + } + + /** + * {@inheritDoc} + */ + public function guessMaxLength($class, $property) + { + return $this->guess(function ($guesser) use ($class, $property) { + return $guesser->guessMaxLength($class, $property); + }); + } + + /** + * {@inheritDoc} + */ + public function guessPattern($class, $property) + { + return $this->guess(function ($guesser) use ($class, $property) { + return $guesser->guessPattern($class, $property); + }); + } + + /** + * Executes a closure for each guesser and returns the best guess from the + * return values + * + * @param \Closure $closure The closure to execute. Accepts a guesser + * as argument and should return a Guess instance + * + * @return Guess The guess with the highest confidence + */ + private function guess(\Closure $closure) + { + $guesses = array(); + + foreach ($this->guessers as $guesser) { + if ($guess = $closure($guesser)) { + $guesses[] = $guess; + } + } + + return Guess::getBestGuess($guesses); + } +} -- cgit v1.2.3