Skip to content

Commit

Permalink
Merge pull request #34 from BowlOfSoup/fix-processed-depth
Browse files Browse the repository at this point in the history
Fix to processed depth
  • Loading branch information
BowlOfSoup authored Jul 23, 2020
2 parents 3872ae0 + f38941e commit 08c3366
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 7 deletions.
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![Build Status](https://travis-ci.org/BowlOfSoup/NormalizerBundle.svg?branch=master)](https://travis-ci.org/BowlOfSoup/NormalizerBundle)
[![Coverage Status](https://coveralls.io/repos/github/BowlOfSoup/NormalizerBundle/badge.svg?branch=master)](https://coveralls.io/github/BowlOfSoup/NormalizerBundle?branch=master)
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.2-blue.svg?no-cache=1)](https://php.net/)
[![Minimum Symfony Version](https://img.shields.io/badge/symfony-%3E%3D%203.4-green.svg)](https://symfony.com/)
[![Minimum Symfony Version](https://img.shields.io/badge/symfony-%3E%3D%204.4-green.svg)](https://symfony.com/)

Installation
------------
Expand Down Expand Up @@ -211,3 +211,42 @@ Calling the normalizer with a group is optional, but certainly recommended. The

$result = $this->normalizer->normalize($someEntity, 'somegroup');
$result = $this->normalizer->normalize(array($someEntity, $anotherEntity), 'somegroup');

Using translations
------------------
### Use statement and alias
BowlOfSoup\NormalizerBundle\Annotation as Bos;

### Add the annotation to your property/method

/**
* @Bos\Normalize(group={"somegroup"}, name="automobile")
* @Bos\Translate(group={"translation"})
*/
private $propertyToBeNormalized

This will try to translate the value in `$propertyToBeNormalized`.
By default, the translation must be in a `Resources/translations/messages.en.xliff`.

### Specify your domain (the .xliff file)

/**
* @Bos\Normalize(group={"somegroup"}, name="automobile")
* @Bos\Translate(group={"translation"}, domain="some_domain")
*/
private $propertyToBeNormalized

This tries to find the translation in `Resources/translations/some_domain.en.xliff`.

### Specify your locale (language)

You can specify your locale, if you did not set that globally in Symfony.

/**
* @Bos\Normalize(group={"somegroup"}, name="automobile")
* @Bos\Translate(group={"translation"}, domain="some_domain", locale="nl")
*/
private $propertyToBeNormalized

This tries to find the translation in `Resources/translations/some_domain.nl.xliff`.
Notice the `nl` in the file name (Dutch language).
4 changes: 4 additions & 0 deletions src/Service/Normalize/MethodNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public function normalize(
$this->sharedNormalizer = $sharedNormalizer;
$this->group = $group;

$this->processedDepthObjects[$objectName] = $this->processedDepth;

$classMethods = $this->methodExtractor->getMethods($object);
$normalizedMethods = [];
foreach ($classMethods as $classMethod) {
Expand Down Expand Up @@ -212,6 +214,8 @@ private function getValueForMethodWithTypeObject(object $object, \ReflectionMeth
}

if (empty($methodValue)) {
--$this->processedDepth;

return null;
}

Expand Down
4 changes: 4 additions & 0 deletions src/Service/Normalize/PropertyNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,14 @@ private function getValueForPropertyWithTypeObject(object $object, $propertyValu

$annotationCallback = $propertyAnnotation->getCallback();
if (!empty($annotationCallback) && is_callable([$propertyValue, $annotationCallback])) {
--$this->processedDepth;

return $this->handleCallbackResult($propertyValue->$annotationCallback(), $propertyAnnotation);
}

if (empty($propertyValue)) {
--$this->processedDepth;

return null;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/Service/NormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,20 +435,23 @@ public function testObjectBeingCached(): void
$this->assertSame([
'hobbies' => [
[
'id' => 1,
'description' => 'Fixing washing machines',
'hobbyType' => [
'id' => 1,
'name' => 'Technical',
],
],
[
'id' => 2,
'description' => 'Volleyball',
'hobbyType' => [
'id' => 2,
'name' => 'Sport',
],
],
[
'id' => 3,
'description' => 'Fixing Computers',
'hobbyType' => [
'id' => 1,
Expand Down Expand Up @@ -548,16 +551,19 @@ private function getDummyDataSet(): Person
$hobbyType2->setName('Technical');

$hobbies1 = new Hobbies();
$hobbies1->setId(1);
$hobbies1->setDescription('Playing Guitar');
$hobbies1->setHobbyType($hobbyType1);
$hobbyCollection->add($hobbies1);

$hobbies2 = new Hobbies();
$hobbies2->setId(2);
$hobbies2->setDescription('Fixing Computers');
$hobbies2->setHobbyType($hobbyType2);
$hobbyCollection->add($hobbies2);

$hobbies3 = new Hobbies();
$hobbies3->setId(3);
$hobbies3->setDescription('Playing Piano');
$hobbies3->setHobbyType($hobbyType1);
$hobbyCollection->add($hobbies3);
Expand Down Expand Up @@ -590,16 +596,19 @@ private function getDummyDataSetWithDuplicateObjectById(): Person
$hobbyType2->setName('Sport');

$hobbies1 = new Hobbies();
$hobbies1->setId(1);
$hobbies1->setDescription('Fixing washing machines');
$hobbies1->setHobbyType($hobbyType);
$hobbyCollection->add($hobbies1);

$hobbies2 = new Hobbies();
$hobbies2->setId(2);
$hobbies2->setDescription('Volleyball');
$hobbies2->setHobbyType($hobbyType2);
$hobbyCollection->add($hobbies2);

$hobbies3 = new Hobbies();
$hobbies3->setId(3);
$hobbies3->setDescription('Fixing Computers');
$hobbies3->setHobbyType($hobbyType);
$hobbyCollection->add($hobbies3);
Expand Down Expand Up @@ -649,20 +658,23 @@ private function getSuccessResult(): array
],
'hobbies' => [
[
'id' => 1,
'description' => 'Playing Guitar',
'hobbyType' => [
'id' => 1,
'name' => 'Music',
],
],
[
'id' => 2,
'description' => 'Fixing Computers',
'hobbyType' => [
'id' => 2,
'name' => 'Technical',
],
],
[
'id' => 3,
'description' => 'Playing Piano',
'hobbyType' => [
'id' => 1,
Expand Down
31 changes: 29 additions & 2 deletions tests/assets/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@
class Group
{
/**
* @Bos\Normalize(group={"maxDepthTestDepth1"})
* @Bos\Normalize(group={"maxDepthTestDepth1", "default"})
*
* @var int
*/
private $id;

/**
* @Bos\Normalize(group={"maxDepthTestDepth1"})
* @Bos\Normalize(group={"maxDepthTestDepth1", "default"})
*
* @var string
*/
private $name;

/**
* @Bos\Normalize(group={"default"})
*
* @var \BowlOfSoup\NormalizerBundle\Tests\assets\Person[]
*/
private $persons;

/**
* @return int
*/
Expand Down Expand Up @@ -59,4 +66,24 @@ public function setName($name)

return $this;
}

/**
* @return \BowlOfSoup\NormalizerBundle\Tests\assets\Person[]
*/
public function getPersons(): array
{
return $this->persons;
}

/**
* @param \BowlOfSoup\NormalizerBundle\Tests\assets\Person[]
*
* @return $this
*/
public function setPersons(array $persons): self
{
$this->persons = $persons;

return $this;
}
}
22 changes: 22 additions & 0 deletions tests/assets/Hobbies.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

class Hobbies
{
/**
* @var int
*
* @Bos\Normalize(group={"default", "duplicateObjectId"})
*/
private $id;

/**
* @var string
*
Expand All @@ -20,6 +27,21 @@ class Hobbies
*/
private $hobbyType;

public function getId(): int
{
return $this->id;
}

/**
* @return $this
*/
public function setId(int $id): self
{
$this->id = $id;

return $this;
}

/**
* @return string
*/
Expand Down
8 changes: 4 additions & 4 deletions tests/assets/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Person
private $telephoneNumbers;

/**
* @var Hobbies
* @var \BowlOfSoup\NormalizerBundle\Tests\assets\Hobbies[]
*
* @Bos\Normalize(group={"default", "duplicateObjectId"}, type="collection")
*/
Expand Down Expand Up @@ -383,15 +383,15 @@ public function setTelephoneNumbers(TelephoneNumbers $telephoneNumbers)
}

/**
* @return Hobbies
* @return \BowlOfSoup\NormalizerBundle\Tests\assets\Hobbies[]
*/
public function getHobbies()
public function getHobbies(): array
{
return $this->hobbies;
}

/**
* @param Hobbies $hobbies
* @param \BowlOfSoup\NormalizerBundle\Tests\assets\Hobbies[] $hobbies
*
* @return $this
*/
Expand Down

0 comments on commit 08c3366

Please sign in to comment.