blob: e6dfc2a84239a963c04586b864ad6eb094cd67bb (
plain) (
blame)
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
|
<?php
namespace SebLucas\EPubMeta\Contents;
use SebLucas\EPubMeta\Data\Item;
use ArrayAccess;
use Countable;
use Iterator;
use BadMethodCallException;
/**
* EPUB spine structure
*
* @author Simon Schrape <simon@epubli.com>
* @implements \Iterator<int, Item>
* @implements \ArrayAccess<int, Item>
*/
class Spine implements Iterator, Countable, ArrayAccess
{
/** @var Item */
protected $tocItem;
protected string $tocFormat;
/** @var array|Item[] The ordered list of all Items in this Spine. */
protected $items = [];
/**
* Spine Constructor.
*
* @param Item $tocItem The TOC Item of this Spine.
* @param string $tocFormat The TOC Format of this Spine (Toc or Nav).
*/
public function __construct(Item $tocItem, string $tocFormat)
{
$this->tocItem = $tocItem;
$this->tocFormat = $tocFormat;
}
/**
* Get the TOC Item of this Spine.
*
* @return Item
*/
public function getTocItem()
{
return $this->tocItem;
}
/**
* Get the TOC Format of this Spine.
*
* @return string
*/
public function getTocFormat()
{
return $this->tocFormat;
}
/**
* Append an Item to this Spine.
*
* @param Item $item The Item to append to this Spine.
* @return void
*/
public function appendItem(Item $item)
{
$this->items[] = $item;
}
/**
* Return the current Item while iterating this Spine.
*
* @link http://php.net/manual/en/iterator.current.php
* @return Item
*/
public function current(): Item
{
return current($this->items);
}
/**
* Move forward to next Item while iterating this Spine.
* @link http://php.net/manual/en/iterator.next.php
* @return void Any returned value is ignored.
*/
public function next(): void
{
next($this->items);
}
/**
* Return the index of the current Item while iterating this Spine.
*
* @link http://php.net/manual/en/iterator.key.php
* @return int|null on success, or null on failure.
*/
public function key(): ?int
{
return key($this->items);
}
/**
* Checks if current Iterator position is valid.
*
* @link http://php.net/manual/en/iterator.valid.php
* @return boolean true on success or false on failure.
*/
public function valid(): bool
{
return (bool) current($this->items);
}
/**
* Rewind the Iterator to the first element.
*
* @link http://php.net/manual/en/iterator.rewind.php
* @return void Any returned value is ignored.
*/
public function rewind(): void
{
reset($this->items);
}
/**
* Get the first Item of this Spine.
*
* @return Item
*/
public function first()
{
return reset($this->items);
}
/**
* Get the last Item of this Spine.
*
* @return Item
*/
public function last()
{
return end($this->items);
}
/**
* Count items of this Spine.
*
* @link https://php.net/manual/en/countable.count.php
* @return int The number of Items contained in this Spine.
*/
public function count(): int
{
return count($this->items);
}
/**
* Whether a offset exists
* @link https://php.net/manual/en/arrayaccess.offsetexists.php
* @param int $offset An offset to check for.
* @return boolean true on success or false on failure.
*/
public function offsetExists($offset): bool
{
return isset($this->items[$offset]);
}
/**
* Offset to retrieve
* @link https://php.net/manual/en/arrayaccess.offsetget.php
* @param int $offset The offset to retrieve.
* @return Item
*/
public function offsetGet($offset): Item
{
return $this->items[$offset];
}
/**
* Offset to set
* @link https://php.net/manual/en/arrayaccess.offsetset.php
* @param mixed $offset The offset to assign the value to.
* @param mixed $value The value to set.
* @throws BadMethodCallException
*/
public function offsetSet($offset, $value): void
{
throw new BadMethodCallException("Only reading array access is supported!");
}
/**
* Offset to unset
* @link https://php.net/manual/en/arrayaccess.offsetunset.php
* @param mixed $offset The offset to unset.
* @throws BadMethodCallException
*/
public function offsetUnset($offset): void
{
throw new BadMethodCallException("Only reading array access is supported!");
}
}
|