Skip to content

Commit 78f80f5

Browse files
authored
Merge pull request #36 from BowlOfSoup/ISSUE-35
ISSUE-35
2 parents 08c3366 + be65900 commit 78f80f5

5 files changed

Lines changed: 60 additions & 0 deletions

File tree

src/Service/Extractor/MethodExtractor.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace BowlOfSoup\NormalizerBundle\Service\Extractor;
66

7+
use Doctrine\Persistence\Proxy;
8+
79
class MethodExtractor extends AbstractExtractor
810
{
911
/** @var string */
@@ -13,11 +15,21 @@ class MethodExtractor extends AbstractExtractor
1315
* Extract all annotations for a (reflected) class method.
1416
*
1517
* @param string|object $annotation
18+
*
19+
* @throws \ReflectionException
1620
*/
1721
public function extractMethodAnnotations(\ReflectionMethod $objectMethod, $annotation): array
1822
{
1923
$annotations = [];
2024

25+
if ($objectMethod->getDeclaringClass()->implementsInterface(Proxy::class)
26+
&& false !== $objectMethod->getDeclaringClass()->getParentClass()
27+
&& empty($this->annotationReader->getMethodAnnotations($objectMethod))
28+
&& $objectMethod->getDeclaringClass()->getParentClass()->hasMethod($objectMethod->getName())
29+
) {
30+
$objectMethod = $objectMethod->getDeclaringClass()->getParentClass()->getMethod($objectMethod->getName());
31+
}
32+
2133
$methodAnnotations = $this->annotationReader->getMethodAnnotations($objectMethod);
2234
foreach ($methodAnnotations as $methodAnnotation) {
2335
if ($methodAnnotation instanceof $annotation) {

src/Service/Normalize/MethodNormalizer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public function normalize(
6363
return $normalizedMethods;
6464
}
6565

66+
/**
67+
* @throws \ReflectionException
68+
*/
6669
private function getMethodAnnotations(string $objectName, \ReflectionMethod $classMethod, string $annotationClass): array
6770
{
6871
$methodName = $classMethod->getName();

tests/Service/NormalizerTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use BowlOfSoup\NormalizerBundle\Tests\assets\HobbyType;
1212
use BowlOfSoup\NormalizerBundle\Tests\assets\Person;
1313
use BowlOfSoup\NormalizerBundle\Tests\assets\ProxyObject;
14+
use BowlOfSoup\NormalizerBundle\Tests\assets\ProxySocial;
1415
use BowlOfSoup\NormalizerBundle\Tests\assets\Social;
1516
use BowlOfSoup\NormalizerBundle\Tests\assets\SomeClass;
1617
use BowlOfSoup\NormalizerBundle\Tests\assets\TelephoneNumbers;
@@ -489,6 +490,16 @@ public function testNormalizeWithTranslation(): void
489490
], $result);
490491
}
491492

493+
public function testNormalizeProxyWithMethods(): void
494+
{
495+
$socialProxy = new ProxySocial();
496+
$socialProxy->setFacebook('foo');
497+
498+
$this->assertSame([
499+
'facebook' => 'foo'
500+
], $this->normalizer->normalize($socialProxy, 'proxy-method'));
501+
}
502+
492503
private function getDummyDataSet(): Person
493504
{
494505
$groupCollection = new ArrayCollection();

tests/assets/ProxySocial.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BowlOfSoup\NormalizerBundle\Tests\assets;
6+
7+
use Doctrine\Common\Persistence\Proxy;
8+
9+
class ProxySocial extends Social implements Proxy
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
public function __load()
15+
{
16+
}
17+
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
public function __isInitialized()
22+
{
23+
}
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function getFacebook()
29+
{
30+
return parent::getFacebook();
31+
}
32+
}

tests/assets/Social.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public function setId($id)
7171

7272
/**
7373
* @return string
74+
*
75+
* @Bos\Normalize(name="facebook", group={"proxy-method"})
7476
*/
7577
public function getFacebook()
7678
{

0 commit comments

Comments
 (0)