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

Commit

Permalink
Improve performance when working with large numbers of resources
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyzerner committed Nov 2, 2015
1 parent 78f02bd commit 300786a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ protected function getIncluded(ElementInterface $element, $includeParent = false
$included = [];

foreach ($element->getResources() as $resource) {
if ($resource->isIdentifier()) {
continue;
}

if ($includeParent) {
$included = $this->mergeResource($included, $resource);
}
Expand All @@ -79,7 +83,13 @@ protected function getIncluded(ElementInterface $element, $includeParent = false
}
}

return $included;
$flattened = [];

array_walk_recursive($included, function($a) use (&$flattened) {
$flattened[] = $a;
});

return $flattened;
}

/**
Expand All @@ -92,16 +102,12 @@ protected function mergeResource(array $resources, Resource $newResource)
$type = $newResource->getType();
$id = $newResource->getId();

foreach ($resources as $resource) {
if ($resource->getType() === $type && $resource->getId() === $id) {
$resource->merge($newResource);

return $resources;
}
if (isset($resources[$type][$id])) {
$resources[$type][$id]->merge($newResource);
} else {
$resources[$type][$id] = $newResource;
}

$resources[] = $newResource;

return $resources;
}

Expand Down
11 changes: 11 additions & 0 deletions src/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ public function toArray()
return $array;
}

/**
* Check whether or not this resource is an identifier (i.e. does it have
* any data attached?)
*
* @return bool
*/
public function isIdentifier()
{
return ! is_object($this->data) && ! is_array($this->data);
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit 300786a

Please sign in to comment.