-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamIterator.php
249 lines (232 loc) · 6.77 KB
/
StreamIterator.php
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
namespace librdf;
/* $Id: Iterator.php 171 2006-06-15 23:24:18Z das-svn $ */
/**
* Wrap a librdf_stream as a PHP iterator using SPL.
*
* Despite the name, the class in this file wraps librdf_stream, not
* librdf_iterator. Since the node iteration functions in {@link Model}
* were not implemented in favor of only {@link Model#findStatements},
* the only need for an iterable object was one using the stream of
* Statements.
*
* PHP version 5
*
* Copyright (C) 2006, David Shea <[email protected]>
*
* LICENSE: This package is Free Software and a derivative work of Redland
* http://librdf.org/. This package is not endorsed by Dave Beckett or the
* University of Bristol. It is licensed under the following three licenses as
* alternatives:
* 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
* 2. GNU General Public License (GPL) V2 or any newer version
* 3. Apache License, V2.0 or any newer version
*
* You may not use this file except in compliance with at least one of the
* above three licenses.
*
* See LICENSE.txt at the top of this package for the complete terms and futher
* detail along with the license tests for the licenses in COPYING.LIB, COPYING
* and LICENSE-2.0.txt repectively.
*
* @package LibRDF
* @author David Shea <[email protected]>
* @copyright 2006 David Shea
* @license LGPL/GPL/APACHE
* @version Release: 1.0.0
* @link http://reallylongword.org/projects/librdf-php/
*/
/**
*/
use librdf\exception\Error;
use librdf\Statement;
/**
* Wrap a librdf_stream resource as an iterable object.
*
* This class should not be created directly, nor are its methods of interest
* to the casual user. Its only intent is to provide a return type for
* Model::find_statements, as well as the underlying iterator for both
* Model and GraphQueryResults, that can then be used in a PHP
* foreach statement without any direct function calls.
*
* Objects of this type may only be used for iteration once. Once iteration
* has begun, a call to rewind will render the object invalid.
*
* @package LibRDF
* @author David Shea <[email protected]>
* @copyright 2006 David Shea
* @license LGPL/GPL/APACHE
* @version Release: 1.0.0
* @link http://reallylongword.org/projects/librdf-php/
*/
class StreamIterator implements \Iterator
{
/**
* A cache of whether the iterator is still valid.
*
* @var boolean
* @access private
*/
private $isvalid;
/**
* The underlying librdf_stream resource.
*
* @var resource
* @access private
*/
private $stream;
/**
* An integer used to provide keys over the iteration.
*
* There are no keys created by the librdf_stream data, so iteration
* keys are created as an integer with an initial value of 0 increasing
* by one for each call of {@link next}.
*
* @var integer
* @access private
*/
private $key;
/**
* A reference to the stream's source object to prevent it from being
* garbage collected before the stream.
*
* @var mixed
* @access private
*/
private $source;
/**
* A flag for whether the stream is rewindable.
*
* A stream may be rewound before {@link next} is called, after which
* rewinding invalidates the stream.
*
* @var boolean
* @access private
*/
private $rewindable;
/**
* Create a new iterable object from a librdf_stream resource.
*
* User functions should not create librdf_stream resources directly,
* so this constructor is intended only to provide an interface into the
* streams returned by librdf functions and called by LibRDF classes.
*
* @param resource $stream The librdf_stream object to wrap
* @param mixed $source The object that created the stream
* @return void
* @access public
*/
public function __construct($stream, $source=NULL)
{
$this->stream = $stream;
$this->isvalid = true;
$this->key = 0;
$this->source = $source;
$this->rewindable = true;
}
/**
* Free the stream's resources.
*
* @return void
* @access public
*/
public function __destruct()
{
if ($this->stream) {
librdf_free_stream($this->stream);
}
}
/**
* Clone a StreamIterator object.
*
* Cloning a stream is unsupported, so using the clone operator on a
* StreamIterator object will produce an empty iterator.
*
* @return void
* @access public
*/
public function __clone()
{
$this->stream = NULL;
$this->isvalid = false;
}
/**
* Rewind the stream.
*
* Rewinding is not supported, so this call invalidates the stream unless
* the stream is still at the starting position.
*
* @return void
* @access public
*/
public function rewind()
{
if (!($this->rewindable)) {
$this->isvalid = false;
}
}
/**
* Return the current statement or NULL if the stream is no longer valid.
*
* @return Statement The current statement on the iterator
* @access public
*/
public function current()
{
if (($this->isvalid) and (!librdf_stream_end($this->stream))) {
// the pointer returned is overwritten when the stream is
// advanced or closed, so make a copy of the statement
$ret = librdf_stream_get_object($this->stream);
if ($ret) {
return new Statement(librdf_new_statement_from_statement($ret));
} else {
throw new Error("Unable to get current statement");
}
} else {
return NULL;
}
}
/**
* Return the key of the current element on the stream.
*
* @return integer The current key
* @access public
*/
public function key()
{
return $this->key;
}
/**
* Advance the stream's position.
*
* @return void
* @access public
*/
public function next()
{
if ($this->isvalid) {
$this->rewindable = false;
$ret = librdf_stream_next($this->stream);
if ($ret) {
$this->isvalid = false;
} else {
$this->key++;
}
}
}
/**
* Return whether the stream is still valid.
*
* @return boolean Whether the stream is still valid
* @access public
*/
public function valid()
{
if (($this->isvalid) and (!librdf_stream_end($this->stream))) {
return true;
} else {
return false;
}
}
}
?>