-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProxySourceItem.php
147 lines (123 loc) · 3.67 KB
/
ProxySourceItem.php
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
<?php
/*
* This file is a part of Sculpin.
*
* (c) Dragonfly Development Inc.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sculpin\Contrib\ProxySourceCollection;
use Sculpin\Core\Source\ProxySource;
class ProxySourceItem extends ProxySource implements \ArrayAccess
{
private $previousItem;
private $nextItem;
public function id()
{
return $this->sourceId();
}
public function meta()
{
return $this->data()->export();
}
public function url()
{
return $this->permalink()->relativeUrlPath();
}
public function date()
{
$calculatedDate = $this->data()->get('calculated_date');
return $calculatedDate ?: $this->data()->get('date');
}
public function title()
{
return $this->data()->get('title');
}
public function blocks()
{
return $this->data()->get('blocks');
}
public function setBlocks(array $blocks = null)
{
$this->data()->set('blocks', $blocks ?: array());
}
public function previousItem()
{
return $this->previousItem;
}
public function setPreviousItem(ProxySourceItem $item = null)
{
$lastPreviousItem = $this->previousItem;
$this->previousItem = $item;
if ($lastPreviousItem) {
// We did have a item before...
if (!$item || $item->id() !== $lastPreviousItem->id()) {
// But we no longer have a item or the item we
// were given does not have the same ID as the
// last one we had...
$this->reprocess();
}
} elseif ($item) {
// We didn't have a item before but we do now...
$this->reprocess();
}
}
public function nextItem()
{
return $this->nextItem;
}
public function setNextItem(ProxySourceItem $item = null)
{
$lastNextItem = $this->nextItem;
$this->nextItem = $item;
if ($lastNextItem) {
// We did have a item before...
if (!$item || $item->id() !== $lastNextItem->id()) {
// But we no longer have a item or the item we
// were given does not have the same ID as the
// last one we had...
$this->reprocess();
}
} elseif ($item) {
// We didn't have a item before but we do now...
$this->reprocess();
}
}
public function reprocess()
{
$this->setHasChanged();
}
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
throw new \InvalidArgumentException("Proxy source items cannot have values pushed onto them");
} else {
if (method_exists($this, $offset)) {
return call_user_func(array($this, $offset, $value));
}
$setMethod = 'set'.ucfirst($offset);
if (method_exists($this, $setMethod)) {
return call_user_func(array($this, $setMethod, $value));
}
$this->data()->set($offset, $value);
}
}
public function offsetExists($offset)
{
return ! method_exists($this, $offset) && null !== $this->data()->get($offset);
}
public function offsetUnset($offset)
{
if (! method_exists($this, $offset)) {
$this->data()->remove($offset);
}
}
public function offsetGet($offset)
{
if (method_exists($this, $offset)) {
return call_user_func(array($this, $offset));
}
return $this->data()->get($offset);
}
}