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()); }
public function jsonLDObjects() { $entity = new Entity(['foo' => 'bar'], 'identifier', 'route'); $property = new Property('self'); $property->setRoute('resource/route')->setRouteParams(['id' => 'identifier']); $entity->getProperties()->add($property); $collection = new Collection([$entity]); $collection->setCollectionRoute('collection/route'); $collection->setEntityRoute('resource/route'); return ['entity' => [$entity], 'collection' => [$collection]]; }
/** * Create a entity and/or collection based on a metadata map * * @param object $object * @param Metadata $metadata * @param bool $renderEmbeddedEntities * @return Entity|Collection * @throws Exception\RuntimeException */ public function createEntityFromMetadata($object, Metadata $metadata, $renderEmbeddedEntities = true) { if ($metadata->isCollection()) { return $this->createCollectionFromMetadata($object, $metadata); } $data = $this->entityExtractor->extract($object); $entityIdentifierName = $metadata->getEntityIdentifierName(); if ($entityIdentifierName && !isset($data[$entityIdentifierName])) { throw new Exception\RuntimeException(sprintf('Unable to determine entity identifier for object of type "%s"; no fields matching "%s"', get_class($object), $entityIdentifierName)); } $id = $entityIdentifierName ? $data[$entityIdentifierName] : null; if (!$renderEmbeddedEntities) { $object = []; } $jsonLDEntity = new Entity($object, $id); $properties = $jsonLDEntity->getProperties(); $this->marshalMetadataProperties($metadata, $properties); $forceFullUriID = $metadata->getForceFullUriID(); if ($forceFullUriID && !$properties->has('id')) { $property = $this->marshalPropertyFromMetadata($metadata, $object, $id, $metadata->getRouteIdentifierName()); $properties->add($property); } return $jsonLDEntity; }
/** * 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(); }
public function setUpChildEntity($id, $name) { $this->child = (object) ['id' => $id, 'name' => $name]; $entity = new Entity($this->child, $id); $property = new Property('some'); $property->setRoute('parent/child'); $property->setRouteParams(['child' => $id]); $entity->getProperties()->add($property); return $entity; }
public function testRendersEmbeddedEntitiesOfIndividualPaginatedCollections() { $this->router->addRoute('user', new Segment('/user[/:id]')); $child = new Entity(['id' => 'matthew', 'name' => 'matthew', 'github' => 'weierophinney'], 'matthew'); $property = new Property('id'); $property->setRoute('user')->setRouteParams(['id' => 'matthew']); $child->getProperties()->add($property); $prototype = ['foo' => 'bar', 'user' => $child]; $items = []; foreach (range(1, 3) as $id) { $item = $prototype; $item['id'] = $id; $items[] = $item; } $adapter = new ArrayPaginator($items); $paginator = new Paginator($adapter); $collection = new Collection($paginator); $collection->setPageSize(5); $collection->setPage(1); $collection->setCollectionRoute('resource'); $collection->setEntityRoute('resource'); $properties = $collection->getProperties(); $idProperty = new Property('id'); $idProperty->setRoute('resource'); $properties->add($idProperty); $result = $this->plugin->renderCollection($collection); $this->assertInternalType('array', $result, var_export($result, 1)); $collection = $result['member']; foreach ($collection as $item) { $this->assertArrayHasKey('user', $item, var_export($item, 1)); $user = $item['user']; $this->assertRelationalPropertyContains('/user/matthew', 'id', $user); foreach ($child->entity as $key => $value) { if ($key === 'id') { $this->assertArrayHasKey('id', $user); $this->assertContains($value, $user['id']); continue; } $this->assertArrayHasKey($key, $user); $this->assertEquals($value, $user[$key]); } } }