Example #1
0
 /**
  * Test whether it's valid json-ld.
  */
 public function testJsonLD()
 {
     //Launch a query in various ways
     $jsonld1 = Stations::getStations('Brussel');
     $jsonld2 = Stations::getStations();
     //Assert whether the json ld is valid
     $doc1 = JsonLD::getDocument($jsonld1);
     $doc2 = JsonLD::getDocument($jsonld2);
     //Assert whether nodes exist
     //E.g., the default graph of doc1 should be http://irail.be/stations/NMBS?q=Brussel
     $this->assertTrue($doc1->containsGraph('http://irail.be/stations/NMBS?q=Brussel'));
     //E.g., the default graph of doc2 should be http://irail.be/stations/NMBS
     $this->assertTrue($doc2->containsGraph('http://irail.be/stations/NMBS'));
 }
Example #2
0
 /**
  * Tests the document API
  *
  * This test intentionally uses the same fixtures as the flattening tests.
  */
 public function testGetDocument()
 {
     $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR;
     $expected = json_decode(file_get_contents($path . 'sample-serialized-document.jsonld'));
     $input = $path . 'sample-in.jsonld';
     $this->assertJsonEquals($expected, JsonLD::getDocument($input)->toJsonLd(), 'Passing the file path');
     $input = file_get_contents($input);
     $this->assertJsonEquals($expected, JsonLD::getDocument($input)->toJsonLd(), 'Passing the raw input (string)');
     $input = json_decode($input);
     $this->assertJsonEquals($expected, JsonLD::getDocument($input)->toJsonLd(), 'Passing the parsed object');
 }
Example #3
0
 /**
  * Create the document to test.
  */
 protected function setUp()
 {
     $this->document = JsonLD::getDocument(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'dataset.jsonld', array('base' => 'http://example.com/dataset.jsonld'));
 }
 /**
  * Deserializes JSON-LD data
  *
  * @param mixed  $data   The data to deserialize.
  * @param object $entity The entity into which the data should be
  *                       deserialized.
  *
  * @return object The entity.
  */
 private function doDeserialize($data, $entity)
 {
     $metadata = $this->hydraApi->getMetadataFor(get_class($entity));
     if (null === $metadata) {
         // TODO Improve this error message
         throw new \Exception(sprintf('"%s" cannot be serialized as it is not documented.', get_class($data)));
     }
     $vocabPrefix = $this->router->generate('hydra_vocab', array(), true) . '#';
     $typeIri = $metadata->isExternalReference() ? $metadata->getIri() : $vocabPrefix . $metadata->getIri();
     $graph = JsonLD::getDocument($data)->getGraph();
     $node = $graph->getNodesByType($typeIri);
     if (1 !== count($node)) {
         throw new RuntimeException('The passed data contains ' . count($node) . ' nodes of the type ' . $typeIri . '; expected 1.');
     }
     $node = reset($node);
     foreach ($metadata->getProperties() as $property) {
         if ($property->isReadOnly()) {
             continue;
         }
         // TODO Parse route!
         if (null !== ($route = $property->getRoute())) {
             continue;
             // FIXXE Handle properties whose value are URLs
         }
         // TODO Recurse!?
         $propertyIri = $property->isExternalReference() ? $property->getIri() : $vocabPrefix . $property->getIri();
         $value = $node->getProperty($propertyIri);
         if ($value instanceof \ML\JsonLD\Value) {
             $value = $value->getValue();
         }
         if (!is_null($value) && $this->hydraApi->hasNormalizer($property->getType())) {
             $normalizer = $this->hydraApi->getNormalizer($property->getType());
             $value = $normalizer->denormalize($value, $property->getType());
         }
         $property->setValue($entity, $value);
         // TODO Fix IRI construction
         // if (is_array($value) || ($value instanceof \ArrayAccess) || ($value instanceof \Travesable)) {
         //     $result[$property] = array();
         //     foreach ($value as $val) {
         //         $result[$property][] = $this->doSerialize($val);
         //     }
         // } else {
         //     $result[$property] = $value;
         // }
     }
     return $entity;
 }
Example #5
0
 /**
  * Parses a JSON-LD document and returns it as a Document
  *
  * The document can be supplied directly as a string or by passing a
  * file path or an IRI.
  *
  * Usage:
  *  <code>
  *    $document = Document::load('document.jsonld');
  *  </code>
  *
  * <strong>Please note that currently all data is merged into the
  *   default graph, named graphs are not supported yet!</strong>
  *
  * It is possible to configure the processing by setting the options
  * parameter accordingly. Available options are:
  *
  *   - <em>base</em>     The base IRI of the input document.
  *
  * @param string|array|object $document The JSON-LD document to process.
  * @param null|array|object   $options  Options to configure the processing.
  *
  * @return Document The parsed JSON-LD document.
  *
  * @throws ParseException If the JSON-LD input document is invalid.
  */
 public static function load($document, $options = null)
 {
     return JsonLD::getDocument($document, $options);
 }
Example #6
0
 /**
  * @param string $path
  * @return HydraClass
  */
 public static function fromPath($path)
 {
     $doc = JsonLD::getDocument($path);
     $graph = $doc->getGraph();
     return static::fromGraph($graph);
 }