Skip to content

Commit f1daa74

Browse files
committed
Start with JMSSerializerConverter Command (i need this myself)
1 parent b296e8f commit f1daa74

File tree

4 files changed

+218
-20
lines changed

4 files changed

+218
-20
lines changed
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* SimpleThings FormSerializerBundle
4+
*
5+
* LICENSE
6+
*
7+
* This source file is subject to the MIT license that is bundled
8+
* with this package in the file LICENSE.txt.
9+
* If you did not receive a copy of the license and are unable to
10+
* obtain it through the world-wide-web, please send an email
11+
* to [email protected] so I can send you a copy immediately.
12+
*/
13+
14+
namespace SimpleThings\FormSerializerBundle\Command;
15+
16+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
17+
use Symfony\Component\Console\Input\InputArgument;
18+
use Symfony\Component\Console\Input\InputInterface;
19+
use Symfony\Component\Console\Input\InputOption;
20+
use Symfony\Component\Console\Output\OutputInterface;
21+
22+
use SimpleThings\FormSerializerBundle\Serializer\JMSSerializerConverter;
23+
use Metadata\MetadataFactory;
24+
25+
/**
26+
* Converter JMS Serializer Metadata into Form-Types for Serialization
27+
*/
28+
class ConvertJMSSerializerCommand extends ContainerAwareCommand
29+
{
30+
protected function configure()
31+
{
32+
$this
33+
->setName('simplethings:convert-jms-metadata')
34+
->setDescription('Command helping with conversion of JMS Metadata to Form-Types.')
35+
->addArgument('class', InputArgument::REQUIRED, 'Class Name to convert')
36+
;
37+
}
38+
39+
protected function execute(InputInterface $input, OutputInterface $output)
40+
{
41+
$driver = $this->getContainer()->get('jms_serializer.metadata_driver');
42+
$converter = new JMSSerializerConverter(new MetadataFactory($driver));
43+
44+
$code = $converter->generateFormPhpCode($input->getArgument('class'));
45+
46+
$output->writeln($code);
47+
}
48+
}
49+

Serializer/JMSSerializerConverter.php

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
/**
3+
* SimpleThings FormSerializerBundle
4+
*
5+
* LICENSE
6+
*
7+
* This source file is subject to the MIT license that is bundled
8+
* with this package in the file LICENSE.txt.
9+
* If you did not receive a copy of the license and are unable to
10+
* obtain it through the world-wide-web, please send an email
11+
* to [email protected] so I can send you a copy immediately.
12+
*/
13+
14+
namespace SimpleThings\FormSerializerBundle\Serializer;
15+
16+
use Metadata\MetadataFactoryInterface;
17+
18+
/**
19+
* Converts JMSSerializer Metadata for classes into PHP code for forms.
20+
*/
21+
class JMSSerializerConverter
22+
{
23+
private $metadataFactory;
24+
private $typeMap = array(
25+
'string' => 'text',
26+
'boolean' => 'checkbox',
27+
'integer' => 'integer',
28+
'double' => 'number',
29+
'DateTime' => 'datetime',
30+
);
31+
32+
static private $template = <<<'PHP'
33+
<?php
34+
35+
namespace {{namespace}};
36+
37+
use Symfony\Component\Form\AbstractType;
38+
use Symfony\Component\Form\FormBuilderInterface;
39+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
40+
41+
class {{class}} extends AbstractType
42+
{
43+
public function buildForm(FormBuilderInterface $builder, array $options)
44+
{
45+
$builder
46+
{{build}}
47+
;
48+
}
49+
50+
public function setDefaultOptions(OptionsResolverInterface $resolver)
51+
{
52+
$options->setDefaults(array(
53+
{{options}
54+
));
55+
}
56+
57+
public function getName()
58+
{
59+
return '{{name}}';
60+
}
61+
}
62+
63+
PHP;
64+
65+
public function __construct(MetadataFactoryInterface $factory)
66+
{
67+
$this->metadataFactory = $factory;
68+
}
69+
70+
private function getType($type, $recursive = false)
71+
{
72+
if (isset(self::$typeMap[$type])) {
73+
return self::$typeMap[$type];
74+
} else if (strpos($type, "array<") === 0) {
75+
if ( ! $recursive) {
76+
return 'collection';
77+
}
78+
79+
if (false === $pos = strpos($type, ',', 6)) {
80+
$listType = substr($type, 6, -1);
81+
} else {
82+
$keyType = trim(substr($type, 6, $pos - 6));
83+
$listType = trim(substr($type, $pos+1, -1));
84+
}
85+
86+
return $this->getType($listType);
87+
} else if (class_exists($type)) {
88+
89+
$parts = explode("\\", $type);
90+
return "new " . end($parts) . "Type()";
91+
}
92+
}
93+
94+
public function generateFormPhpCode($className)
95+
{
96+
$metadata = $this->metadataFactory->getMetadataForClass($className);
97+
$lines = array();
98+
99+
$defaults = array(
100+
"'data_class' => '" . $metadata->type . "'";
101+
);
102+
if ($metadata->xmlRootName) {
103+
$efaults[] = "'serialize_xml_name' => '" . $metadata->xmlRootName . "'";
104+
}
105+
106+
$builder = array();
107+
foreach ($metadata->propertyMetadata as $property) {
108+
$options = array();
109+
110+
if ($property->xmlCollection) {
111+
$options[] = "'type' => " . $this->getType($property->type, true);
112+
if ( ! $property->xmlCollectionInline) {
113+
$options[] = "'serialize_xml_inline' => false,";
114+
}
115+
116+
if ($property->xmlEntryName) {
117+
$options[] = "'serialize_xml_name' => '" . $property->xmlEntryName . "',";
118+
}
119+
}
120+
121+
if ($property->xmlAttribute) {
122+
$options[] = "'serialize_xml_attribute' => true";
123+
}
124+
125+
if ($property->xmlValue) {
126+
$options[] = "'serialize_xml_value' => true";
127+
}
128+
129+
if ($property->readOnly) {
130+
$options[] = "'read_only' => true";
131+
}
132+
133+
$options = $options ? ", array(" . implode(", ", $options) . ")" : "";
134+
135+
$type = $this->getType($property->type);
136+
$builder[] = "->add('" . $property->name . "', '" . $type . "'" . $options . ")";
137+
}
138+
139+
// TODO: Replace
140+
return "";
141+
}
142+
}
143+

composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
"symfony/http-foundation": "2.1.*",
1212
"symfony/serializer": "2.1.*"
1313
},
14+
"require-dev": {
15+
"jms/serializer-bundle": "dev-master"
16+
},
1417
"autoload": {
1518
"psr-0": {"SimpleThings\\FormSerializerBundle\\":""}
1619
},

composer.lock

+23-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)