Browse Source

added "Default" group to all properties

Properties that do not have an explicit group set will now automatically
have the "Default" group.
Johannes Schmitt 12 years ago
parent
commit
6f020a7046

+ 5 - 3
Serializer/Exclusion/GroupsExclusionStrategy.php

@@ -24,12 +24,14 @@ use JMS\SerializerBundle\Exception\RuntimeException;
 
 class GroupsExclusionStrategy implements ExclusionStrategyInterface
 {
+    const DEFAULT_GROUP = 'Default';
+
     private $groups = array();
 
     public function __construct(array $groups)
     {
         if (empty($groups)) {
-            throw new RuntimeException('Empty group array may not be configured for GroupsExclusionStrategy');
+            $groups = array(self::DEFAULT_GROUP);
         }
 
         foreach ($groups as $group) {
@@ -47,8 +49,8 @@ class GroupsExclusionStrategy implements ExclusionStrategyInterface
      */
     public function shouldSkipProperty(PropertyMetadata $property, $object = null)
     {
-        if (!$property->groups) {
-            return true;
+        if ( ! $property->groups) {
+            return ! isset($this->groups[self::DEFAULT_GROUP]);
         }
 
         foreach ($property->groups as $group) {

+ 1 - 1
Serializer/Serializer.php

@@ -70,7 +70,7 @@ class Serializer implements SerializerInterface
 
     public function setGroups($groups)
     {
-        if (!$groups) {
+        if ( ! $groups) {
             $this->exclusionStrategy = null;
 
             return;

+ 1 - 1
Tests/Serializer/Fixture/Article.php

@@ -1,6 +1,6 @@
 <?php
 
-namespace JMS\SerializerBundle\Tests\Serializer\Fixture;
+namespace JMS\SerializerBundle\Tests\Fixtures;
 
 use JMS\SerializerBundle\Serializer\JsonDeserializationVisitor;
 use JMS\SerializerBundle\Serializer\XmlDeserializationVisitor;

+ 1 - 1
Tests/Fixtures/GroupsObject.php

@@ -37,7 +37,7 @@ class GroupsObject
     private $foobar;
 
     /**
-     * @Groups({"bar"})
+     * @Groups({"bar", "Default"})
      * @Type("string")
      */
     private $bar;

+ 14 - 13
Tests/Serializer/BaseSerializationTest.php

@@ -68,7 +68,7 @@ use JMS\SerializerBundle\Tests\Fixtures\Order;
 use JMS\SerializerBundle\Tests\Fixtures\Price;
 use JMS\SerializerBundle\Tests\Fixtures\SimpleObject;
 use JMS\SerializerBundle\Tests\Fixtures\SimpleObjectProxy;
-use JMS\SerializerBundle\Tests\Serializer\Fixture\Article;
+use JMS\SerializerBundle\Tests\Fixtures\Article;
 use JMS\SerializerBundle\Tests\Fixtures\Input;
 use JMS\SerializerBundle\Tests\Fixtures\ObjectWithEmptyHash;
 use Metadata\MetadataFactory;
@@ -281,7 +281,7 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals($this->getContent('article'), $result);
 
         if ($this->hasDeserializer()) {
-            $this->assertEquals($article, $this->deserialize($result, 'JMS\SerializerBundle\Tests\Serializer\Fixture\Article'));
+            $this->assertEquals($article, $this->deserialize($result, 'JMS\SerializerBundle\Tests\Fixtures\Article'));
         }
     }
 
@@ -443,23 +443,24 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
 
     public function testGroups()
     {
-        $serializer =  $this->serializer;
-
         $groupsObject = new GroupsObject();
 
-        $this->assertEquals($this->getContent('groups_all'), $serializer->serialize($groupsObject, $this->getFormat()));
+        $this->assertEquals($this->getContent('groups_all'), $this->serializer->serialize($groupsObject, $this->getFormat()));
+
+        $this->serializer->setGroups(array("foo"));
+        $this->assertEquals($this->getContent('groups_foo'), $this->serializer->serialize($groupsObject, $this->getFormat()));
 
-        $serializer->setGroups(array("foo"));
-        $this->assertEquals($this->getContent('groups_foo'), $serializer->serialize($groupsObject, $this->getFormat()));
+        $this->serializer->setGroups(array("foo", "bar"));
+        $this->assertEquals($this->getContent('groups_foobar'), $this->serializer->serialize($groupsObject, $this->getFormat()));
 
-        $serializer->setGroups(array("foo", "bar"));
-        $this->assertEquals($this->getContent('groups_foobar'), $serializer->serialize($groupsObject, $this->getFormat()));
+        $this->serializer->setGroups(null);
+        $this->assertEquals($this->getContent('groups_all'), $this->serializer->serialize($groupsObject, $this->getFormat()));
 
-        $serializer->setGroups(null);
-        $this->assertEquals($this->getContent('groups_all'), $serializer->serialize($groupsObject, $this->getFormat()));
+        $this->serializer->setGroups(array());
+        $this->assertEquals($this->getContent('groups_all'), $this->serializer->serialize($groupsObject, $this->getFormat()));
 
-        $serializer->setGroups(array());
-        $this->assertEquals($this->getContent('groups_all'), $serializer->serialize($groupsObject, $this->getFormat()));
+        $this->serializer->setGroups(array('Default'));
+        $this->assertEquals($this->getContent('groups_default'), $this->serializer->serialize($groupsObject, $this->getFormat()));
     }
 
     public function testVirtualProperty()

+ 1 - 0
Tests/Serializer/JsonSerializationTest.php

@@ -76,6 +76,7 @@ class JsonSerializationTest extends BaseSerializationTest
             $outputs['groups_all'] = '{"foo":"foo","foobar":"foobar","bar":"bar","none":"none"}';
             $outputs['groups_foo'] = '{"foo":"foo","foobar":"foobar"}';
             $outputs['groups_foobar'] = '{"foo":"foo","foobar":"foobar","bar":"bar"}';
+            $outputs['groups_default'] = '{"bar":"bar","none":"none"}';
             $outputs['virtual_properties'] = '{"exist_field":"value","virtual_value":"value","test":"other-name"}';
             $outputs['virtual_properties_low'] = '{"low":1}';
             $outputs['virtual_properties_high'] = '{"high":8}';

+ 5 - 0
Tests/Serializer/xml/groups_default.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<result>
+  <bar><![CDATA[bar]]></bar>
+  <none><![CDATA[none]]></none>
+</result>

+ 2 - 0
Tests/Serializer/yml/groups_default.yml

@@ -0,0 +1,2 @@
+bar: bar
+none: none