瀏覽代碼

Quick prototype for Groups feature

Christian Stocker 13 年之前
父節點
當前提交
77ae6eb708

+ 38 - 0
Annotation/Group.php

@@ -0,0 +1,38 @@
+<?php
+
+/*
+ * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
+ *
+ * Licensed under the Apache License, Group 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\SerializerBundle\Annotation;
+
+use JMS\SerializerBundle\Exception\RuntimeException;
+
+/**
+ * @Annotation
+ * @Target("PROPERTY")
+ */
+final class Group
+{
+    public $group;
+
+    public function __construct(array $values)
+    {
+        if (!isset($values['value']) || !is_array($values['value'])) {
+            throw new RuntimeException('$group must be a array.');
+        }
+        $this->group = $values['value'];
+    }
+}

+ 3 - 1
Metadata/Driver/AnnotationDriver.php

@@ -38,6 +38,7 @@ use Metadata\MethodMetadata;
 use Doctrine\Common\Annotations\Reader;
 use JMS\SerializerBundle\Annotation\Type;
 use JMS\SerializerBundle\Annotation\Exclude;
+use JMS\SerializerBundle\Annotation\Group;
 use JMS\SerializerBundle\Annotation\Expose;
 use JMS\SerializerBundle\Annotation\SerializedName;
 use JMS\SerializerBundle\Annotation\Until;
@@ -91,7 +92,6 @@ class AnnotationDriver implements DriverInterface
                 $AccessType = $classAccessType;
                 $accessor = array(null, null);
                 foreach ($this->reader->getPropertyAnnotations($property) as $annot) {
-                    if ($annot instanceof Since) {
                         $propertyMetadata->sinceVersion = $annot->version;
                     } else if ($annot instanceof Until) {
                         $propertyMetadata->untilVersion = $annot->version;
@@ -99,6 +99,8 @@ class AnnotationDriver implements DriverInterface
                         $propertyMetadata->serializedName = $annot->name;
                     } else if ($annot instanceof Expose) {
                         $isExpose = true;
+                    } else if ($annot instanceof Group) {
+                        $propertyMetadata->group = $annot->group;
                     } else if ($annot instanceof Exclude) {
                         $isExclude = true;
                     } else if ($annot instanceof Type) {

+ 3 - 0
Metadata/PropertyMetadata.php

@@ -27,6 +27,7 @@ class PropertyMetadata extends BasePropertyMetadata
 
     public $sinceVersion;
     public $untilVersion;
+    public $group;
     public $serializedName;
     public $type;
     public $xmlCollection = false;
@@ -73,6 +74,7 @@ class PropertyMetadata extends BasePropertyMetadata
         return serialize(array(
             $this->sinceVersion,
             $this->untilVersion,
+            $this->group,
             $this->serializedName,
             $this->type,
             $this->xmlCollection,
@@ -94,6 +96,7 @@ class PropertyMetadata extends BasePropertyMetadata
         list(
             $this->sinceVersion,
             $this->untilVersion,
+            $this->group,
             $this->serializedName,
             $this->type,
             $this->xmlCollection,

+ 57 - 0
Serializer/Exclusion/GroupExclusionStrategy.php

@@ -0,0 +1,57 @@
+<?php
+
+/*
+ * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
+ *
+ * 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\SerializerBundle\Serializer\Exclusion;
+
+use JMS\SerializerBundle\Metadata\ClassMetadata;
+use JMS\SerializerBundle\Metadata\PropertyMetadata;
+
+class GroupExclusionStrategy implements ExclusionStrategyInterface
+{
+    private $group;
+
+    public function __construct($group)
+    {
+        if (!is_array($group)) {
+            $group = array($group);
+        }
+        $this->group = $group;
+    }
+
+    public function shouldSkipClass(ClassMetadata $metadata)
+    {
+        return false;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function shouldSkipProperty(PropertyMetadata $property)
+    {
+        if ($this->group && $property->group) {
+            foreach($this->group as $group) {
+                if (in_array($group, $property->group)) {
+                    return false;
+                }
+            }
+        }
+            
+        return true;
+        
+    }
+}

+ 12 - 0
Serializer/Serializer.php

@@ -22,6 +22,7 @@ use JMS\SerializerBundle\Exception\UnsupportedFormatException;
 use Metadata\MetadataFactoryInterface;
 use JMS\SerializerBundle\Exception\InvalidArgumentException;
 use JMS\SerializerBundle\Serializer\Exclusion\VersionExclusionStrategy;
+use JMS\SerializerBundle\Serializer\Exclusion\GroupExclusionStrategy;
 use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyInterface;
 
 class Serializer implements SerializerInterface
@@ -53,6 +54,17 @@ class Serializer implements SerializerInterface
 
         $this->exclusionStrategy = new VersionExclusionStrategy($version);
     }
+    
+    public function setGroup($group)
+    {
+        if (null === $group) {
+            $this->group = null;
+
+            return;
+        }
+
+        $this->exclusionStrategy = new GroupExclusionStrategy($group);
+    }
 
     public function serialize($data, $format)
     {