public function testPropertyCollectionMayBeInjected() { $entity = new stdClass(); $jsonLD = new Entity($entity, 'id', 'route', ['foo' => 'bar']); $properties = new PropertyCollection(); $jsonLD->setProperties($properties); $this->assertSame($properties, $jsonLD->getProperties()); }
/** * Render an individual entity * * Creates a hash representation of the Entity. The entity is first * converted to an array, and its associated properties are injected as properties. * If any members of the entity are themselves * Entity objects, they are extracted into an "member" hash. * * @param Entity $jsonLDEntity * @param bool $renderEntity * @param int $depth depth of the current rendering recursion * @param int $maxDepth maximum rendering depth for the current metadata * @throws Exception\CircularReferenceException * @return array */ public function renderEntity(Entity $jsonLDEntity, $renderEntity = true, $depth = 0, $maxDepth = null) { $this->getEventManager()->trigger(__FUNCTION__, $this, ['entity' => $jsonLDEntity]); $entity = $jsonLDEntity->entity; $entityProperties = clone $jsonLDEntity->getProperties(); // Clone to prevent property duplication $metadataMap = $this->getMetadataMap(); if (is_object($entity)) { if ($maxDepth === null && $metadataMap->has($entity)) { $maxDepth = $metadataMap->get($entity)->getMaxDepth(); } if ($maxDepth === null) { $entityHash = spl_object_hash($entity); if (isset($this->entityHashStack[$entityHash])) { // we need to clear the stack, as the exception may be caught and the plugin may be invoked again $this->entityHashStack = []; throw new Exception\CircularReferenceException(sprintf("Circular reference detected in '%s'. %s", get_class($entity), "Either set a 'max_depth' metadata attribute or remove the reference")); } $this->entityHashStack[$entityHash] = get_class($entity); } } if (!$renderEntity || $maxDepth !== null && $depth > $maxDepth) { $entity = []; } if (!is_array($entity)) { $entity = $this->getEntityExtractor()->extract($entity); } foreach ($entity as $key => $value) { if (is_object($value) && $metadataMap->has($value)) { $value = $this->getResourceFactory()->createEntityFromMetadata($value, $metadataMap->get($value), $this->getRenderEmbeddedEntities()); } if ($value instanceof Entity) { $this->extractEmbeddedEntity($entity, $key, $value, $depth + 1, $maxDepth); } if ($value instanceof Collection) { $this->extractEmbeddedCollection($entity, $key, $value, $depth + 1, $maxDepth); } if ($value instanceof Property) { // We have a property; add it to the entity if it's not already present. $entityProperties = $this->injectPropertyAsProperty($value, $entityProperties); unset($entity[$key]); } if ($value instanceof PropertyCollection) { foreach ($value as $property) { $entityProperties = $this->injectPropertyAsProperty($property, $entityProperties); } unset($entity[$key]); } } $jsonLDEntity->setProperties($entityProperties); $entity = ArrayUtils::merge($entity, $this->fromResource($jsonLDEntity)); $payload = new ArrayObject($entity); $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, ['payload' => $payload, 'entity' => $jsonLDEntity]); if (isset($entityHash)) { unset($this->entityHashStack[$entityHash]); } return $payload->getArrayCopy(); }