Skip to content

Commit 6df1922

Browse files
committedJul 6, 2012
Merge pull request #145 from Wiakowe/virtual_property_exclude_all_support
Always expose virtual properties by default
2 parents c9277bb + d36c772 commit 6df1922

8 files changed

+81
-3
lines changed
 

Diff for: ‎Metadata/Driver/AnnotationDriver.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ public function loadMetadataForClass(\ReflectionClass $class)
122122

123123
foreach ($propertiesMetadata as $propertyKey => $propertyMetadata) {
124124

125-
$isExclude = $isExpose = false;
125+
$isExclude = false;
126+
$isExpose = $propertyMetadata instanceof VirtualPropertyMetadata;
126127
$AccessType = $classAccessType;
127128
$accessor = array(null, null);
128129

Diff for: ‎Metadata/Driver/XmlDriver.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ protected function loadMetadataFromFile(\ReflectionClass $class, $path)
8888

8989
foreach ($propertiesMetadata as $propertyKey => $pMetadata) {
9090

91-
$isExclude = $isExpose = false;
91+
$isExclude = false;
92+
$isExpose = $pMetadata instanceof VirtualPropertyMetadata;
9293

9394
$pElem = $propertiesNodes[$propertyKey];
9495
if (!empty( $pElem )) {

Diff for: ‎Metadata/Driver/YamlDriver.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ protected function loadMetadataFromFile(\ReflectionClass $class, $file)
8181

8282
foreach ($propertiesMetadata as $pName => $pMetadata) {
8383

84-
$isExclude = $isExpose = false;
84+
$isExclude = false;
85+
$isExpose = $pMetadata instanceof VirtualPropertyMetadata;
86+
8587
if (isset($config['properties'][$pName])) {
8688
$pConfig = $config['properties'][$pName];
8789

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace JMS\SerializerBundle\Tests\Fixtures;
20+
21+
use JMS\SerializerBundle\Annotation\VirtualProperty;
22+
use JMS\SerializerBundle\Annotation\ExclusionPolicy;
23+
24+
/**
25+
* @ExclusionPolicy("all")
26+
*/
27+
class ObjectWithVirtualPropertiesAndExcludeAll
28+
{
29+
/**
30+
* @VirtualProperty
31+
*/
32+
public function getVirtualValue()
33+
{
34+
return 'value';
35+
}
36+
}

Diff for: ‎Tests/Metadata/Driver/BaseDriverTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,18 @@ public function testXmlKeyValuePairs()
9494
$this->assertTrue($m->propertyMetadata['array']->xmlKeyValuePairs);
9595
}
9696

97+
public function testVirtualPropertyWithExcludeAll()
98+
{
99+
$a = new \JMS\SerializerBundle\Tests\Fixtures\ObjectWithVirtualPropertiesAndExcludeAll();
100+
$m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass($a));
101+
102+
$this->assertArrayHasKey('virtualValue', $m->propertyMetadata);
103+
104+
$p = new VirtualPropertyMetadata($m->name, 'virtualValue');
105+
$p->getter = 'getVirtualValue';
106+
107+
$this->assertEquals($p, $m->propertyMetadata['virtualValue']);
108+
}
109+
97110
abstract protected function getDriver();
98111
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use JMS\SerializerBundle\Metadata\ClassMetadata;
4+
use JMS\SerializerBundle\Metadata\PropertyMetadata;
5+
use JMS\SerializerBundle\Metadata\VirtualPropertyMetadata;
6+
7+
$className = 'JMS\SerializerBundle\Tests\Fixtures\ObjectWithVirtualPropertiesAndExcludeAll';
8+
9+
$metadata = new ClassMetadata( $className );
10+
11+
$pMetadata = new VirtualPropertyMetadata($className, 'virtualValue');
12+
$pMetadata->getter = 'getVirtualValue';
13+
$metadata->addPropertyMetadata($pMetadata);
14+
15+
return $metadata;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<serializer>
3+
<class name="JMS\SerializerBundle\Tests\Fixtures\ObjectWithVirtualPropertiesAndExcludeAll" exclusion-policy="ALL">
4+
<virtual-property method="getVirtualValue"/>
5+
</class>
6+
</serializer>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
JMS\SerializerBundle\Tests\Fixtures\ObjectWithVirtualPropertiesAndExcludeAll:
2+
exclusion_policy: all
3+
virtual_properties:
4+
getVirtualValue: ~

0 commit comments

Comments
 (0)
Please sign in to comment.