Skip to content

Commit

Permalink
Merge pull request #127 from ConductionNL/feature/CONNECTOR-182/fix-e…
Browse files Browse the repository at this point in the history
…xtend

fix extend for sub objects
  • Loading branch information
bbrands02 authored Jan 16, 2025
2 parents 5b65d14 + f5ce9bd commit cd5bc10
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/Service/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1550,17 +1550,25 @@ public function renderEntity(array $entity, ?array $extend = []): array
* @return array The extended entity as an array
* @throws Exception If property not found
*/
public function extendEntity(array $entity, array $extend): array
public function extendEntity(array $entity, array $extend, ?int $depth = 0): array
{
if ($depth > 3) {
return $entity;
}

// Convert entity to array if needed
if (is_array($entity)) {
$result = $entity;
} else {
$result = $entity->jsonSerialize();
}

if (in_array('all', $extend)) {
$extendProperties = array_keys($result);
}

// Process each property to extend
foreach ($extend as $property) {
foreach ($extendProperties as $property) {
$singularProperty = rtrim($property, 's');

// Check if property exists
Expand Down Expand Up @@ -1597,7 +1605,8 @@ public function extendEntity(array $entity, array $extend): array
try {
$found = $this->objectEntityMapper->find($val);
if ($found) {
$extendedValues[] = $found;
$extendedFound = $this->extendEntity($found->jsonSerialize(), $extend, $depth + 1);
$extendedValues[] = $extendedFound;
}
} catch (Exception $e) {
continue;
Expand All @@ -1610,7 +1619,8 @@ public function extendEntity(array $entity, array $extend): array
// Handle single value
$found = $this->objectEntityMapper->find($value);
if ($found) {
$result[$property] = $found;
// Serialize and recursively extend the found object (apply depth tracking here)
$result[$property] = $this->extendEntity($found->jsonSerialize(), $extend, $depth + 1); // Start with depth 1
}
}
} catch (Exception $e2) {
Expand Down

0 comments on commit cd5bc10

Please sign in to comment.