|
| 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 | + |
0 commit comments