1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace musa11971\TVDB; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use Countable; |
7
|
|
|
use Iterator; |
8
|
|
|
|
9
|
|
|
class EpisodeCollection implements ArrayAccess, Iterator, Countable { |
10
|
|
|
public $page; |
11
|
|
|
public $episodes; |
12
|
|
|
protected $iteratorPosition = 0; |
13
|
|
|
|
14
|
|
|
public function __construct($page, $episodes) { |
15
|
|
|
$this->page = $page; |
16
|
|
|
$this->episodes = $episodes; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Checks whether or not the episode collection has a follow-up page |
21
|
|
|
* .. with more results |
22
|
|
|
* |
23
|
|
|
* @return bool |
24
|
|
|
*/ |
25
|
|
|
public function hasNextPage() { |
26
|
|
|
return (count($this->episodes) >= TVDB::EPISODES_PER_PAGE); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Returns the page number of the next page |
31
|
|
|
* |
32
|
|
|
* @return int |
33
|
|
|
*/ |
34
|
|
|
public function nextPage() { |
35
|
|
|
return ($this->page + 1); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** Method from interface Countable */ |
39
|
|
|
public function count() |
40
|
|
|
{ |
41
|
|
|
return count($this->episodes); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** Methods from interface ArrayAccess */ |
45
|
|
|
public function offsetSet($offset, $value) { |
46
|
|
|
if(is_null($offset)) { |
47
|
|
|
$this->episodes[] = $value; |
48
|
|
|
} else { |
49
|
|
|
$this->episodes[$offset] = $value; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function offsetExists($offset) { |
54
|
|
|
return isset($this->episodes[$offset]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function offsetUnset($offset) { |
58
|
|
|
unset($this->episodes[$offset]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function offsetGet($offset) { |
62
|
|
|
return isset($this->episodes[$offset]) ? $this->episodes[$offset] : null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** Methods from interface Iterator */ |
66
|
|
|
public function rewind() { |
67
|
|
|
$this->iteratorPosition = 0; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function current() { |
71
|
|
|
return $this->episodes[$this->iteratorPosition]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function key() { |
75
|
|
|
return $this->iteratorPosition; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function next() { |
79
|
|
|
++$this->iteratorPosition; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function valid() { |
83
|
|
|
return isset($this->episodes[$this->iteratorPosition]); |
84
|
|
|
} |
85
|
|
|
} |