]> git.immae.eu Git - github/wallabag/wallabag.git/blame - vendor/symfony/form/Symfony/Component/Form/FormFactory.php
gitignore vendor
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / FormFactory.php
CommitLineData
4f5b44bd
NL
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
12namespace Symfony\Component\Form;
13
14use Symfony\Component\Form\Exception\UnexpectedTypeException;
15
16class FormFactory implements FormFactoryInterface
17{
18 /**
19 * @var FormRegistryInterface
20 */
21 private $registry;
22
23 /**
24 * @var ResolvedFormTypeFactoryInterface
25 */
26 private $resolvedTypeFactory;
27
28 public function __construct(FormRegistryInterface $registry, ResolvedFormTypeFactoryInterface $resolvedTypeFactory)
29 {
30 $this->registry = $registry;
31 $this->resolvedTypeFactory = $resolvedTypeFactory;
32 }
33
34 /**
35 * {@inheritdoc}
36 */
37 public function create($type = 'form', $data = null, array $options = array())
38 {
39 return $this->createBuilder($type, $data, $options)->getForm();
40 }
41
42 /**
43 * {@inheritdoc}
44 */
45 public function createNamed($name, $type = 'form', $data = null, array $options = array())
46 {
47 return $this->createNamedBuilder($name, $type, $data, $options)->getForm();
48 }
49
50 /**
51 * {@inheritdoc}
52 */
53 public function createForProperty($class, $property, $data = null, array $options = array())
54 {
55 return $this->createBuilderForProperty($class, $property, $data, $options)->getForm();
56 }
57
58 /**
59 * {@inheritdoc}
60 */
61 public function createBuilder($type = 'form', $data = null, array $options = array())
62 {
63 $name = $type instanceof FormTypeInterface || $type instanceof ResolvedFormTypeInterface
64 ? $type->getName()
65 : $type;
66
67 return $this->createNamedBuilder($name, $type, $data, $options);
68 }
69
70 /**
71 * {@inheritdoc}
72 */
73 public function createNamedBuilder($name, $type = 'form', $data = null, array $options = array())
74 {
75 if (null !== $data && !array_key_exists('data', $options)) {
76 $options['data'] = $data;
77 }
78
79 if ($type instanceof FormTypeInterface) {
80 $type = $this->resolveType($type);
81 } elseif (is_string($type)) {
82 $type = $this->registry->getType($type);
83 } elseif (!$type instanceof ResolvedFormTypeInterface) {
84 throw new UnexpectedTypeException($type, 'string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface');
85 }
86
87 return $type->createBuilder($this, $name, $options);
88 }
89
90 /**
91 * {@inheritdoc}
92 */
93 public function createBuilderForProperty($class, $property, $data = null, array $options = array())
94 {
95 if (null === $guesser = $this->registry->getTypeGuesser()) {
96 return $this->createNamedBuilder($property, 'text', $data, $options);
97 }
98
99 $typeGuess = $guesser->guessType($class, $property);
100 $maxLengthGuess = $guesser->guessMaxLength($class, $property);
101 $requiredGuess = $guesser->guessRequired($class, $property);
102 $patternGuess = $guesser->guessPattern($class, $property);
103
104 $type = $typeGuess ? $typeGuess->getType() : 'text';
105
106 $maxLength = $maxLengthGuess ? $maxLengthGuess->getValue() : null;
107 $pattern = $patternGuess ? $patternGuess->getValue() : null;
108
109 if (null !== $pattern) {
110 $options = array_merge(array('pattern' => $pattern), $options);
111 }
112
113 if (null !== $maxLength) {
114 $options = array_merge(array('max_length' => $maxLength), $options);
115 }
116
117 if ($requiredGuess) {
118 $options = array_merge(array('required' => $requiredGuess->getValue()), $options);
119 }
120
121 // user options may override guessed options
122 if ($typeGuess) {
123 $options = array_merge($typeGuess->getOptions(), $options);
124 }
125
126 return $this->createNamedBuilder($property, $type, $data, $options);
127 }
128
129 /**
130 * Wraps a type into a ResolvedFormTypeInterface implementation and connects
131 * it with its parent type.
132 *
133 * @param FormTypeInterface $type The type to resolve.
134 *
135 * @return ResolvedFormTypeInterface The resolved type.
136 */
137 private function resolveType(FormTypeInterface $type)
138 {
139 $parentType = $type->getParent();
140
141 if ($parentType instanceof FormTypeInterface) {
142 $parentType = $this->resolveType($parentType);
143 } elseif (null !== $parentType) {
144 $parentType = $this->registry->getType($parentType);
145 }
146
147 return $this->resolvedTypeFactory->createResolvedType(
148 $type,
149 // Type extensions are not supported for unregistered type instances,
150 // i.e. type instances that are passed to the FormFactory directly,
151 // nor for their parents, if getParent() also returns a type instance.
152 array(),
153 $parentType
154 );
155 }
156}