Skip to content

Commit dabc7bd

Browse files
authored
Merge pull request #646 from Erikvv/fix/strategy-before-type-conversion
Apply hydrator strategies before type conversion
2 parents ce1bfb0 + 336b33f commit dabc7bd

File tree

7 files changed

+260
-209
lines changed

7 files changed

+260
-209
lines changed

src/DoctrineModule/Stdlib/Hydrator/DoctrineObject.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,19 @@ protected function extractByReference($object)
262262
return $data;
263263
}
264264

265+
/**
266+
* Converts a value for hydration
267+
* Apply strategies first, then the type conversions
268+
*
269+
* @inheritdoc
270+
*/
271+
public function hydrateValue($name, $value, $data = null)
272+
{
273+
$value = parent::hydrateValue($name, $value, $data);
274+
275+
return $this->handleTypeConversions($value, $this->metadata->getTypeOfField($name));
276+
}
277+
265278
/**
266279
* Hydrate the object using a by-value logic (this means that it uses the entity API, in this
267280
* case, setters)
@@ -282,7 +295,6 @@ protected function hydrateByValue(array $data, $object)
282295

283296
foreach ($data as $field => $value) {
284297
$field = $this->computeHydrateFieldName($field);
285-
$value = $this->handleTypeConversions($value, $metadata->getTypeOfField($field));
286298
$setter = 'set' . Inflector::classify($field);
287299

288300
if ($metadata->hasAssociation($field)) {
@@ -344,7 +356,6 @@ protected function hydrateByReference(array $data, $object)
344356
continue;
345357
}
346358

347-
$value = $this->handleTypeConversions($value, $metadata->getTypeOfField($field));
348359
$reflProperty = $refl->getProperty($field);
349360
$reflProperty->setAccessible(true);
350361

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace DoctrineModuleTest\Stdlib\Hydrator\Asset;
4+
5+
class ByValueDifferentiatorEntity
6+
{
7+
/**
8+
* @var int
9+
*/
10+
protected $id;
11+
12+
/**
13+
* @var string
14+
*/
15+
protected $field;
16+
17+
public function setId($id)
18+
{
19+
$this->id = $id;
20+
}
21+
22+
public function getId()
23+
{
24+
return $this->id;
25+
}
26+
27+
public function setField($field, $modifyValue = true)
28+
{
29+
// Modify the value to illustrate the difference between by value and by reference
30+
if ($modifyValue) {
31+
$this->field = "From setter: $field";
32+
} else {
33+
$this->field = $field;
34+
}
35+
}
36+
37+
public function getField($modifyValue = true)
38+
{
39+
// Modify the value to illustrate the difference between by value and by reference
40+
if ($modifyValue) {
41+
return "From getter: $this->field";
42+
} else {
43+
return $this->field;
44+
}
45+
}
46+
}

tests/DoctrineModuleTest/Stdlib/Hydrator/Asset/ContextEntity.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

tests/DoctrineModuleTest/Stdlib/Hydrator/Asset/OneToOneEntity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class OneToOneEntity
1010
protected $id;
1111

1212
/**
13-
* @var SimpleEntity
13+
* @var ByValueDifferentiatorEntity
1414
*/
1515
protected $toOne;
1616

@@ -25,7 +25,7 @@ public function getId()
2525
return $this->id;
2626
}
2727

28-
public function setToOne(SimpleEntity $entity = null, $modifyValue = true)
28+
public function setToOne(ByValueDifferentiatorEntity $entity = null, $modifyValue = true)
2929
{
3030
// Modify the value to illustrate the difference between by value and by reference
3131
if ($modifyValue && $entity !== null) {

tests/DoctrineModuleTest/Stdlib/Hydrator/Asset/OneToOneEntityNotNullable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class OneToOneEntityNotNullable
1010
protected $id;
1111

1212
/**
13-
* @var SimpleEntity
13+
* @var ByValueDifferentiatorEntity
1414
*/
1515
protected $toOne;
1616

@@ -25,7 +25,7 @@ public function getId()
2525
return $this->id;
2626
}
2727

28-
public function setToOne(SimpleEntity $entity, $modifyValue = true)
28+
public function setToOne(ByValueDifferentiatorEntity $entity, $modifyValue = true)
2929
{
3030
// Modify the value to illustrate the difference between by value and by reference
3131
if ($modifyValue) {

tests/DoctrineModuleTest/Stdlib/Hydrator/Asset/SimpleEntity.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,13 @@ public function getId()
2424
return $this->id;
2525
}
2626

27-
public function setField($field, $modifyValue = true)
27+
public function setField($field)
2828
{
29-
// Modify the value to illustrate the difference between by value and by reference
30-
if ($modifyValue) {
31-
$this->field = "From setter: $field";
32-
} else {
33-
$this->field = $field;
34-
}
29+
$this->field = $field;
3530
}
3631

37-
public function getField($modifyValue = true)
32+
public function getField()
3833
{
39-
// Modify the value to illustrate the difference between by value and by reference
40-
if ($modifyValue) {
41-
return "From getter: $this->field";
42-
} else {
43-
return $this->field;
44-
}
34+
return $this->field;
4535
}
4636
}

0 commit comments

Comments
 (0)