-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathQueryResult.php
More file actions
126 lines (112 loc) · 3.08 KB
/
Copy pathQueryResult.php
File metadata and controls
126 lines (112 loc) · 3.08 KB
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
<?php
namespace Scriptotek\Marc;
use ArrayAccess;
use ArrayIterator;
use Countable;
use File_MARC_Field;
use File_MARC_Reference;
use File_MARC_Subfield;
use IteratorAggregate;
use Traversable;
use Scriptotek\Marc\Fields\Field;
class QueryResult implements IteratorAggregate, ArrayAccess, Countable
{
protected $ref;
protected array $data;
protected array $content;
/**
* QueryResult constructor.
*
* @param File_MARC_Reference $ref
*/
public function __construct(File_MARC_Reference $ref)
{
$this->ref = $ref->ref;
$this->data = $ref->data;
$this->content = $ref->content;
for ($i = 0; $i < count($this->data); $i++) {
if (is_a($this->data[$i], File_MARC_Field::class)) {
$this->data[$i] = new Field($this->data[$i]);
}
}
}
public function getReference()
{
return $this->ref;
}
/**
* Get the first result (field or subfield), or null if no results.
*
* @return Field|File_MARC_Subfield|null
*/
public function first(): Field|File_MARC_Subfield|null
{
return $this->data[0] ?? null;
}
/**
* Get the text content of the first result, or null if no results.
*
* @return string|null
*/
public function text(): ?string
{
return $this->content[0] ?? null;
}
/**
* Retrieve an external iterator
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
* @return Traversable
*/
public function getIterator(): Traversable|ArrayIterator
{
return new ArrayIterator($this->data);
}
/**
* Whether a offset exists
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
* @param mixed $offset An offset to check for.
* @return boolean true on success or false on failure.
*/
public function offsetExists(mixed $offset): bool
{
return isset($this->data[$offset]);
}
/**
* Offset to retrieve
* @link http://php.net/manual/en/arrayaccess.offsetget.php
* @param mixed $offset The offset to retrieve.
* @return Field|File_MARC_Subfield|null
*/
public function offsetGet(mixed $offset): Field|File_MARC_Subfield|null
{
return $this->data[$offset];
}
/**
* Offset to set
* @link http://php.net/manual/en/arrayaccess.offsetset.php
* @param mixed $offset The offset to assign the value to.
* @param mixed $value The value to set.
*/
public function offsetSet(mixed $offset, mixed $value): void
{
$this->data[$offset] = $value;
}
/**
* Offset to unset
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
* @param mixed $offset The offset to unset.
*/
public function offsetUnset(mixed $offset): void
{
unset($this->data[$offset]);
}
/**
* Count elements of an object
* @link http://php.net/manual/en/countable.count.php
* @return int The number of results
*/
public function count(): int
{
return count($this->data);
}
}