コード例 #1
0
 /**
  * @inheritDoc
  */
 public function extract(Property $object)
 {
     if (!$object->isComplete()) {
         throw new DomainException(sprintf('Property from resource provided to %s was incomplete; must contain a URL or a route', __METHOD__));
     }
     if ($object->hasValue()) {
         $value = $object->getValue();
         if ($value instanceof PropertyCollection) {
             $extractor = new PropertyCollectionExtractor($this);
             $value = $extractor->extract($value);
         }
         return $value;
     }
     if ($object->hasUrl()) {
         return $object->getUrl();
     }
     $reuseMatchedParams = true;
     $options = $object->getRouteOptions();
     if (isset($options['reuse_matched_params'])) {
         $reuseMatchedParams = (bool) $options['reuse_matched_params'];
         unset($options['reuse_matched_params']);
     }
     $path = call_user_func($this->urlHelper, $object->getRoute(), $object->getRouteParams(), $options, $reuseMatchedParams);
     if (substr($path, 0, 4) == 'http') {
         return $path;
     }
     return $this->getServerUrl() . $path;
 }
コード例 #2
0
 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]];
 }
コード例 #3
0
 /**
  * Add a property
  *
  * @param  Property $property
  * @param  bool $overwrite
  * @return self
  * @throws Exception\DomainException
  */
 public function add(Property $property, $overwrite = false)
 {
     $keyword = $property->getKeyword();
     if (!isset($this->properties[$keyword]) || $overwrite || 'id' == $keyword) {
         $this->properties[$keyword] = $property;
         return $this;
     }
     if ($this->properties[$keyword] instanceof Property || $this->properties[$keyword] instanceof PropertyCollection) {
         $this->properties[$keyword] = [$this->properties[$keyword]];
     }
     if (!is_array($this->properties[$keyword])) {
         $type = is_object($this->properties[$keyword]) ? get_class($this->properties[$keyword]) : gettype($this->properties[$keyword]);
         throw new Exception\DomainException(sprintf('%s::$properties should be either a %s\\Property or an array; however, it is a "%s"', __CLASS__, __NAMESPACE__, $type));
     }
     $this->properties[$keyword][] = $property;
     return $this;
 }
コード例 #4
0
 /**
  * @group 95
  */
 public function testPassingFalseReuseParamsOptionShouldOmitMatchedParametersInGeneratedProperty()
 {
     $serverUrlHelper = $this->getMock('Zend\\View\\Helper\\ServerUrl');
     $urlHelper = new UrlHelper();
     $propertyExtractor = new PropertyExtractor($serverUrlHelper, $urlHelper);
     $match = $this->matchUrl('/resource/foo', $urlHelper);
     $this->assertEquals('foo', $match->getParam('id', false));
     $property = Property::factory(['key' => 'resource', 'route' => ['name' => 'hostname/resource', 'options' => ['reuse_matched_params' => false]]]);
     $result = $propertyExtractor->extract($property);
     $this->assertInternalType('string', $result);
     $this->assertEquals('http://localhost.localdomain/resource', $result);
 }
コード例 #5
0
 public function testPropertyCollectionWithTwoPropertiesForSameRelationShouldReturnArrayWithOneKeyAggregatingProperties()
 {
     $propertyCollection = new PropertyCollection();
     $propertyCollection->add(Property::factory(['key' => 'foo', 'url' => 'http://example.com/foo']));
     $propertyCollection->add(Property::factory(['key' => 'foo', 'url' => 'http://example.com/bar']));
     $propertyCollection->add(Property::factory(['key' => 'baz', 'url' => 'http://example.com/baz']));
     $result = $this->propertyCollectionExtractor->extract($propertyCollection);
     $this->assertInternalType('array', $result);
     $this->assertCount(2, $result);
     $this->assertInternalType('array', $result['foo']);
     $this->assertCount(2, $result['foo']);
 }
