/**
* Convert an EntityResult into an array of entities
*
* @see https://cloud.google.com/datastore/reference/rest/v1/EntityResult EntityResult
*
* @param array $entityResult The EntityResult from a Lookup.
* @param string|array $class If a string, the name of the class to return results as.
* Must be a subclass of {@see Google\Cloud\Datastore\Entity}.
* If not set, {@see Google\Cloud\Datastore\Entity} will be used.
* If an array is given, it must be an associative array, where
* the key is a Kind and the value is the name of a subclass of
* {@see Google\Cloud\Datastore\Entity}.
* @return Entity[]
*/
private function mapEntityResult(array $entityResult, $class)
{
$entities = [];
foreach ($entityResult as $result) {
$entity = $result['entity'];
$properties = [];
$excludes = [];
$meanings = [];
if (isset($entity['properties'])) {
$res = $this->entityMapper->responseToEntityProperties($entity['properties']);
$properties = $res['properties'];
$excludes = $res['excludes'];
$meanings = $res['meanings'];
}
$namespaceId = isset($entity['key']['partitionId']['namespaceId']) ? $entity['key']['partitionId']['namespaceId'] : null;
$key = new Key($this->projectId, ['path' => $entity['key']['path'], 'namespaceId' => $namespaceId]);
if (is_array($class)) {
$lastPathElement = $key->pathEnd();
if (!array_key_exists($lastPathElement['kind'], $class)) {
throw new InvalidArgumentException(sprintf('No class found for kind %s', $lastPathElement['kind']));
}
$className = $class[$lastPathElement['kind']];
} else {
$className = $class;
}
$entities[] = $this->entity($key, $properties, ['cursor' => isset($result['cursor']) ? $result['cursor'] : null, 'baseVersion' => isset($result['version']) ? $result['version'] : null, 'className' => $className, 'populatedByService' => true, 'excludeFromIndexes' => $excludes, 'meanings' => $meanings]);
}
return $entities;
}