Skip to content
This repository was archived by the owner on Jan 24, 2020. It is now read-only.

Commit 20667a1

Browse files
committed
Updated with latest changes from doctrine/DoctrineModule
1 parent c2ba08c commit 20667a1

File tree

8 files changed

+415
-213
lines changed

8 files changed

+415
-213
lines changed

src/DoctrineObject.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,23 @@ protected function extractByReference($object)
252252
return $data;
253253
}
254254

255+
/**
256+
* Converts a value for hydration
257+
* Apply strategies first, then the type conversions
258+
*
259+
* @inheritdoc
260+
*/
261+
public function hydrateValue($name, $value, $data = null)
262+
{
263+
$value = parent::hydrateValue($name, $value, $data);
264+
265+
if (is_null($value) && $this->isNullable($name)) {
266+
return null;
267+
}
268+
269+
return $this->handleTypeConversions($value, $this->metadata->getTypeOfField($name));
270+
}
271+
255272
/**
256273
* Hydrate the object using a by-value logic (this means that it uses the entity API, in this
257274
* case, setters)
@@ -272,7 +289,6 @@ protected function hydrateByValue(array $data, $object)
272289

273290
foreach ($data as $field => $value) {
274291
$field = $this->computeHydrateFieldName($field);
275-
$value = $this->handleTypeConversions($value, $metadata->getTypeOfField($field));
276292
$setter = 'set' . Inflector::classify($field);
277293

278294
if ($metadata->hasAssociation($field)) {
@@ -334,7 +350,6 @@ protected function hydrateByReference(array $data, $object)
334350
continue;
335351
}
336352

337-
$value = $this->handleTypeConversions($value, $metadata->getTypeOfField($field));
338353
$reflProperty = $refl->getProperty($field);
339354
$reflProperty->setAccessible(true);
340355

@@ -511,6 +526,10 @@ function ($item) {
511526
*/
512527
protected function handleTypeConversions($value, $typeOfField)
513528
{
529+
if (is_null($value)) {
530+
return null;
531+
}
532+
514533
switch ($typeOfField) {
515534
case 'boolean':
516535
$value = (bool) $value;
@@ -604,6 +623,28 @@ function ($value) {
604623
return false;
605624
}
606625

626+
/**
627+
* Check the field is nullable
628+
*
629+
* @param $name
630+
* @return bool
631+
*/
632+
private function isNullable($name)
633+
{
634+
//TODO: need update after updating isNullable method of Doctrine\ORM\Mapping\ClassMetadata
635+
if ($this->metadata->hasField($name)) {
636+
return method_exists($this->metadata, 'isNullable') && $this->metadata->isNullable($name);
637+
}
638+
639+
if ($this->metadata->hasAssociation($name) && method_exists($this->metadata, 'getAssociationMapping')) {
640+
$mapping = $this->metadata->getAssociationMapping($name);
641+
642+
return false !== $mapping && isset($mapping['nullable']) && $mapping['nullable'];
643+
}
644+
645+
return false;
646+
}
647+
607648
/**
608649
* Applies the naming strategy if there is one set
609650
*
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ZendTest\Doctrine\Hydrator\Assets;
6+
7+
class ByValueDifferentiatorEntity
8+
{
9+
/**
10+
* @var int
11+
*/
12+
protected $id;
13+
14+
/**
15+
* @var string
16+
*/
17+
protected $field;
18+
19+
public function setId($id)
20+
{
21+
$this->id = $id;
22+
}
23+
24+
public function getId()
25+
{
26+
return $this->id;
27+
}
28+
29+
public function setField($field, $modifyValue = true)
30+
{
31+
// Modify the value to illustrate the difference between by value and by reference
32+
if ($modifyValue) {
33+
$this->field = "From setter: $field";
34+
} else {
35+
$this->field = $field;
36+
}
37+
}
38+
39+
public function getField($modifyValue = true)
40+
{
41+
// Modify the value to illustrate the difference between by value and by reference
42+
if ($modifyValue) {
43+
return "From getter: $this->field";
44+
} else {
45+
return $this->field;
46+
}
47+
}
48+
}

test/Assets/ContextEntity.php

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

test/Assets/OneToOneEntity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class OneToOneEntity
1212
protected $id;
1313

1414
/**
15-
* @var SimpleEntity
15+
* @var ByValueDifferentiatorEntity
1616
*/
1717
protected $toOne;
1818

@@ -27,7 +27,7 @@ public function getId()
2727
return $this->id;
2828
}
2929

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

test/Assets/OneToOneEntityNotNullable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class OneToOneEntityNotNullable
1212
protected $id;
1313

1414
/**
15-
* @var SimpleEntity
15+
* @var ByValueDifferentiatorEntity
1616
*/
1717
protected $toOne;
1818

@@ -27,7 +27,7 @@ public function getId()
2727
return $this->id;
2828
}
2929

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

test/Assets/SimpleEntity.php

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

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

39-
public function getField($modifyValue = true)
34+
public function getField()
4035
{
41-
// Modify the value to illustrate the difference between by value and by reference
42-
if ($modifyValue) {
43-
return "From getter: $this->field";
44-
} else {
45-
return $this->field;
46-
}
36+
return $this->field;
4737
}
4838
}

0 commit comments

Comments
 (0)