-
Notifications
You must be signed in to change notification settings - Fork 18
[WIP] JMS Serializer Bundle Integration #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
a81d0cb
fb2ec07
036e78f
f8880ad
59f1d85
53d0c71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
/** | ||
* SimpleThings FormSerializerBundle | ||
* | ||
* LICENSE | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this package in the file LICENSE.txt. | ||
* If you did not receive a copy of the license and are unable to | ||
* obtain it through the world-wide-web, please send an email | ||
* to [email protected] so I can send you a copy immediately. | ||
*/ | ||
|
||
namespace SimpleThings\FormSerializerBundle\Annotation; | ||
|
||
/** | ||
* Register form type that is responsible for serializing the annotated entity. | ||
* | ||
* @Annotation | ||
* @Target("CLASS") | ||
*/ | ||
class FormType | ||
{ | ||
/** | ||
* Class or form-type name that is registered in Form Factory. | ||
* | ||
* @var string | ||
*/ | ||
private $type; | ||
|
||
public function __construct(array $values) | ||
{ | ||
if (isset($values['value'])) { | ||
$this->type = $values['value']; | ||
} | ||
} | ||
|
||
public function getType() | ||
{ | ||
return $this->type; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace SimpleThings\FormSerializerBundle\Serializer\JMS; | ||
|
||
use JMS\SerializerBundle\Metadata\ClassMetadata; | ||
use JMS\SerializerBundle\Metadata\PropertyMetadata; | ||
use JMS\SerializerBundle\Metadata\VirtualPropertyMetadata; | ||
|
||
use SimpleThings\FormSerializerBundle\Annotation\FormType; | ||
|
||
use Metadata\Driver\DriverInterface; | ||
use Symfony\Component\Form\FormFactoryInterface; | ||
use Doctrine\Common\Annotations\Reader; | ||
|
||
class FormMetadataDriver implements DriverInterface | ||
{ | ||
private $reader; | ||
private $formFactory; | ||
|
||
public function __construct(Reader $reader, FormFactoryInterface $formFactory) | ||
{ | ||
$this->reader = $reader; | ||
$this->formFactory = $formFactory; | ||
} | ||
|
||
public function loadMetadataForClass(\ReflectionClass $class) | ||
{ | ||
$classMetadata = new ClassMetadata($name = $class->getName()); | ||
|
||
foreach ($this->reader->getClassAnnotations($class) as $annot) { | ||
if ($annot instanceof FormType) { | ||
$type = $annot->getType(); | ||
|
||
if (class_exists($type)) { | ||
$type = new $type; | ||
} | ||
|
||
$form = $this->formFactory->create($type, null, array()); | ||
$options = $form->getConfig()->getOptions(); | ||
|
||
$classMetadata->xmlRootName = $options['serialize_xml_name']; | ||
|
||
$propertiesMetadata = array(); | ||
foreach ($form->getChildren() as $children) { | ||
$childOptions = $children->getConfig()->getOptions(); | ||
$type = $children->getConfig()->getType(); | ||
|
||
$property = $class->getProperty($children->getName()); | ||
$propertyMetadata = new PropertyMetadata($name, $property->getName()); | ||
#$propertyMetadata->setAccessor('public_method', null, null); | ||
|
||
if (!empty($childOptions['serialize_name'])) { | ||
$propertyMetadata->serializedName = $childOptions['serialize_name']; | ||
} | ||
|
||
if ($type->getName() == "collection") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about child types of the collection type ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you know of any? The problem is that detection of custom form types is very hard in this context and the collection type is very special. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a custom type extending collection in one of my projects (not the project for which I could use this bundle soon though) |
||
$propertyMetadata->xmlCollection = true; | ||
$propertyMetadata->xmlCollectionInline = $childOptions['serialize_xml_inline']; | ||
|
||
if ( ! empty($childOptions['serialize_xml_name'])) { | ||
$propertyMetadata->xmlEntryName = $childOptions['serialize_xml_name']; | ||
} | ||
|
||
$subForm = $this->formFactory->create($childOptions['type']); | ||
|
||
$propertyMetadata->type = sprintf('array<%s>', $this->translateType($subForm)); | ||
} else if ($type->getName() == "choice") { | ||
$propertyMetadata->type = $childOptions['multiple'] ? "array<string>" : "string"; | ||
} else if ($type->getName() == "entity") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exactly the same here. you consider There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. btw, this even concerns some core types: |
||
$propertyMetadata->type = $childOptions['multiple'] ? "array<string>" : "string"; | ||
} else { | ||
$propertyMetadata->type = $this->translateType($children); | ||
} | ||
|
||
if ($childOptions['serialize_xml_attribute']) { | ||
$propertyMetadata->xmlAttribute = true; | ||
} else if ($childOptions['serialize_xml_value']) { | ||
$propertyMetadata->xmlValue = true; | ||
} | ||
|
||
if ($childOptions['disabled']) { | ||
$propertyMetadata->readOnly = true; | ||
} | ||
|
||
$classMetadata->addPropertyMetadata($propertyMetadata); | ||
} | ||
} | ||
} | ||
|
||
return $classMetadata; | ||
} | ||
|
||
private function translateType($form) | ||
{ | ||
$options = $form->getConfig()->getOptions(); | ||
if ($options['data_class']) { | ||
return $options['data_class']; | ||
} | ||
|
||
switch ($form->getConfig()->getType()->getName()) { | ||
case 'date': | ||
case 'datetime': | ||
case 'time': | ||
case 'birthday'; | ||
return 'DateTime'; | ||
case 'number': | ||
return 'double'; | ||
case 'checkbox': | ||
return 'boolean'; | ||
case 'integer'; | ||
return 'integer'; | ||
default: | ||
return 'string'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about the integer type ? |
||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace SimpleThings\FormSerializerBundle\Tests\Serializer; | ||
|
||
use SimpleThings\FormSerializerBundle\Tests\TestCase; | ||
use SimpleThings\FormSerializerBundle\Serializer\JMS\FormMetadataDriver; | ||
|
||
use SimpleThings\FormSerializerBundle\Tests\Serializer\Fixture\User; | ||
use SimpleThings\FormSerializerBundle\Tests\Serializer\Fixture\Address; | ||
use SimpleThings\FormSerializerBundle\Tests\Serializer\Fixture\UserType; | ||
use SimpleThings\FormSerializerBundle\Tests\Serializer\Fixture\AddressType; | ||
|
||
class JmsFormMetadataDriverTest extends TestCase | ||
{ | ||
public function testLoadMetadata() | ||
{ | ||
$reader = new \Doctrine\Common\Annotations\AnnotationReader(); | ||
$driver = new FormMetadataDriver($reader, $this->createFormFactory()); | ||
|
||
$reflClass = new \ReflectionClass('SimpleThings\FormSerializerBundle\Tests\Serializer\Fixture\User'); | ||
$metadata = $driver->loadMetadataForClass($reflClass); | ||
|
||
$this->assertInstanceOf('JMS\SerializerBundle\Metadata\ClassMetadata', $metadata); | ||
$this->assertEquals(array('username', 'email', 'birthday', 'country', 'address', 'addresses'), array_keys($metadata->propertyMetadata)); | ||
} | ||
|
||
public function testSerialize() | ||
{ | ||
$serializer = $this->createJmsSerializer(true); | ||
|
||
$address = new Address(); | ||
$address->street = "Somestreet 1"; | ||
$address->zipCode = 12345; | ||
$address->city = "Bonn"; | ||
|
||
$user = new User(); | ||
$user->username = "beberlei"; | ||
$user->email = "[email protected]"; | ||
$user->birthday = new \DateTime("1984-03-18"); | ||
$user->gender = 'male'; | ||
$user->interests = array('sport', 'reading'); | ||
$user->country = "DE"; | ||
$user->address = $address; | ||
$user->addresses = array($address, $address); | ||
|
||
$xml = $serializer->serialize($user, 'xml'); | ||
|
||
$this->assertEquals(<<<XML | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<user> | ||
<username><![CDATA[beberlei]]></username> | ||
<email><![CDATA[[email protected]]]></email> | ||
<birthday>1984-03-18T00:00:00+0100</birthday> | ||
<country><![CDATA[DE]]></country> | ||
<address street="Somestreet 1" zip_code="12345" city="Bonn"/> | ||
<addresses> | ||
<address street="Somestreet 1" zip_code="12345" city="Bonn"/> | ||
<address street="Somestreet 1" zip_code="12345" city="Bonn"/> | ||
</addresses> | ||
</user> | ||
|
||
XML | ||
, $xml); | ||
|
||
$json = $serializer->serialize($user, 'json'); | ||
|
||
$this->assertEquals(<<<JSON | ||
{"username":"beberlei","email":"[email protected]","birthday":"1984-03-18T00:00:00+0100","country":"DE","address":{"street":"Somestreet 1","zip_code":12345,"city":"Bonn"},"addresses":[{"street":"Somestreet 1","zip_code":12345,"city":"Bonn"},{"street":"Somestreet 1","zip_code":12345,"city":"Bonn"}]} | ||
JSON | ||
, $json); | ||
} | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is this ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the form component uses getter/setters to access properties, jms serializer uses reflection by default. The line was an attempt to synchronize behavior, however jms serailizer doesnt detect public properties and therefore fails when using "public_method" with my test objects.