|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Phile\Repository; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Page collection which delays searching for and loading pages until necessary. |
|
7
|
|
|
* |
|
8
|
|
|
* @author PhileCMS |
|
9
|
|
|
* @link https://philecms.github.io |
|
10
|
|
|
* @license http://opensource.org/licenses/MIT |
|
11
|
|
|
* @package Phile\Repository |
|
12
|
|
|
*/ |
|
13
|
|
|
class PageCollection implements \ArrayAccess, \IteratorAggregate, \Countable |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var callable A function to be used for loading the pages. |
|
17
|
|
|
*/ |
|
18
|
|
|
private $loader; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array of \Phile\Model\Page |
|
22
|
|
|
*/ |
|
23
|
|
|
private $pages; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param callable $loader pages loader |
|
29
|
|
|
*/ |
|
30
|
13 |
|
public function __construct(callable $loader) |
|
31
|
|
|
{ |
|
32
|
13 |
|
$this->loader = $loader; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Perform page loading. |
|
37
|
|
|
* |
|
38
|
|
|
* @return void |
|
39
|
|
|
*/ |
|
40
|
13 |
|
private function load() |
|
41
|
|
|
{ |
|
42
|
13 |
|
if ($this->pages === null) { |
|
43
|
13 |
|
$this->pages = call_user_func($this->loader); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get pages in a array. |
|
49
|
|
|
* |
|
50
|
|
|
* @return array of \Phile\Model\Page |
|
51
|
|
|
*/ |
|
52
|
2 |
|
public function toArray() |
|
53
|
|
|
{ |
|
54
|
2 |
|
$this->load(); |
|
55
|
1 |
|
return $this->pages; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
8 |
|
public function getIterator(): \Traversable |
|
59
|
|
|
{ |
|
60
|
8 |
|
$this->load(); |
|
61
|
8 |
|
return new \ArrayIterator($this->pages); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function offsetExists($offset): bool |
|
65
|
|
|
{ |
|
66
|
|
|
$this->load(); |
|
67
|
|
|
return isset($this->pages[$offset]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
2 |
|
public function offsetGet($offset): mixed |
|
71
|
|
|
{ |
|
72
|
2 |
|
$this->load(); |
|
73
|
2 |
|
return $this->pages[$offset]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function offsetSet($offset, $value): void |
|
77
|
|
|
{ |
|
78
|
|
|
$this->load(); |
|
79
|
|
|
$this->pages[$offset] = $value; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function offsetUnset($offset): void |
|
83
|
|
|
{ |
|
84
|
|
|
$this->load(); |
|
85
|
|
|
unset($this->pages[$offset]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
public function count(): int |
|
89
|
|
|
{ |
|
90
|
2 |
|
$this->load(); |
|
91
|
2 |
|
return count($this->pages); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|