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

Commit

Permalink
Updated with latest changes from doctrine/DoctrineModule
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra committed Dec 13, 2018
1 parent c2ba08c commit 20667a1
Show file tree
Hide file tree
Showing 8 changed files with 415 additions and 213 deletions.
45 changes: 43 additions & 2 deletions src/DoctrineObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,23 @@ protected function extractByReference($object)
return $data;
}

/**
* Converts a value for hydration
* Apply strategies first, then the type conversions
*
* @inheritdoc
*/
public function hydrateValue($name, $value, $data = null)
{
$value = parent::hydrateValue($name, $value, $data);

if (is_null($value) && $this->isNullable($name)) {
return null;
}

return $this->handleTypeConversions($value, $this->metadata->getTypeOfField($name));
}

/**
* Hydrate the object using a by-value logic (this means that it uses the entity API, in this
* case, setters)
Expand All @@ -272,7 +289,6 @@ protected function hydrateByValue(array $data, $object)

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

if ($metadata->hasAssociation($field)) {
Expand Down Expand Up @@ -334,7 +350,6 @@ protected function hydrateByReference(array $data, $object)
continue;
}

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

Expand Down Expand Up @@ -511,6 +526,10 @@ function ($item) {
*/
protected function handleTypeConversions($value, $typeOfField)
{
if (is_null($value)) {
return null;
}

switch ($typeOfField) {
case 'boolean':
$value = (bool) $value;
Expand Down Expand Up @@ -604,6 +623,28 @@ function ($value) {
return false;
}

/**
* Check the field is nullable
*
* @param $name
* @return bool
*/
private function isNullable($name)
{
//TODO: need update after updating isNullable method of Doctrine\ORM\Mapping\ClassMetadata
if ($this->metadata->hasField($name)) {
return method_exists($this->metadata, 'isNullable') && $this->metadata->isNullable($name);
}

if ($this->metadata->hasAssociation($name) && method_exists($this->metadata, 'getAssociationMapping')) {
$mapping = $this->metadata->getAssociationMapping($name);

return false !== $mapping && isset($mapping['nullable']) && $mapping['nullable'];
}

return false;
}

/**
* Applies the naming strategy if there is one set
*
Expand Down
48 changes: 48 additions & 0 deletions test/Assets/ByValueDifferentiatorEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace ZendTest\Doctrine\Hydrator\Assets;

class ByValueDifferentiatorEntity
{
/**
* @var int
*/
protected $id;

/**
* @var string
*/
protected $field;

public function setId($id)
{
$this->id = $id;
}

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

public function setField($field, $modifyValue = true)
{
// Modify the value to illustrate the difference between by value and by reference
if ($modifyValue) {
$this->field = "From setter: $field";
} else {
$this->field = $field;
}
}

public function getField($modifyValue = true)
{
// Modify the value to illustrate the difference between by value and by reference
if ($modifyValue) {
return "From getter: $this->field";
} else {
return $this->field;
}
}
}
38 changes: 0 additions & 38 deletions test/Assets/ContextEntity.php

This file was deleted.

4 changes: 2 additions & 2 deletions test/Assets/OneToOneEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class OneToOneEntity
protected $id;

/**
* @var SimpleEntity
* @var ByValueDifferentiatorEntity
*/
protected $toOne;

Expand All @@ -27,7 +27,7 @@ public function getId()
return $this->id;
}

public function setToOne(SimpleEntity $entity = null, $modifyValue = true)
public function setToOne(ByValueDifferentiatorEntity $entity = null, $modifyValue = true)
{
// Modify the value to illustrate the difference between by value and by reference
if ($modifyValue && $entity !== null) {
Expand Down
4 changes: 2 additions & 2 deletions test/Assets/OneToOneEntityNotNullable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class OneToOneEntityNotNullable
protected $id;

/**
* @var SimpleEntity
* @var ByValueDifferentiatorEntity
*/
protected $toOne;

Expand All @@ -27,7 +27,7 @@ public function getId()
return $this->id;
}

public function setToOne(SimpleEntity $entity, $modifyValue = true)
public function setToOne(ByValueDifferentiatorEntity $entity, $modifyValue = true)
{
// Modify the value to illustrate the difference between by value and by reference
if ($modifyValue) {
Expand Down
18 changes: 4 additions & 14 deletions test/Assets/SimpleEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,13 @@ public function getId()
return $this->id;
}

public function setField($field, $modifyValue = true)
public function setField($field)
{
// Modify the value to illustrate the difference between by value and by reference
if ($modifyValue) {
$this->field = "From setter: $field";
} else {
$this->field = $field;
}
$this->field = $field;
}

public function getField($modifyValue = true)
public function getField()
{
// Modify the value to illustrate the difference between by value and by reference
if ($modifyValue) {
return "From getter: $this->field";
} else {
return $this->field;
}
return $this->field;
}
}
Loading

0 comments on commit 20667a1

Please sign in to comment.