Example #1
0
 /**
  * Tests the framing API
  *
  * This test intentionally uses the same fixtures as the flattening tests.
  *
  * @group framing
  */
 public function testFrame()
 {
     $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR;
     $expected = json_decode(file_get_contents($path . 'sample-flattened.jsonld'));
     $input = $path . 'sample-in.jsonld';
     $context = $path . 'sample-context.jsonld';
     $this->assertJsonEquals($expected, JsonLD::frame($input, $context), 'Passing the file path');
     $input = file_get_contents($input);
     $context = file_get_contents($context);
     $this->assertJsonEquals($expected, JsonLD::frame($input, $context), 'Passing the raw input (string)');
     $input = json_decode($input);
     $context = json_decode($context);
     $this->assertJsonEquals($expected, JsonLD::frame($input, $context), 'Passing the parsed object');
 }
Example #2
0
    }
  ],
  "@embedChildren": false,
  "supportedProperties": {
    "@default": [ ],
    "@embed": true
  },
  "supportedOperations": {
    "@default": [ ],
    "@embed": true,
    "expects": { "@default": null, "@embed": false },
    "statusCodes": { "@default": [], "@embed": true }
  }
}
    ';
        $document = JsonLD::toString(JsonLD::frame(JsonLD::expand($document, $options), $frame));
        $headers['Content-Type'] = 'application/ld+json';
    } catch (Exception $e) {
        $exceptionName = get_class($e);
        if (false !== ($pos = strrpos(get_class($e), '\\'))) {
            $exceptionName = substr($exceptionName, $pos + 1);
        }
        header('HTTP/1.1 400 ' . $exceptionName);
        //Bad Request');
        print htmlspecialchars($e->getMessage());
        die;
    }
};
$proxy = new AjaxProxy();
if ($debug) {
    $proxy->setResponseModifier($debugExpansion);
Example #3
0
 /**
  * @param \EasyRdf\Graph  $graph
  * @param string          $format
  * @param array           $options
  *
  * @throws Exception
  * @return string
  */
 public function serialise($graph, $format, array $options = array())
 {
     parent::checkSerialiseParams($graph, $format);
     if ($format != 'jsonld') {
         throw new Exception(__CLASS__ . ' does not support: ' . $format);
     }
     $ld_graph = new LD\Graph();
     $nodes = array();
     // cache for id-to-node association
     foreach ($graph->toRdfPhp() as $resource => $properties) {
         if (array_key_exists($resource, $nodes)) {
             $node = $nodes[$resource];
         } else {
             $node = $ld_graph->createNode($resource);
             $nodes[$resource] = $node;
         }
         foreach ($properties as $property => $values) {
             foreach ($values as $value) {
                 if ($value['type'] == 'bnode' or $value['type'] == 'uri') {
                     if (array_key_exists($value['value'], $nodes)) {
                         $_value = $nodes[$value['value']];
                     } else {
                         $_value = $ld_graph->createNode($value['value']);
                         $nodes[$value['value']] = $_value;
                     }
                 } elseif ($value['type'] == 'literal') {
                     if (isset($value['lang'])) {
                         $_value = new LD\LanguageTaggedString($value['value'], $value['lang']);
                     } elseif (isset($value['datatype'])) {
                         $_value = new LD\TypedValue($value['value'], $value['datatype']);
                     } else {
                         $_value = $value['value'];
                     }
                 } else {
                     throw new Exception("Unable to serialise object to JSON-LD: " . $value['type']);
                 }
                 if ($property == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") {
                     $node->addType($_value);
                 } else {
                     $node->addPropertyValue($property, $_value);
                 }
             }
         }
     }
     // OPTIONS
     $use_native_types = !(isset($options['expand_native_types']) and $options['expand_native_types'] == true);
     $should_compact = (isset($options['compact']) and $options['compact'] == true);
     $should_frame = isset($options['frame']);
     // expanded form
     $data = $ld_graph->toJsonLd($use_native_types);
     if ($should_frame) {
         $data = LD\JsonLD::frame($data, $options['frame'], $options);
     }
     if ($should_compact) {
         // compact form
         $compact_context = isset($options['context']) ? $options['context'] : null;
         $compact_options = array('useNativeTypes' => $use_native_types, 'base' => $graph->getUri());
         $data = LD\JsonLD::compact($data, $compact_context, $compact_options);
     }
     return LD\JsonLD::toString($data);
 }
Example #4
0
 /**
  * Tests framing.
  *
  * @param string $name    The test name.
  * @param object $test    The test definition.
  * @param object $options The options to configure the algorithms.
  *
  * @group framing
  * @dataProvider framingProvider
  */
 public function testFraming($name, $test, $options)
 {
     $ignoredTests = array('frame-0005-in.jsonld', 'frame-0009-in.jsonld', 'frame-0010-in.jsonld', 'frame-0012-in.jsonld', 'frame-0013-in.jsonld');
     if (in_array($test->{'input'}, $ignoredTests)) {
         $this->markTestSkipped('This implementation uses deep value matching and aggressive re-embedding. See ISSUE-110 and ISSUE-119.');
     }
     $expected = json_decode(file_get_contents($this->basedir . $test->{'expect'}));
     $result = JsonLD::frame($this->basedir . $test->{'input'}, $this->basedir . $test->{'frame'}, $options);
     $this->assertJsonEquals($expected, $result);
 }