@@ -51,6 +51,11 @@ class Builder
51
51
*/
52
52
private $ collectionLoader ;
53
53
54
+ /**
55
+ * @var SitemapPresenter
56
+ */
57
+ private $ sitemapPresenter ;
58
+
54
59
/**
55
60
* Create the Sitemap Builder.
56
61
*
@@ -80,6 +85,7 @@ public function __construct(array $data)
80
85
$ this ->setModelFactory ($ data ['model/factory ' ]);
81
86
$ this ->setCollectionLoader ($ data ['model/collection/loader ' ]);
82
87
$ this ->setTranslator ($ data ['translator ' ]);
88
+ $ this ->setSitemapPresenter ($ data ['sitemap/presenter ' ]);
83
89
84
90
return $ this ;
85
91
}
@@ -114,12 +120,13 @@ protected function defaultOptions()
114
120
'l10n ' => true ,
115
121
'check_active_routes ' => true ,
116
122
'relative_urls ' => true ,
123
+ 'transformer ' => null ,
117
124
'objects ' => [
118
125
'label ' => '{{title}} ' ,
119
126
'url ' => '{{url}} ' ,
120
127
'children ' => [],
121
128
'data ' => [],
122
- ],
129
+ ]
123
130
];
124
131
}
125
132
@@ -179,6 +186,10 @@ public function build($ident = 'default')
179
186
$ options ['relative_urls ' ] = $ defaults ['relative_urls ' ];
180
187
}
181
188
189
+ if (!isset ($ options ['transformer ' ])) {
190
+ $ options ['transformer ' ] = $ defaults ['transformer ' ];
191
+ }
192
+
182
193
$ out [] = $ this ->buildObject ($ class , $ options );
183
194
}
184
195
@@ -212,7 +223,7 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
212
223
if (isset ($ options ['filters ' ])) {
213
224
$ filters = $ options ['filters ' ];
214
225
if ($ parent ) {
215
- $ filters = $ this ->renderData ($ parent , $ filters );
226
+ $ filters = $ this ->renderData ($ parent , $ filters, $ options [ ' transformer ' ] );
216
227
}
217
228
$ loader ->addFilters ($ filters );
218
229
}
@@ -221,7 +232,7 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
221
232
if (isset ($ options ['orders ' ])) {
222
233
$ orders = $ options ['orders ' ];
223
234
if ($ parent ) {
224
- $ orders = $ this ->renderData ($ parent , $ orders );
235
+ $ orders = $ this ->renderData ($ parent , $ orders, $ options [ ' transformer ' ] );
225
236
}
226
237
$ loader ->addOrders ($ orders );
227
238
}
@@ -239,6 +250,7 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
239
250
$ defaultLocale = $ options ['locale ' ];
240
251
$ checkActiveRoutes = $ options ['check_active_routes ' ];
241
252
$ relativeUrls = $ options ['relative_urls ' ];
253
+ $ transformer = $ options ['transformer ' ];
242
254
243
255
// Locales
244
256
$ availableLocales = $ l10n
@@ -264,38 +276,40 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
264
276
continue ;
265
277
}
266
278
279
+ $ presentedParent = $ this ->sitemapPresenter ()->transform ($ object , $ transformer );
280
+
267
281
// Hierarchical (children, when defined)
268
282
$ cs = [];
269
283
if (!empty ($ children )) {
270
284
foreach ($ children as $ cname => $ opts ) {
271
285
$ opts = array_merge ($ this ->defaultOptions (), $ opts );
272
- $ cs [] = $ this ->buildObject ($ cname , $ opts , $ object , $ level );
286
+ $ cs [] = $ this ->buildObject ($ cname , $ opts , $ presentedParent , $ level );
273
287
}
274
288
}
275
289
276
290
$ url = $ relativeUrls
277
- ? trim ($ this ->renderData ($ object , $ options ['url ' ]))
278
- : $ this ->withBaseUrl (trim ($ this ->renderData ($ object , $ options ['url ' ])));
291
+ ? trim ($ this ->renderData ($ object , $ options ['url ' ], $ transformer ))
292
+ : $ this ->withBaseUrl (trim ($ this ->renderData ($ object , $ options ['url ' ], $ transformer )));
279
293
$ tmp = [
280
- 'label ' => trim ($ this ->renderData ($ object , $ options ['label ' ])),
294
+ 'label ' => trim ($ this ->renderData ($ object , $ options ['label ' ], $ transformer )),
281
295
'url ' => $ url ,
282
296
'children ' => $ cs ,
283
- 'data ' => $ this ->renderData ($ object , $ options ['data ' ]),
297
+ 'data ' => $ this ->renderData ($ object , $ options ['data ' ], $ transformer ),
284
298
'level ' => $ level ,
285
299
'lang ' => $ locale ,
286
300
];
287
301
288
302
// If you need a priority, fix your own rules
289
303
$ priority = '' ;
290
304
if (isset ($ options ['priority ' ]) && $ options ['priority ' ]) {
291
- $ priority = $ this ->renderData ($ object , (string )$ options ['priority ' ]);
305
+ $ priority = $ this ->renderData ($ object , (string )$ options ['priority ' ], $ transformer );
292
306
}
293
307
$ tmp ['priority ' ] = $ priority ;
294
308
295
309
// If you need a date of last modification, fix your own rules
296
310
$ last = '' ;
297
311
if (isset ($ options ['last_modified ' ]) && $ options ['last_modified ' ]) {
298
- $ last = $ this ->renderData ($ object , $ options ['last_modified ' ]);
312
+ $ last = $ this ->renderData ($ object , $ options ['last_modified ' ], $ transformer );
299
313
}
300
314
$ tmp ['last_modified ' ] = $ last ;
301
315
@@ -310,8 +324,8 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
310
324
}
311
325
312
326
$ url = $ relativeUrls
313
- ? trim ($ this ->renderData ($ object , $ options ['url ' ]))
314
- : $ this ->withBaseUrl (trim ($ this ->renderData ($ object , $ options ['url ' ])));
327
+ ? trim ($ this ->renderData ($ object , $ options ['url ' ], $ transformer ))
328
+ : $ this ->withBaseUrl (trim ($ this ->renderData ($ object , $ options ['url ' ], $ transformer )));
315
329
316
330
$ alternates [] = [
317
331
'url ' => $ url ,
@@ -336,10 +350,11 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
336
350
* @param mixed $data Pretty much anything to be rendered
337
351
* @return mixed Rendered data.
338
352
*/
339
- protected function renderData (ViewableInterface $ obj , $ data )
353
+ protected function renderData (ViewableInterface $ obj , $ data, $ transformer = null )
340
354
{
341
355
if (is_scalar ($ data )) {
342
- return $ obj ->view ()->render ($ data , $ obj );
356
+ $ presentedObject = $ this ->sitemapPresenter ()->transform ($ obj , $ transformer );
357
+ return $ obj ->view ()->render ($ data , $ presentedObject );
343
358
}
344
359
345
360
if (is_array ($ data )) {
@@ -429,6 +444,24 @@ protected function setCollectionLoader(CollectionLoader $loader)
429
444
$ this ->collectionLoader = $ loader ;
430
445
}
431
446
447
+ /**
448
+ * @return SitemapPresenter
449
+ */
450
+ public function sitemapPresenter ()
451
+ {
452
+ return $ this ->sitemapPresenter ;
453
+ }
454
+
455
+ /**
456
+ * @param SitemapPresenter $sitemapPresenter
457
+ * @return Builder
458
+ */
459
+ public function setSitemapPresenter (SitemapPresenter $ sitemapPresenter )
460
+ {
461
+ $ this ->sitemapPresenter = $ sitemapPresenter ;
462
+ return $ this ;
463
+ }
464
+
432
465
/**
433
466
* Get the website's base URL.
434
467
*
0 commit comments