function doPOST()
 {
     // FIXME This should be done at apache level probably...
     $this->allowFromKey();
     // 		$p = $this->assertNotEmpty('p');
     $action = $this->assertNotEmpty('action');
     $graph = $this->assertNotEmpty('graph');
     // 		$type = $this->assertNotEmpty('type');
     if ($action == 'put') {
         $json = $this->assertNotEmpty('json');
         $json = json_decode(base64_decode($json));
         switch (json_last_error()) {
             case JSON_ERROR_DEPTH:
                 $error = ' - Maximum stack depth exceeded';
                 break;
             case JSON_ERROR_CTRL_CHAR:
                 $error = ' - Unexpected control character found';
                 break;
             case JSON_ERROR_SYNTAX:
                 $error = ' - Syntax error, malformed JSON';
                 break;
             case JSON_ERROR_NONE:
             default:
                 $error = '';
         }
         if ($error) {
             _error('Broken JSON: ' . $error);
         }
         $ntriples = jsonld_normalize($json, array('format' => 'application/nquads'));
         $this->parameters['data'] = $ntriples;
         //put_ntriples($ntriples, $graph);
         //$output = $ntriples;
         //header("HTTP/1.1 " . 200, true, 200);
         return $this->_doPUT();
     } else {
         if ($action == 'delete') {
             return $this->doDELETE();
         }
     }
     //
     exit(0);
     // Die happy
 }
Exemplo n.º 2
0
 public function run($manifests)
 {
     /* Manifest format: {
            name: <optional manifest name>,
            sequence: [{
              'name': <test name>,
              '@type': ["test:TestCase", "jld:<type of test>"],
              'input': <input file for test>,
              'context': <context file for add context test type>,
              'frame': <frame file for frame test type>,
              'expect': <expected result file>,
            }]
          }
        */
     global $eol;
     foreach ($manifests as $manifest) {
         if (property_exists($manifest, 'name')) {
             $this->group($manifest->name);
         }
         $filepath = $manifest->filepath;
         foreach ($manifest->sequence as $test) {
             // read test input files
             $type = $test->{'@type'};
             $options = array('base' => 'http://json-ld.org/test-suite/tests/' . $test->input);
             try {
                 if (in_array('jld:NormalizeTest', $type)) {
                     $this->test($test->name);
                     $input = read_test_json($test->input, $filepath);
                     $test->expect = read_test_nquads($test->expect, $filepath);
                     $options['format'] = 'application/nquads';
                     $result = jsonld_normalize($input, $options);
                 } else {
                     if (in_array('jld:ExpandTest', $type)) {
                         $this->test($test->name);
                         $input = read_test_json($test->input, $filepath);
                         $test->expect = read_test_json($test->expect, $filepath);
                         $result = jsonld_expand($input, $options);
                     } else {
                         if (in_array('jld:CompactTest', $type)) {
                             $this->test($test->name);
                             $input = read_test_json($test->input, $filepath);
                             $test->context = read_test_json($test->context, $filepath);
                             $test->expect = read_test_json($test->expect, $filepath);
                             $result = jsonld_compact($input, $test->context, $options);
                         } else {
                             if (in_array('jld:FrameTest', $type)) {
                                 $this->test($test->name);
                                 $input = read_test_json($test->input, $filepath);
                                 $test->frame = read_test_json($test->frame, $filepath);
                                 $test->expect = read_test_json($test->expect, $filepath);
                                 $result = jsonld_frame($input, $test->frame, $options);
                             } else {
                                 if (in_array('jld:FromRDFTest', $type)) {
                                     $this->test($test->name);
                                     $input = read_test_nquads($test->input, $filepath);
                                     $test->expect = read_test_json($test->expect, $filepath);
                                     $result = jsonld_from_rdf($input, $options);
                                 } else {
                                     if (in_array('jld:ToRDFTest', $type)) {
                                         $this->test($test->name);
                                         $input = read_test_json($test->input, $filepath);
                                         $test->expect = read_test_nquads($test->expect, $filepath);
                                         $options['format'] = 'application/nquads';
                                         $result = jsonld_to_rdf($input, $options);
                                     } else {
                                         echo "Skipping test \"{$test->name}\" of type: " . json_encode($type) . $eol;
                                         continue;
                                     }
                                 }
                             }
                         }
                     }
                 }
                 // check results
                 $this->check($test, $test->expect, $result);
             } catch (JsonLdException $e) {
                 echo $eol . $e;
                 $this->failed += 1;
                 echo "FAIL{$eol}";
             }
         }
         if (property_exists($manifest, 'name')) {
             $this->ungroup();
         }
     }
 }
Exemplo n.º 3
0
/**
 * Frames JSON-LD input.
 *
 * @param input the JSON-LD input.
 * @param frame the frame to use.
 * @param options framing options to use.
 *
 * @return the framed output.
 */
function jsonld_frame($input, $frame, $options = null)
{
    $rval;
    // normalize input
    $input = jsonld_normalize($input);
    // save frame context
    $ctx = null;
    if (property_exists($frame, '@context')) {
        $ctx = _clone($frame->{'@context'});
    }
    // remove context from frame
    $frame = jsonld_expand($frame);
    // create framing options
    // TODO: merge in options from function parameter
    $options = new stdClass();
    $options->defaults = new stdClass();
    $options->defaults->embedOn = true;
    $options->defaults->explicitOn = false;
    $options->defaults->omitDefaultOn = false;
    // build map of all subjects
    $subjects = new stdClass();
    foreach ($input as $i) {
        $subjects->{$i->{__S}->{'@iri'}} = $i;
    }
    // frame input
    $rval = _frame($subjects, $input, $frame, new stdClass(), $options);
    // apply context
    if ($ctx !== null and $rval !== null) {
        $rval = jsonld_compact($ctx, $rval);
    }
    return $rval;
}
Exemplo n.º 4
0
 function to_jsonld_string()
 {
     $r = array();
     $stream = librdf_model_as_stream($this->_model);
     while (!librdf_stream_end($stream)) {
         $elt = $this->_statement(librdf_stream_get_object($stream));
         $d = new \stdClass();
         $d->{'@subject'} = $elt[0]['value'];
         if ($elt[2]['type'] == 'literal') {
             if (isset($elt[2]['datatype'])) {
                 $d->{$elt[1]['value']} = (object) array('@literal' => $elt[2]['value'], '@datatype' => $elt[2]['datatype']);
             } else {
                 $d->{$elt[1]['value']} = $elt[2]['value'];
             }
         } else {
             $d->{$elt[1]['value']} = (object) array('@iri' => $elt[2]['value']);
         }
         $r[] = $d;
         librdf_stream_next($stream);
     }
     librdf_free_stream($stream);
     return str_replace('\\/', '/', json_encode(jsonld_normalize($r)));
 }