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
|
<?php
namespace SebLucas\EPubMeta\Data;
use ArrayAccess;
use Countable;
use Exception;
use BadMethodCallException;
use Iterator;
/**
* EPUB manifest structure
*
* @author Simon Schrape <simon@epubli.com>
* @implements \Iterator<string, Item>
* @implements \ArrayAccess<string, Item>
*/
class Manifest implements Iterator, Countable, ArrayAccess
{
/** @var array|Item[] The map of all Items in this Manifest indexed by their IDs. */
protected $items = [];
/**
* Create and add an Item with the given properties.
*
* @param string $id The identifier of the new item.
* @param string $href The relative path of the referenced file in the EPUB.
* @param callable $callable A callable to get data from the referenced file in the EPUB.
* @param int $size The size of the referenced file in the EPUB.
* @param string|null $mediaType
* @return Item The newly created Item.
* @throws Exception If $id is already taken.
*/
public function createItem($id, $href, $callable, $size, $mediaType = null)
{
if (isset($this->items[$id])) {
throw new Exception("Item with ID $id already exists!");
}
$item = new Item($id, $href, $callable, $size, $mediaType);
$this->items[$id] = $item;
return $item;
}
/**
* Return the current Item while iterating this Manifest.
*
* @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 Manifest.
* @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 ID of the current Item while iterating this Manifest.
*
* @link http://php.net/manual/en/iterator.key.php
* @return string|null on success, or null on failure.
*/
public function key(): ?string
{
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 Manifest.
*
* @return Item
*/
public function first()
{
return reset($this->items);
}
/**
* Get the last Item of this Manifest.
*
* @return Item
*/
public function last()
{
return end($this->items);
}
/**
* Count items of this Manifest.
*
* @link https://php.net/manual/en/countable.count.php
* @return int The number of Items contained in this Manifest.
*/
public function count(): int
{
return count($this->items);
}
/**
* Whether a offset exists
* @link https://php.net/manual/en/arrayaccess.offsetexists.php
* @param string $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 string $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!");
}
}
|