コード例 #1
0
ファイル: jsonld-tests.php プロジェクト: iAhmadZain/iksce
 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();
         }
     }
 }
コード例 #2
0
ファイル: jsonld.php プロジェクト: Sunnepah/ldphp
/**
 * 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;
}