-
-
Notifications
You must be signed in to change notification settings - Fork 311
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
Add @Virtual annotation #109
Changes from all commits
38912e0
f72bc8e
896bfd9
ab5cc80
e90b0a4
636fd3a
ffa0d20
8207823
3af5c86
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 |
---|---|---|
|
@@ -22,7 +22,7 @@ | |
|
||
/** | ||
* @Annotation | ||
* @Target("PROPERTY") | ||
* @Target({"PROPERTY","METHOD"}) | ||
*/ | ||
final class Groups | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
|
||
/** | ||
* @Annotation | ||
* @Target({"PROPERTY"}) | ||
* @Target({"PROPERTY","METHOD"}) | ||
*/ | ||
final class Inline | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace JMS\SerializerBundle\Annotation; | ||
|
||
/** | ||
* @Annotation | ||
* @Target("METHOD") | ||
* | ||
* @author Alexander Klimenkov <alx.devel@gmail.com> | ||
*/ | ||
final class VirtualProperty | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
|
||
/** | ||
* @Annotation | ||
* @Target("PROPERTY") | ||
* @Target({"PROPERTY","METHOD"}) | ||
*/ | ||
final class XmlValue | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,32 +138,32 @@ public function unserialize($str) | |
|
||
private function sortProperties() | ||
{ | ||
switch ($this->accessorOrder) { | ||
case self::ACCESSOR_ORDER_ALPHABETICAL: | ||
ksort($this->propertyMetadata); | ||
break; | ||
case self::ACCESSOR_ORDER_CUSTOM: | ||
switch ($this->accessorOrder) { | ||
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 has been changed here? whitespace? |
||
case self::ACCESSOR_ORDER_ALPHABETICAL: | ||
ksort($this->propertyMetadata); | ||
break; | ||
|
||
case self::ACCESSOR_ORDER_CUSTOM: | ||
$order = $this->customOrder; | ||
uksort($this->propertyMetadata, function($a, $b) use ($order) { | ||
$existsA = isset($order[$a]); | ||
$existsB = isset($order[$b]); | ||
|
||
if (!$existsA && !$existsB) { | ||
return 0; | ||
} | ||
|
||
if (!$existsA) { | ||
return 1; | ||
} | ||
|
||
if (!$existsB) { | ||
return -1; | ||
$existsA = isset($order[$a]); | ||
$existsB = isset($order[$b]); | ||
|
||
if (!$existsA && !$existsB) { | ||
return 0; | ||
} | ||
|
||
if (!$existsA) { | ||
return 1; | ||
} | ||
|
||
return $order[$a] < $order[$b] ? -1 : 1; | ||
if (!$existsB) { | ||
return -1; | ||
} | ||
|
||
return $order[$a] < $order[$b] ? -1 : 1; | ||
}); | ||
break; | ||
} | ||
break; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
use JMS\SerializerBundle\Annotation\PostSerialize; | ||
use JMS\SerializerBundle\Annotation\PostDeserialize; | ||
use JMS\SerializerBundle\Annotation\PreSerialize; | ||
use JMS\SerializerBundle\Annotation\VirtualProperty; | ||
use Metadata\MethodMetadata; | ||
use Doctrine\Common\Annotations\Reader; | ||
use JMS\SerializerBundle\Annotation\Type; | ||
|
@@ -46,6 +47,7 @@ | |
use JMS\SerializerBundle\Annotation\ReadOnly; | ||
use JMS\SerializerBundle\Metadata\ClassMetadata; | ||
use JMS\SerializerBundle\Metadata\PropertyMetadata; | ||
use JMS\SerializerBundle\Metadata\VirtualPropertyMetadata; | ||
use Metadata\Driver\DriverInterface; | ||
|
||
class AnnotationDriver implements DriverInterface | ||
|
@@ -62,6 +64,9 @@ public function loadMetadataForClass(\ReflectionClass $class) | |
$classMetadata = new ClassMetadata($name = $class->getName()); | ||
$classMetadata->fileResources[] = $class->getFilename(); | ||
|
||
$propertiesMetadata = array(); | ||
$propertiesAnnotations = array(); | ||
|
||
$exclusionPolicy = 'NONE'; | ||
$excludeAll = false; | ||
$classAccessType = PropertyMetadata::ACCESS_TYPE_PROPERTY; | ||
|
@@ -79,17 +84,50 @@ public function loadMetadataForClass(\ReflectionClass $class) | |
} | ||
} | ||
|
||
foreach ($class->getMethods() as $method) { | ||
if ($method->getDeclaringClass()->getName() !== $name) { | ||
continue; | ||
} | ||
|
||
$methodAnnotations = $this->reader->getMethodAnnotations($method); | ||
|
||
foreach ($methodAnnotations as $annot) { | ||
if ($annot instanceof PreSerialize) { | ||
$classMetadata->addPreSerializeMethod(new MethodMetadata($name, $method->getName())); | ||
continue 2; | ||
} else if ($annot instanceof PostDeserialize) { | ||
$classMetadata->addPostDeserializeMethod(new MethodMetadata($name, $method->getName())); | ||
continue 2; | ||
} else if ($annot instanceof PostSerialize) { | ||
$classMetadata->addPostSerializeMethod(new MethodMetadata($name, $method->getName())); | ||
continue 2; | ||
} else if ($annot instanceof VirtualProperty) { | ||
$virtualPropertyMetadata = new VirtualPropertyMetadata($name, $method->getName()); | ||
$propertiesMetadata[] = $virtualPropertyMetadata; | ||
$propertiesAnnotations[] = $methodAnnotations; | ||
continue 2; | ||
} | ||
} | ||
} | ||
|
||
if (!$excludeAll) { | ||
foreach ($class->getProperties() as $property) { | ||
if ($property->getDeclaringClass()->getName() !== $name) { | ||
continue; | ||
} | ||
$propertiesMetadata[] = new PropertyMetadata($name, $property->getName()); | ||
$propertiesAnnotations[] = $this->reader->getPropertyAnnotations($property); | ||
} | ||
|
||
foreach ($propertiesMetadata as $propertyKey => $propertyMetadata) { | ||
|
||
$propertyMetadata = new PropertyMetadata($name, $property->getName()); | ||
$isExclude = $isExpose = false; | ||
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. $isExclude and $isExpose only apply to the real properties, we should probably move them into the loop above. 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 don't sure, that this a good idea. If we do this, we will must to split loop over $propertyAnnotations on two parts: first for "real" properties and second - for virtual. IMHO, it's bad. |
||
$AccessType = $classAccessType; | ||
$accessor = array(null, null); | ||
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. Same for $accessor. |
||
foreach ($this->reader->getPropertyAnnotations($property) as $annot) { | ||
|
||
$propertyAnnotations = $propertiesAnnotations[$propertyKey]; | ||
|
||
foreach ($propertyAnnotations as $annot) { | ||
if ($annot instanceof Since) { | ||
$propertyMetadata->sinceVersion = $annot->version; | ||
} else if ($annot instanceof Until) { | ||
|
@@ -137,25 +175,6 @@ public function loadMetadataForClass(\ReflectionClass $class) | |
} | ||
} | ||
|
||
foreach ($class->getMethods() as $method) { | ||
if ($method->getDeclaringClass()->getName() !== $name) { | ||
continue; | ||
} | ||
|
||
foreach ($this->reader->getMethodAnnotations($method) as $annot) { | ||
if ($annot instanceof PreSerialize) { | ||
$classMetadata->addPreSerializeMethod(new MethodMetadata($name, $method->getName())); | ||
continue 2; | ||
} else if ($annot instanceof PostDeserialize) { | ||
$classMetadata->addPostDeserializeMethod(new MethodMetadata($name, $method->getName())); | ||
continue 2; | ||
} else if ($annot instanceof PostSerialize) { | ||
$classMetadata->addPostSerializeMethod(new MethodMetadata($name, $method->getName())); | ||
continue 2; | ||
} | ||
} | ||
} | ||
|
||
return $classMetadata; | ||
} | ||
} |
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.
Have you reviewed all the other annotations? Whether they make sense on methods?
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.
Now supported annotations Groups, SerializedName, Version, XmlAttribute, XmlList, Inline, Since, Until, VirtualProperty, XmlMap, XmlValue. For other annotations it's don't need