Skip to content

Commit

Permalink
Build entity with metadata identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrerolland committed Feb 5, 2018
1 parent 17c70d5 commit 17167d5
Showing 1 changed file with 83 additions and 8 deletions.
91 changes: 83 additions & 8 deletions Builder/EntityBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,17 @@ private function fillAssociations($entity, ClassMetadata $metadata, Request $req
private function retrieveAssociationValue($fromClass, $targetClass, array $requestValues)
{
$repo = $this->entityManager->getRepository($targetClass);
if (isset($requestValues['id'])) {
$value = $repo->find($requestValues['id']);
$targetClassMetadata = $this->entityManager->getClassMetadata($targetClass);
$identifiers = $targetClassMetadata->getIdentifierFieldNames();

$search = [];
foreach ($identifiers as $identifier) {
if (isset($requestValues[$identifier])) {
$search[$identifier] = $requestValues[$identifier];
}
}
if (count($search) === count($identifiers)) {
$value = $repo->findBy($search);
if (!$value) {
$value = new $targetClass();
}
Expand Down Expand Up @@ -187,11 +196,13 @@ private function mergeCollection(
$entityValues = $this->removeItemsNotInRequest($entityValues, $requestValues);
$collection = is_array($entityValues) ? [] : new ArrayCollection();

$identifiers = is_array($entityValues) && count($entityValues) > 0 ?
$this->entityManager->getClassMetadata(current($entityValues))->getIdentifierFieldNames() : [];
foreach ($requestValues as $requestValue) {
if ($entityValues && isset($requestValue['id'])) {
if ($entityValues && $this->requestValuesHaveIdentifiers($requestValues, $identifiers)) {
foreach ($entityValues as $entityValue) {
if (method_exists($entityValue, 'getId')) {
if ($entityValue->getId() == $requestValue['id']) {
if ($this->objectHasIdentifiersGetters($entity, $identifiers)) {
if ($this->objectIdentifiersMatchRequest($entityValue, $requestValue, $identifiers)) {
$this->buildEntity($entityValue, $this->getNewRequest($requestValue), $fromClass);
if ($isInverseSide && $mappedBy) {
$this->propertyAccessor->setValue($entityValue, $mappedBy, $entity);
Expand Down Expand Up @@ -223,13 +234,25 @@ private function removeItemsNotInRequest($entityValues, array $requestValues)
if (!$entityValues) {
return null;
}

$entityClass = $entityValues instanceof Collection ? get_class($entityValues->current()) :
is_array($entityValues) ? get_class(current($entityValues)) : null;

if (!$entityClass) {
return null;
}

$identifiers = $this->entityManager->getClassMetadata($entityClass)->getIdentifierFieldNames();

foreach ($entityValues as $id => $entityValue) {
if (!method_exists($entityValue, 'getId')) {
if (!$this->objectHasIdentifiersGetters($entityValue, $identifiers)) {
return;
}
$entityValueId = $entityValue->getId();
foreach ($requestValues as $requestValue) {
if (isset($requestValue['id']) && $requestValue['id'] == $entityValueId) {
if (
$this->requestValuesHaveIdentifiers($requestValue, $identifiers) &&
$this->objectIdentifiersMatchRequest($entityValue, $requestValue, $identifiers)
) {
continue 2;
}
}
Expand All @@ -253,4 +276,56 @@ private function getNewRequest($values)
{
return new Request([], [], [], [], [], [], json_encode($values));
}

/**
* @param array $requestValues
* @param array $identifiers
*
* @return bool
*/
private function requestValuesHaveIdentifiers(array $requestValues, array $identifiers)
{
foreach ($identifiers as $identifier) {
if (!isset($requestValues[$identifier])) {
return false;
}
}

return true;
}

/**
* @param object $object
* @param array $identifiers
*
* @return bool
*/
private function objectHasIdentifiersGetters($object, array $identifiers)
{
foreach ($identifiers as $identifier) {
if (!method_exists($object, sprintf('get%s', ucfirst($identifier)))) {
return false;
}
}

return true;
}

/**
* @param object $object
* @param array $requestValue
* @param array $identifiers
*
* @return bool
*/
private function objectIdentifiersMatchRequest($object, array $requestValue, array $identifiers)
{
foreach ($identifiers as $identifier) {
if ($object->{sprintf('get%s', ucfirst($identifier))}() != $requestValue[$identifier]) {
return false;
}
}

return true;
}
}

0 comments on commit 17167d5

Please sign in to comment.