-
-
Notifications
You must be signed in to change notification settings - Fork 585
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
added support for xml-attributes as discriminators #692
Changes from all commits
14fadd2
9ba0de2
5d63f75
8d4548d
23348ad
a1f4e04
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,37 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright 2016 Björn Bösel <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace JMS\Serializer\Annotation; | ||
|
||
|
||
/** | ||
* @Annotation | ||
* @Target("CLASS") | ||
*/ | ||
class XmlDiscriminator | ||
{ | ||
/** | ||
* @var boolean | ||
*/ | ||
public $attribute = false; | ||
|
||
/** | ||
* @var boolean | ||
*/ | ||
public $cdata = true; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,9 @@ class ClassMetadata extends MergeableClassMetadata | |
public $discriminatorMap = array(); | ||
public $discriminatorGroups = array(); | ||
|
||
public $xmlDiscriminatorAttribute = false; | ||
public $xmlDiscriminatorCData = true; | ||
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. Should it be 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. capitalizing CD follows the convention used in PropertyMetadata::$xmlElementCData ... |
||
|
||
public function setDiscriminator($fieldName, array $map, array $groups = array()) | ||
{ | ||
if (empty($fieldName)) { | ||
|
@@ -203,6 +206,7 @@ public function merge(MergeableInterface $object) | |
$this->discriminatorGroups | ||
); | ||
$discriminatorProperty->serializedName = $this->discriminatorFieldName; | ||
$discriminatorProperty->xmlAttribute = $this->xmlDiscriminatorAttribute; | ||
$this->propertyMetadata[$this->discriminatorFieldName] = $discriminatorProperty; | ||
} | ||
|
||
|
@@ -249,6 +253,8 @@ public function serialize() | |
$this->discriminatorGroups, | ||
parent::serialize(), | ||
'discriminatorGroups' => $this->discriminatorGroups, | ||
'xmlDiscriminatorAttribute' => $this->xmlDiscriminatorAttribute, | ||
'xmlDiscriminatorCData' => $this->xmlDiscriminatorCData, | ||
)); | ||
} | ||
|
||
|
@@ -279,6 +285,14 @@ public function unserialize($str) | |
$this->discriminatorGroups = $deserializedData['discriminatorGroups']; | ||
} | ||
|
||
if (isset($deserializedData['xmlDiscriminatorAttribute'])) { | ||
$this->xmlDiscriminatorAttribute = $deserializedData['xmlDiscriminatorAttribute']; | ||
} | ||
|
||
if (isset($deserializedData['xmlDiscriminatorCData'])) { | ||
$this->xmlDiscriminatorCData = $deserializedData['xmlDiscriminatorCData']; | ||
} | ||
|
||
parent::unserialize($parentStr); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -278,6 +278,16 @@ private function addClassProperties(ClassMetadata $metadata, array $config) | |
} | ||
$groups = isset($config['discriminator']['groups']) ? $config['discriminator']['groups'] : array(); | ||
$metadata->setDiscriminator($config['discriminator']['field_name'], $config['discriminator']['map'], $groups); | ||
|
||
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. PSR standards, max one empty line |
||
if (isset($config['discriminator']['xml_attribute'])) { | ||
$metadata->xmlDiscriminatorAttribute = (bool) $config['discriminator']['xml_attribute']; | ||
} | ||
if (isset($config['discriminator']['xml_element'])) { | ||
if (isset($config['discriminator']['xml_element']['cdata'])) { | ||
$metadata->xmlDiscriminatorCData = (bool) $config['discriminator']['xml_element']['cdata']; | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright 2016 Björn Bösel <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace JMS\Serializer\Tests\Fixtures\Discriminator; | ||
|
||
class ObjectWithXmlAttributeDiscriminatorChild extends ObjectWithXmlAttributeDiscriminatorParent | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright 2016 Björn Bösel <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace JMS\Serializer\Tests\Fixtures\Discriminator; | ||
|
||
use JMS\Serializer\Annotation as Serializer; | ||
|
||
/** | ||
* @Serializer\Discriminator(field = "type", map = { | ||
* "child": "JMS\Serializer\Tests\Fixtures\Discriminator\ObjectWithXmlAttributeDiscriminatorChild" | ||
* }) | ||
* @Serializer\XmlDiscriminator(attribute=true, cdata=false) | ||
*/ | ||
class ObjectWithXmlAttributeDiscriminatorParent | ||
{ | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
use JMS\Serializer\Metadata\ClassMetadata; | ||
use JMS\Serializer\Metadata\PropertyMetadata; | ||
|
||
$metadata = new ClassMetadata('JMS\Serializer\Tests\Fixtures\Discriminator\ObjectWithXmlAttributeDiscriminatorParent'); | ||
$metadata->setDiscriminator('type', array( | ||
'child' => 'JMS\Serializer\Tests\Fixtures\Discriminator\ObjectWithXmlAttributeDiscriminatorChild' | ||
)); | ||
$metadata->xmlDiscriminatorAttribute = true; | ||
$metadata->xmlDiscriminatorCData = false; | ||
return $metadata; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<serializer> | ||
<class name="JMS\Serializer\Tests\Fixtures\Discriminator\ObjectWithXmlAttributeDiscriminatorParent" | ||
discriminator-field-name="type" | ||
> | ||
<discriminator-class value="child">JMS\Serializer\Tests\Fixtures\Discriminator\ObjectWithXmlAttributeDiscriminatorChild</discriminator-class> | ||
<xml-discriminator attribute="true" cdata="false" /> | ||
</class> | ||
</serializer> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
JMS\Serializer\Tests\Fixtures\Discriminator\ObjectWithXmlAttributeDiscriminatorParent: | ||
discriminator: | ||
field_name: type | ||
map: | ||
child: JMS\Serializer\Tests\Fixtures\Discriminator\ObjectWithXmlAttributeDiscriminatorChild | ||
xml_attribute: true | ||
xml_element: | ||
cdata: false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<result type="child"/> |
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.
i will put this statement before the previous one