8000 Fix #257 uppercase attribute names will stay uppercased by soyuka · Pull Request #260 · api-platform/core · GitHub
[go: up one dir, main page]

Skip to content

Fix #257 uppercase attribute names will stay uppercased #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Mapping/Loader/ReflectionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private function populateFromSetter(
return;
}

$attributeName = lcfirst($matches[2]);
$attributeName = $this->getAttributeName($matches[2]);
$attributeMetadata = $this->attributeMetadataFactory->getAttributeMetadataFor(
$classMetadata, $attributeName, $normalizationGroups, $denormalizationGroups
)->withWritable(true);
Expand Down Expand Up @@ -175,7 +175,7 @@ private function populateFromGetterAndHasser(
return;
}

$attributeName = lcfirst(substr($methodName, 3));
$attributeName = $this->getAttributeName(substr($methodName, 3));
$attributeMetadata = $this->attributeMetadataFactory->getAttributeMetadataFor(
$classMetadata, $attributeName, $normalizationGroups, $denormalizationGroups
)->withReadable(true);
Expand All @@ -199,7 +199,7 @@ private function populateFromIsser(ClassMetadataInterface $classMetadata, $metho
return;
}

$attributeName = lcfirst(substr($methodName, 2));
$attributeName = $this->getAttributeName(substr($methodName, 2));
$attributeMetadata = $this->attributeMetadataFactory->getAttributeMetadataFor(
$classMetadata, $attributeName, $normalizationGroups, $denormalizationGroups
)->withReadable(true);
Expand Down Expand Up @@ -258,4 +258,10 @@ private function addAttributeMetadata(ClassMetadataInterface $classMetadata, Att

return $classMetadata;
}

private function getAttributeName($name)
{
//if more than two uppercase characters don't lcfirst
return preg_match('/[A-Z]{2,}$/', $name) ? $name : lcfirst($name);
}
}
58 changes: 58 additions & 0 deletions Tests/Mapping/Loader/ReflectionLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the DunglasApiBundle package.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Dunglas\ApiBundle\Tests\Doctrine\Mapping\Loader;

use Dunglas\ApiBundle\Mapping\Loader\ReflectionLoader;

class MyTestClass
{
private $SIRET;
public function getSIRET()
{
return $this->SIRET;
}
public function setSIRET($SIRET)
{
$this->SIRET = $SIRET;
}
}

class ReflectionLoaderTest extends \PHPUnit_Framework_TestCase
{
public function testUppercaseMethods()
{
$testClass = new MyTestClass();
$reflectionClass = new \ReflectionClass($testClass);

$attributeMetadataProphecy2 = $this->prophesize('Dunglas\ApiBundle\Mapping\AttributeMetadataInterface');
$attributeMetadata2 = $attributeMetadataProphecy2->reveal();

$attributeMetadataProphecy = $this->prophesize('Dunglas\ApiBundle\Mapping\AttributeMetadataInterface');
$attributeMetadataProphecy->withWritable(true)->willReturn($attributeMetadata2)->shouldBeCalled();
$attributeMetadataProphecy->withReadable(true)->willReturn($attributeMetadata2)->shouldBeCalled();
$attributeMetadata = $attributeMetadataProphecy->reveal();

$classMetadataProphecy = $this->prophesize('Dunglas\ApiBundle\Mapping\ClassMetadataInterface');
$classMetadataProphecy->withAttributeMetadata('SIRET', $attributeMetadata2)->shouldBeCalled();
$classMetadataProphecy->getReflectionClass()->willReturn($reflectionClass)->shouldBeCalled();
$classMetadata = $classMetadataProphecy->reveal();

$factoryProphecy = $this->prophesize('Dunglas\ApiBundle\Mapping\Factory\AttributeMetadataFactory');
$factoryProphecy->getAttributeMetadataFor($classMetadata, 'SIRET', null, null)->willReturn($attributeMetadata)->shouldBeCalled();
$factory = $factoryProphecy->reveal();

$loader = new ReflectionLoader($factory);
$this->assertInstanceOf('Dunglas\ApiBundle\Mapping\Loader\LoaderInterface', $loader);

$this->assertEquals($loader->loadClassMetadata($classMetadata)->getReflectionClass(), $reflectionClass);
}
}
0