File: TestManifestIterator.php

package info (click to toggle)
php-ml-json-ld 1.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 628 kB
  • sloc: php: 4,521; makefile: 21
file content (120 lines) | stat: -rw-r--r-- 3,062 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php

/*
 * (c) Markus Lanthaler <mail@markus-lanthaler.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ML\JsonLD\Test;

/**
 * TestManifestIterator reads a test manifest and returns the contained test
 * definitions.
 *
 * @author Markus Lanthaler <mail@markus-lanthaler.com>
 */
class TestManifestIterator implements \Iterator
{
    /** The current position. */
    private $key = 0;

    /** The test directory. */
    private $directory;

    /** The test manifest. */
    private $manifest;

    /** The URL of the test manifest. */
    private $url;

    /** The total number of tests. */
    private $numberTests = 0;

    /**
     * Constructor
     *
     * @param string $file The manifest's filename.
     * @param string $url  The manifest's URL.
     */
    public function __construct($file, $url)
    {
        if (file_exists($file)) {
            $this->manifest = json_decode(file_get_contents($file));
            $this->numberTests = count($this->manifest->{'sequence'});
            $this->url = $url;
            $this->directory = dirname($file) . DIRECTORY_SEPARATOR;
        }
    }

    /**
     * Rewinds the TestManifestIterator to the first element.
     */
    public function rewind()
    {
        $this->key = 0;
    }

    /**
     * Checks if current position is valid.
     *
     * @return bool True if the current position is valid; otherwise, false.
     */
    public function valid()
    {
        return ($this->key < $this->numberTests);
    }

    /**
     * Returns the key of the current element.
     *
     * @return string The key of the current element
     */
    public function key()
    {
        return $this->url . $this->manifest->{'sequence'}[$this->key]->{'@id'};
    }

    /**
     * Returns the current element.
     *
     * @return array Returns an array containing the name of the test and the
     *                test definition object.
     */
    public function current()
    {
        $test = $this->manifest->{'sequence'}[$this->key];
        $options = isset($test->{'option'})
            ? clone $test->{'option'}  // cloning because we are modifying it
            : new \stdClass();

        if (false === property_exists($options, 'base')) {
            if (property_exists($this->manifest, 'baseIri')) {
                $options->base = $this->manifest->{'baseIri'} . $test->{'input'};
            } else {
                $options->base = $test->{'input'};
            }
        }

        if (isset($options->{'expandContext'}) && (false === strpos($options->{'expandContext'}, ':'))) {
            $options->{'expandContext'} = $this->directory . $options->{'expandContext'};
        }

        $test = array(
            'name'    => $test->{'name'},
            'test'    => $test,
            'options' => $options
        );

        return $test;
    }

    /**
     * Moves forward to next element.
     */
    public function next()
    {
        $this->key++;
    }
}