Skip to content

Commit

Permalink
adds a shorter syntax for exposing properties in YML
Browse files Browse the repository at this point in the history
Assuming you use an exclusion policy ALL, you can now expose properties with
a shorter syntax:

```
SomeClass:
    exclusion_policy: ALL
    properties:
        # Before
        some_property: { expose: true }

        # After also
        some_property: ~
```
  • Loading branch information
schmittjoh committed Jan 22, 2014
1 parent bae3203 commit 5f9fb57
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 40 deletions.
83 changes: 43 additions & 40 deletions src/JMS/Serializer/Metadata/Driver/YamlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,45 +46,12 @@ protected function loadMetadataFromFile(\ReflectionClass $class, $file)
$excludeAll = isset($config['exclude']) ? (Boolean) $config['exclude'] : false;
$classAccessType = isset($config['access_type']) ? $config['access_type'] : PropertyMetadata::ACCESS_TYPE_PROPERTY;

$propertiesMetadata = array();

if (isset($config['accessor_order'])) {
$metadata->setAccessorOrder($config['accessor_order'], isset($config['custom_accessor_order']) ? $config['custom_accessor_order'] : array());
}

if (isset($config['xml_root_name'])) {
$metadata->xmlRootName = (string) $config['xml_root_name'];
}

if (array_key_exists('xml_namespaces', $config) ) {

foreach ( $config['xml_namespaces'] as $prefix => $uri) {
$metadata->registerNamespace($uri, $prefix);
}

}

if (isset($config['discriminator'])) {
if (isset($config['discriminator']['disabled']) && true === $config['discriminator']['disabled']) {
$metadata->discriminatorDisabled = true;
} else {
if ( ! isset($config['discriminator']['field_name'])) {
throw new RuntimeException('The "field_name" attribute must be set for discriminators.');
}

if ( ! isset($config['discriminator']['map']) || ! is_array($config['discriminator']['map'])) {
throw new RuntimeException('The "map" attribute must be set, and be an array for discriminators.');
}

$metadata->setDiscriminator($config['discriminator']['field_name'], $config['discriminator']['map']);
}
}
$this->addClassProperties($metadata, $config);

$propertiesMetadata = array();
if (array_key_exists('virtual_properties', $config) ) {

foreach ( $config['virtual_properties'] as $methodName => $propertySettings ) {

if ( !$class->hasMethod( $methodName ) ) {
if ( ! $class->hasMethod( $methodName ) ) {
throw new RuntimeException('The method '.$methodName.' not found in class ' . $class->name);
}

Expand All @@ -95,19 +62,20 @@ protected function loadMetadataFromFile(\ReflectionClass $class, $file)
}
}

if (!$excludeAll) {
if ( ! $excludeAll) {
foreach ($class->getProperties() as $property) {
if ($name !== $property->class) {
continue;
}

$pName = $property->getName();
$propertiesMetadata[$pName] = new PropertyMetadata($name, $pName);
}

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

$isExclude = false;
$isExpose = $pMetadata instanceof VirtualPropertyMetadata;
$isExpose = $pMetadata instanceof VirtualPropertyMetadata
|| (isset($config['properties']) && array_key_exists($pName, $config['properties']));

if (isset($config['properties'][$pName])) {
$pConfig = $config['properties'][$pName];
Expand Down Expand Up @@ -217,7 +185,7 @@ protected function loadMetadataFromFile(\ReflectionClass $class, $file)
}
}
if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
|| (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
|| (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {
$metadata->addPropertyMetadata($pMetadata);
}
}
Expand Down Expand Up @@ -254,6 +222,41 @@ protected function getExtension()
return 'yml';
}

private function addClassProperties(ClassMetadata $metadata, array $config)
{
if (isset($config['accessor_order'])) {
$metadata->setAccessorOrder($config['accessor_order'], isset($config['custom_accessor_order']) ? $config['custom_accessor_order'] : array());
}

if (isset($config['xml_root_name'])) {
$metadata->xmlRootName = (string) $config['xml_root_name'];
}

if (array_key_exists('xml_namespaces', $config) ) {

foreach ( $config['xml_namespaces'] as $prefix => $uri) {
$metadata->registerNamespace($uri, $prefix);
}

}

if (isset($config['discriminator'])) {
if (isset($config['discriminator']['disabled']) && true === $config['discriminator']['disabled']) {
$metadata->discriminatorDisabled = true;
} else {
if ( ! isset($config['discriminator']['field_name'])) {
throw new RuntimeException('The "field_name" attribute must be set for discriminators.');
}

if ( ! isset($config['discriminator']['map']) || ! is_array($config['discriminator']['map'])) {
throw new RuntimeException('The "map" attribute must be set, and be an array for discriminators.');
}

$metadata->setDiscriminator($config['discriminator']['field_name'], $config['discriminator']['map']);
}
}
}

private function getCallbackMetadata(\ReflectionClass $class, $config)
{
if (is_string($config)) {
Expand Down
8 changes: 8 additions & 0 deletions tests/JMS/Serializer/Tests/Metadata/Driver/YamlDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@

class YamlDriverTest extends BaseDriverTest
{
public function testShortExposeSyntax()
{
$m = $this->getDriverForSubDir('short_expose')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Person'));

$this->assertArrayHasKey('name', $m->propertyMetadata);
$this->assertArrayNotHasKey('age', $m->propertyMetadata);
}

public function testBlogPost()
{
$m = $this->getDriverForSubDir('exclude_all')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost'));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
JMS\Serializer\Tests\Fixtures\Person:
exclusion_policy: ALL
properties:
name: ~

0 comments on commit 5f9fb57

Please sign in to comment.