1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the SexyField package. |
5
|
|
|
* |
6
|
|
|
* (c) Dion Snoeijen <[email protected]> |
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
|
|
|
declare (strict_types = 1); |
13
|
|
|
|
14
|
|
|
namespace Tardigrades\Twig; |
15
|
|
|
|
16
|
|
|
use Tardigrades\SectionField\Service\EntryNotFoundException; |
17
|
|
|
use Tardigrades\SectionField\Service\ReadOptions; |
18
|
|
|
use Tardigrades\SectionField\Service\ReadSectionInterface; |
19
|
|
|
use Twig_Extension; |
20
|
|
|
use Twig_Function; |
21
|
|
|
|
22
|
|
|
class SectionTwigExtension extends Twig_Extension |
23
|
|
|
{ |
24
|
|
|
/** @var ReadSectionInterface */ |
25
|
|
|
private $readSection; |
26
|
|
|
|
27
|
|
|
/** @var array */ |
28
|
|
|
private $options; |
29
|
|
|
|
30
|
|
|
/** @var bool */ |
31
|
|
|
private $throwNotFound = false; |
32
|
|
|
|
33
|
|
|
public function __construct(ReadSectionInterface $readSection) |
34
|
|
|
{ |
35
|
|
|
$this->readSection = $readSection; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getFunctions(): array |
39
|
|
|
{ |
40
|
|
|
return array( |
41
|
|
|
new Twig_Function('section', array($this, 'section')) |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns entries that belong to a specific section. |
47
|
|
|
* In the future it will be possible to fetch entries of |
48
|
|
|
* multiple sections at once. |
49
|
|
|
* |
50
|
|
|
* A section can be passed by it's FQCN: Fully.Qualified.Example |
51
|
|
|
* Or by it's handle: example |
52
|
|
|
* |
53
|
|
|
* @param string $section |
54
|
|
|
* @return SectionTwigExtension |
55
|
|
|
*/ |
56
|
|
|
public function section(string $section): SectionTwigExtension |
57
|
|
|
{ |
58
|
|
|
$this->options[ReadOptions::SECTION] = $section; |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Limit returned entries |
65
|
|
|
* |
66
|
|
|
* @param int $limit |
67
|
|
|
* @return SectionTwigExtension |
68
|
|
|
*/ |
69
|
|
|
public function limit(int $limit): SectionTwigExtension |
70
|
|
|
{ |
71
|
|
|
$this->options[ReadOptions::LIMIT] = $limit; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Offset returned entries |
78
|
|
|
* |
79
|
|
|
* @param int $offset |
80
|
|
|
* @return SectionTwigExtension |
81
|
|
|
*/ |
82
|
|
|
public function offset(int $offset): SectionTwigExtension |
83
|
|
|
{ |
84
|
|
|
$this->options[ReadOptions::OFFSET] = $offset; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Specify which field to order by |
91
|
|
|
* |
92
|
|
|
* @param array $orderBy |
93
|
|
|
* @return SectionTwigExtension |
94
|
|
|
*/ |
95
|
|
|
public function orderBy(array $orderBy): SectionTwigExtension |
96
|
|
|
{ |
97
|
|
|
$this->options[ReadOptions::ORDER_BY] = $orderBy; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Specify sort direction |
104
|
|
|
* |
105
|
|
|
* ASC|DESC |
106
|
|
|
* |
107
|
|
|
* @param string $sort |
108
|
|
|
* @return SectionTwigExtension |
109
|
|
|
*/ |
110
|
|
|
public function sort(string $sort): SectionTwigExtension |
111
|
|
|
{ |
112
|
|
|
$this->options[ReadOptions::SORT] = $sort; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Only fetch entries with a Post Date that is before the given date. |
119
|
|
|
* |
120
|
|
|
* @param string $before |
121
|
|
|
* @return SectionTwigExtension |
122
|
|
|
*/ |
123
|
|
|
public function before(string $before): SectionTwigExtension |
124
|
|
|
{ |
125
|
|
|
$this->options[ReadOptions::BEFORE] = new \DateTime($before); |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Only fetch entries with a Post Date that is on or after the given date. |
132
|
|
|
* |
133
|
|
|
* @param string $after |
134
|
|
|
* @return SectionTwigExtension |
135
|
|
|
*/ |
136
|
|
|
public function after(string $after): SectionTwigExtension |
137
|
|
|
{ |
138
|
|
|
$this->options[ReadOptions::AFTER] = new \DateTime($after); |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Provides ability to get entries that are for another locale. (v1.1) |
145
|
|
|
* |
146
|
|
|
* @param bool $enabled |
147
|
|
|
* @return SectionTwigExtension |
148
|
|
|
*/ |
149
|
|
|
public function localeEnabled(bool $enabled = true): SectionTwigExtension |
150
|
|
|
{ |
151
|
|
|
$this->options[ReadOptions::LOCALE_ENABLED] = $enabled; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Fetch just the entries for a specific locale. |
158
|
|
|
* |
159
|
|
|
* @param string $locale |
160
|
|
|
* @return SectionTwigExtension |
161
|
|
|
*/ |
162
|
|
|
public function locale(string $locale): SectionTwigExtension |
163
|
|
|
{ |
164
|
|
|
$this->options[ReadOptions::LOCALE] = $locale; |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Only fetch the entry with the given ID. |
171
|
|
|
* |
172
|
|
|
* @param int $id |
173
|
|
|
* @return SectionTwigExtension |
174
|
|
|
*/ |
175
|
|
|
public function id(int $id): SectionTwigExtension |
176
|
|
|
{ |
177
|
|
|
$this->throwNotFound = true; |
178
|
|
|
$this->options[ReadOptions::ID] = $id; |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Only fetch the entry with the given slug. |
185
|
|
|
* |
186
|
|
|
* @param string $slug |
187
|
|
|
* @return SectionTwigExtension |
188
|
|
|
*/ |
189
|
|
|
public function slug(string $slug): SectionTwigExtension |
190
|
|
|
{ |
191
|
|
|
$this->throwNotFound = true; |
192
|
|
|
$this->options[ReadOptions::SLUG] = $slug; |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* This method requires some extra thinking. |
199
|
|
|
* How are we going to handle searching in the context of |
200
|
|
|
* SexyField intelligently? What version of SexyField will |
201
|
|
|
* this be available? |
202
|
|
|
* |
203
|
|
|
* @param string $searchQuery |
204
|
|
|
* @return SectionTwigExtension |
205
|
|
|
*/ |
206
|
|
|
public function search(string $searchQuery): SectionTwigExtension |
207
|
|
|
{ |
208
|
|
|
$this->options[ReadOptions::SEARCH] = $searchQuery; |
209
|
|
|
|
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Return entries based on a field value. |
215
|
|
|
* Example, based on a title with a value: "Awesome Title" |
216
|
|
|
* |
217
|
|
|
* @param string $fieldHandle |
218
|
|
|
* @param string $value |
219
|
|
|
* @return SectionTwigExtension |
220
|
|
|
*/ |
221
|
|
|
public function field(string $fieldHandle, string $value): SectionTwigExtension |
222
|
|
|
{ |
223
|
|
|
$this->options[ReadOptions::FIELD] = [$fieldHandle=>$value]; |
224
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param array $options |
230
|
|
|
* @return \ArrayIterator |
231
|
|
|
*/ |
232
|
|
|
public function fetch(array $options = []): \ArrayIterator |
233
|
|
|
{ |
234
|
|
|
$options = array_merge($this->options, $options); |
235
|
|
|
|
236
|
|
|
if (!$this->throwNotFound) { |
237
|
|
|
try { |
238
|
|
|
$entries = $this->readSection->read(ReadOptions::fromArray($options)); |
239
|
|
|
} catch (EntryNotFoundException $exception) { |
240
|
|
|
$entries = new \ArrayIterator(); |
241
|
|
|
} |
242
|
|
|
} else { |
243
|
|
|
$entries = $this->readSection->read(ReadOptions::fromArray($options)); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return $entries; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|