コード例 #6
0
ファイル: JsonLD.php プロジェクト: vixriihi/zf-jsonld
 /**
  * Extract a collection as an array
  *
  * @param  Collection $jsonLDCollection
  * @param  int $depth                   depth of the current rendering recursion
  * @param  int $maxDepth                maximum rendering depth for the current metadata
  * @return array
  */
 protected function extractCollection(Collection $jsonLDCollection, $depth = 0, $maxDepth = null)
 {
     $collection = [];
     $events = $this->getEventManager();
     $routeIdentifierName = $jsonLDCollection->getRouteIdentifierName();
     $entityRoute = $jsonLDCollection->getEntityRoute();
     $entityRouteParams = $jsonLDCollection->getEntityRouteParams();
     $entityRouteOptions = $jsonLDCollection->getEntityRouteOptions();
     $metadataMap = $this->getMetadataMap();
     $entityMetadata = null;
     foreach ($jsonLDCollection->getCollection() as $entity) {
         $eventParams = new ArrayObject(['collection' => $jsonLDCollection, 'entity' => $entity, 'route' => $entityRoute, 'routeParams' => $entityRouteParams, 'routeOptions' => $entityRouteOptions]);
         $events->trigger('renderCollection.entity', $this, $eventParams);
         $entity = $eventParams['entity'];
         if (is_object($entity) && $metadataMap->has($entity)) {
             $entity = $this->getResourceFactory()->createEntityFromMetadata($entity, $metadataMap->get($entity));
         }
         if ($entity instanceof Entity) {
             // Depth does not increment at this level
             $collection[] = $this->renderEntity($entity, $this->getRenderCollections(), $depth, $maxDepth);
             continue;
         }
         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));
             }
             if ($value instanceof Entity) {
                 $this->extractEmbeddedEntity($entity, $key, $value, $depth + 1, $maxDepth);
             }
             if ($value instanceof Collection) {
                 $this->extractEmbeddedCollection($entity, $key, $value, $depth + 1, $maxDepth);
             }
         }
         $id = $this->getIdFromEntity($entity);
         if ($id === false) {
             // Cannot handle entities without an identifier
             // Return as-is
             $collection[] = $entity;
             continue;
         }
         if ($eventParams['entity'] instanceof PropertyCollectionAwareInterface) {
             $properties = $eventParams['entity']->getProperties();
         } else {
             $properties = new PropertyCollection();
         }
         if (isset($entity['properties']) && $entity['properties'] instanceof PropertyCollection) {
             $properties = $entity['properties'];
         }
         if (!isset($entity['id'])) {
             $idProperty = new Property('id');
             $idProperty->setRoute($eventParams['route'], array_merge($eventParams['routeParams'], [$routeIdentifierName => $id]), $eventParams['routeOptions']);
             $properties->add($idProperty);
         }
         $entity = ArrayUtils::merge($entity, $this->fromPropertyCollection($properties));
         $collection[] = $entity;
     }
     return $collection;
 }
コード例 #7
0
 public function setUpChildCollection()
 {
     $children = [['luke', 'Luke Skywalker'], ['leia', 'Leia Organa']];
     $collection = [];
     foreach ($children as $info) {
         $collection[] = call_user_func_array([$this, 'setUpChildEntity'], $info);
     }
     $collection = new Collection($collection);
     $collection->setCollectionRoute('parent/child');
     $collection->setEntityRoute('parent/child');
     $collection->setPage(1);
     $collection->setPageSize(10);
     $collection->setCollectionName('child');
     $property = new Property('id');
     $property->setRoute('parent/child');
     $collection->getProperties()->add($property);
     return $collection;
 }
コード例 #8
0
 /**
  * Inject any properties found in the metadata into the resource's property collection
  *
  * @param  Metadata $metadata
  * @param  PropertyCollection $properties
  */
 public function marshalMetadataProperties(Metadata $metadata, PropertyCollection $properties)
 {
     foreach ($metadata->getProperties() as $propertyData) {
         $property = Property::factory($propertyData);
         $properties->add($property);
     }
 }
コード例 #9
0
ファイル: PropertyTest.php プロジェクト: vixriihi/zf-jsonld
 /**
  * @group 79
  */
 public function testFactoryCanGeneratePropertyWithRouteInformation()
 {
     $rel = 'describedby';
     $route = 'api/docs';
     $params = ['version' => '1.1'];
     $options = ['query' => 'version=1.1'];
     $property = Property::factory(['key' => $rel, 'route' => ['name' => $route, 'params' => $params, 'options' => $options]]);
     $this->assertInstanceOf('ZF\\JsonLD\\Property\\Property', $property);
     $this->assertEquals('describedby', $property->getKeyword());
     $this->assertEquals($route, $property->getRoute());
     $this->assertEquals($params, $property->getRouteParams());
     $this->assertEquals($options, $property->getRouteOptions());
 }
コード例 #10
0
ファイル: JsonLDTest.php プロジェクト: vixriihi/zf-jsonld
 public function testAllowsSpecifyingAlternateCallbackForReturningEntityId()
 {
     $this->plugin->getEventManager()->attach('getIdFromEntity', function ($e) {
         $entity = $e->getParam('entity');
         if (!is_array($entity)) {
             return false;
         }
         if (array_key_exists('name', $entity)) {
             return $entity['name'];
         }
         return false;
     }, 10);
     $prototype = ['foo' => 'bar'];
     $items = [];
     foreach (range(1, 100) as $id) {
         $item = $prototype;
         $item['name'] = $id;
         $items[] = $item;
     }
     $collection = new Collection($items);
     $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));
     $this->assertPropertyEquals('http://localhost.localdomain/resource', 'id', $result);
     $this->assertArrayHasKey('member', $result);
     $this->assertInternalType('array', $result['member']);
     $this->assertEquals(100, count($result['member']));
     foreach ($result['member'] as $key => $item) {
         $id = $key + 1;
         $this->assertPropertyEquals('http://localhost.localdomain/resource/' . $id, 'id', $item);
         $this->assertArrayHasKey('name', $item, var_export($item, 1));
         $this->assertEquals($id, $item['name']);
         $this->assertArrayHasKey('foo', $item);
         $this->assertEquals('bar', $item['foo']);
     }
 }