1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Knp\RadBundle\Form; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
6
|
|
|
use Symfony\Component\Form\FormEvent;; |
|
|
|
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
8
|
|
|
use Symfony\Component\Form\FormEvents; |
9
|
|
|
use Knp\RadBundle\Reflection\ClassMetadataFetcher; |
10
|
|
|
use Doctrine\Common\Inflector\Inflector; |
11
|
|
|
|
12
|
|
|
class DefaultFormCreator implements FormCreatorInterface, EventSubscriberInterface |
13
|
|
|
{ |
14
|
|
|
private $fetcher; |
15
|
|
|
private $factory; |
16
|
|
|
private $dataTypeGuesser; |
17
|
|
|
|
18
|
|
|
public function __construct(ClassMetadataFetcher $fetcher = null, FormFactoryInterface $factory, DataTypeGuesser $dataTypeGuesser) |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
$this->fetcher = $fetcher ?: new ClassMetadataFetcher; |
21
|
|
|
$this->factory = $factory; |
22
|
|
|
$this->dataTypeGuesser = $dataTypeGuesser; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public static function getSubscribedEvents() |
26
|
|
|
{ |
27
|
|
|
return array( |
28
|
|
|
FormEvents::PRE_SET_DATA => 'preSetData' |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function create($object, $purpose = null, array $options = array()) |
33
|
|
|
{ |
34
|
|
|
$builder = $this->factory->createBuilder('form', $object, $options); |
35
|
|
|
$builder->addEventSubscriber($this); |
36
|
|
|
|
37
|
|
|
return $builder->getForm(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function preSetData(FormEvent $event) |
41
|
|
|
{ |
42
|
|
|
$object = $event->getData(); |
43
|
|
|
$form = $event->getForm(); |
44
|
|
|
|
45
|
|
|
if (!is_object($object)) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->dataTypeGuesser->setData($object); |
50
|
|
|
|
51
|
|
|
foreach ($this->fetcher->getMethods($object) as $method) { |
52
|
|
|
if (0 === strpos($method, 'get') || 0 === strpos($method, 'is')) { |
53
|
|
|
$propertyName = $this->extractPropertyName($method); |
54
|
|
|
if ($this->hasRelatedSetter($object, $propertyName)) { |
55
|
|
|
$form->add($this->factory->createForProperty(get_class($object), $propertyName, null, array( |
56
|
|
|
'auto_initialize' => false, |
57
|
|
|
))); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
foreach ($this->fetcher->getProperties($object) as $property) { |
63
|
|
|
$form->add($this->factory->createForProperty(get_class($object), $property, null, array( |
64
|
|
|
'auto_initialize' => false, |
65
|
|
|
))); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function extractPropertyName($methodName) |
70
|
|
|
{ |
71
|
|
|
return lcfirst(preg_replace('#is|get#', '', $methodName)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function hasRelatedSetter($object, $propertyName) |
75
|
|
|
{ |
76
|
|
|
return $this->fetcher->hasMethod($object, 'set'.Inflector::classify($propertyName)); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.