Forráskód Böngészése

Merge pull request #145 from Wiakowe/virtual_property_exclude_all_support

Always expose virtual properties by default
Johannes 13 éve
szülő
commit
6df19222b5

+ 2 - 1
Metadata/Driver/AnnotationDriver.php

@@ -122,7 +122,8 @@ class AnnotationDriver implements DriverInterface
 
             foreach ($propertiesMetadata as $propertyKey => $propertyMetadata) {
 
-                $isExclude = $isExpose = false;
+                $isExclude = false;
+                $isExpose = $propertyMetadata instanceof VirtualPropertyMetadata;
                 $AccessType = $classAccessType;
                 $accessor = array(null, null);
 

+ 2 - 1
Metadata/Driver/XmlDriver.php

@@ -88,7 +88,8 @@ class XmlDriver extends AbstractFileDriver
 
             foreach ($propertiesMetadata as $propertyKey => $pMetadata) {
 
-                $isExclude = $isExpose = false;
+                $isExclude = false;
+                $isExpose = $pMetadata instanceof VirtualPropertyMetadata;
 
                 $pElem = $propertiesNodes[$propertyKey];
                 if (!empty( $pElem )) {

+ 3 - 1
Metadata/Driver/YamlDriver.php

@@ -81,7 +81,9 @@ class YamlDriver extends AbstractFileDriver
 
             foreach ($propertiesMetadata as $pName => $pMetadata) {
 
-                $isExclude = $isExpose = false;
+                $isExclude = false;
+                $isExpose = $pMetadata instanceof VirtualPropertyMetadata;
+
                 if (isset($config['properties'][$pName])) {
                     $pConfig = $config['properties'][$pName];
 

+ 36 - 0
Tests/Fixtures/ObjectWithVirtualPropertiesAndExcludeAll.php

@@ -0,0 +1,36 @@
+<?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\Tests\Fixtures;
+
+use JMS\SerializerBundle\Annotation\VirtualProperty;
+use JMS\SerializerBundle\Annotation\ExclusionPolicy;
+
+/**
+ * @ExclusionPolicy("all")
+ */
+class ObjectWithVirtualPropertiesAndExcludeAll
+{
+    /**
+     * @VirtualProperty
+     */
+    public function getVirtualValue()
+    {
+        return 'value';
+    }
+}

+ 13 - 0
Tests/Metadata/Driver/BaseDriverTest.php

@@ -94,5 +94,18 @@ abstract class BaseDriverTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($m->propertyMetadata['array']->xmlKeyValuePairs);
     }
 
+    public function testVirtualPropertyWithExcludeAll()
+    {
+        $a = new \JMS\SerializerBundle\Tests\Fixtures\ObjectWithVirtualPropertiesAndExcludeAll();
+        $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass($a));
+
+        $this->assertArrayHasKey('virtualValue', $m->propertyMetadata);
+
+        $p = new VirtualPropertyMetadata($m->name, 'virtualValue');
+        $p->getter = 'getVirtualValue';
+
+        $this->assertEquals($p, $m->propertyMetadata['virtualValue']);
+    }
+
     abstract protected function getDriver();
 }

+ 15 - 0
Tests/Metadata/Driver/php/ObjectWithVirtualPropertiesAndExcludeAll.php

@@ -0,0 +1,15 @@
+<?php
+
+use JMS\SerializerBundle\Metadata\ClassMetadata;
+use JMS\SerializerBundle\Metadata\PropertyMetadata;
+use JMS\SerializerBundle\Metadata\VirtualPropertyMetadata;
+
+$className = 'JMS\SerializerBundle\Tests\Fixtures\ObjectWithVirtualPropertiesAndExcludeAll';
+
+$metadata = new ClassMetadata( $className );
+
+$pMetadata = new VirtualPropertyMetadata($className, 'virtualValue');
+$pMetadata->getter = 'getVirtualValue';
+$metadata->addPropertyMetadata($pMetadata);
+
+return $metadata;

+ 6 - 0
Tests/Metadata/Driver/xml/ObjectWithVirtualPropertiesAndExcludeAll.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<serializer>
+    <class name="JMS\SerializerBundle\Tests\Fixtures\ObjectWithVirtualPropertiesAndExcludeAll"  exclusion-policy="ALL">
+        <virtual-property method="getVirtualValue"/>
+    </class>
+</serializer>

+ 4 - 0
Tests/Metadata/Driver/yml/ObjectWithVirtualPropertiesAndExcludeAll.yml

@@ -0,0 +1,4 @@
+JMS\SerializerBundle\Tests\Fixtures\ObjectWithVirtualPropertiesAndExcludeAll:
+    exclusion_policy: all
+    virtual_properties:
+        getVirtualValue: ~