1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is a part of Sculpin. |
5
|
|
|
* |
6
|
|
|
* (c) Dragonfly Development Inc. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Symplify\PHP7_Sculpin\Contrib\ProxySourceCollection; |
13
|
|
|
|
14
|
|
|
use Symplify\PHP7_Sculpin\Core\Source\ProxySource; |
15
|
|
|
|
16
|
|
|
class ProxySourceItem extends ProxySource implements \ArrayAccess |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var |
20
|
|
|
*/ |
21
|
|
|
private $previousItem; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var |
25
|
|
|
*/ |
26
|
|
|
private $nextItem; |
27
|
|
|
|
28
|
|
|
public function id() |
29
|
|
|
{ |
30
|
|
|
return $this->sourceId(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function meta() |
34
|
|
|
{ |
35
|
|
|
return $this->data()->export(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function url() |
39
|
|
|
{ |
40
|
|
|
return $this->permalink()->relativeUrlPath(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// public function date() |
|
|
|
|
44
|
|
|
// { |
45
|
|
|
// $calculatedDate = $this->data()->get('calculated_date'); |
46
|
|
|
|
47
|
|
|
// return $calculatedDate ?: $this->data()->get('date'); |
|
|
|
|
48
|
|
|
// } |
49
|
|
|
|
50
|
|
|
// public function title() |
|
|
|
|
51
|
|
|
// { |
52
|
|
|
// return $this->data()->get('title'); |
53
|
|
|
// } |
54
|
|
|
|
55
|
|
|
public function blocks() |
56
|
|
|
{ |
57
|
|
|
return $this->data()->get('blocks'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function setBlocks(array $blocks = null) |
61
|
|
|
{ |
62
|
|
|
$this->data()->set('blocks', $blocks ?: []); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function previousItem() |
66
|
|
|
{ |
67
|
|
|
return $this->previousItem; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function setPreviousItem(ProxySourceItem $item = null) |
71
|
|
|
{ |
72
|
|
|
$lastPreviousItem = $this->previousItem; |
73
|
|
|
$this->previousItem = $item; |
74
|
|
|
if ($lastPreviousItem) { |
75
|
|
|
// We did have a item before... |
76
|
|
|
if (!$item || $item->id() !== $lastPreviousItem->id()) { |
77
|
|
|
// But we no longer have a item or the item we |
78
|
|
|
// were given does not have the same ID as the |
79
|
|
|
// last one we had... |
80
|
|
|
$this->reprocess(); |
81
|
|
|
} |
82
|
|
|
} elseif ($item) { |
83
|
|
|
// We didn't have a item before but we do now... |
84
|
|
|
$this->reprocess(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function nextItem() |
89
|
|
|
{ |
90
|
|
|
return $this->nextItem; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function setNextItem(ProxySourceItem $item = null) |
94
|
|
|
{ |
95
|
|
|
$lastNextItem = $this->nextItem; |
96
|
|
|
$this->nextItem = $item; |
97
|
|
|
if ($lastNextItem) { |
98
|
|
|
// We did have a item before... |
99
|
|
|
if (!$item || $item->id() !== $lastNextItem->id()) { |
100
|
|
|
// But we no longer have a item or the item we |
101
|
|
|
// were given does not have the same ID as the |
102
|
|
|
// last one we had... |
103
|
|
|
$this->reprocess(); |
104
|
|
|
} |
105
|
|
|
} elseif ($item) { |
106
|
|
|
// We didn't have a item before but we do now... |
107
|
|
|
$this->reprocess(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function reprocess() |
112
|
|
|
{ |
113
|
|
|
$this->setHasChanged(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function offsetSet($offset, $value) |
117
|
|
|
{ |
118
|
|
|
if (is_null($offset)) { |
119
|
|
|
throw new \InvalidArgumentException('Proxy source items cannot have values pushed onto them'); |
120
|
|
|
} else { |
121
|
|
|
if (method_exists($this, $offset)) { |
122
|
|
|
return call_user_func([$this, $offset, $value]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$setMethod = 'set'.ucfirst($offset); |
126
|
|
|
if (method_exists($this, $setMethod)) { |
127
|
|
|
return call_user_func([$this, $setMethod, $value]); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$this->data()->set($offset, $value); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function offsetExists($offset) |
135
|
|
|
{ |
136
|
|
|
return !method_exists($this, $offset) && null !== $this->data()->get($offset); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function offsetUnset($offset) |
140
|
|
|
{ |
141
|
|
|
if (!method_exists($this, $offset)) { |
142
|
|
|
$this->data()->remove($offset); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function offsetGet($offset) |
147
|
|
|
{ |
148
|
|
|
if (method_exists($this, $offset)) { |
149
|
|
|
return call_user_func([$this, $offset]); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this->data()->get($offset); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.