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