1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelSitemap; |
6
|
|
|
|
7
|
|
|
use Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap; |
8
|
|
|
use DOMDocument; |
9
|
|
|
use Illuminate\Support\{Collection, Str}; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class SitemapBuilder |
13
|
|
|
* |
14
|
|
|
* @package Arcanedev\LaravelSitemap |
15
|
|
|
* @author ARCANEDEV <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class SitemapBuilder |
18
|
|
|
{ |
19
|
|
|
/* ----------------------------------------------------------------- |
20
|
|
|
| Main Methods |
21
|
|
|
| ----------------------------------------------------------------- |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create the builder instance. |
26
|
|
|
* |
27
|
|
|
* @return \Arcanedev\LaravelSitemap\SitemapBuilder |
28
|
|
|
*/ |
29
|
66 |
|
public static function make() |
30
|
|
|
{ |
31
|
66 |
|
return new static; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Build the sitemap. |
36
|
|
|
* |
37
|
|
|
* @param string|null $name |
38
|
|
|
* @param \Illuminate\Support\Collection $sitemaps |
39
|
|
|
* @param string $format |
40
|
|
|
* |
41
|
|
|
* @throws \Throwable |
42
|
|
|
* |
43
|
|
|
* @return string|null |
44
|
|
|
*/ |
45
|
66 |
|
public function build(?string $name, Collection $sitemaps, string $format): ?string |
46
|
|
|
{ |
47
|
66 |
|
if ($sitemaps->isEmpty()) |
48
|
24 |
|
return null; |
49
|
|
|
|
50
|
60 |
|
if (is_null($name)) { |
51
|
48 |
|
return $sitemaps->count() > 1 |
52
|
36 |
|
? static::renderSitemapIndex($format, $sitemaps) |
53
|
48 |
|
: static::renderSitemap($format, $sitemaps->first()); |
54
|
|
|
} |
55
|
|
|
|
56
|
54 |
|
list($name, $key) = Str::contains($name, '.') |
57
|
6 |
|
? explode('.', $name, 2) |
58
|
54 |
|
: [$name, null]; |
59
|
|
|
|
60
|
54 |
|
if ($sitemaps->has($name)) |
61
|
54 |
|
return static::renderSitemap($format, $sitemaps->get($name), $key); |
62
|
|
|
|
63
|
6 |
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/* ----------------------------------------------------------------- |
67
|
|
|
| Other Methods |
68
|
|
|
| ----------------------------------------------------------------- |
69
|
|
|
*/ |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Build a single sitemap item. |
73
|
|
|
* |
74
|
|
|
* @param string $format |
75
|
|
|
* @param \Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap|null $sitemap |
76
|
|
|
* @param string|null $key |
77
|
|
|
* |
78
|
|
|
* @throws \Throwable |
79
|
|
|
* |
80
|
|
|
* @return string|null |
81
|
|
|
*/ |
82
|
54 |
|
protected function renderSitemap(string $format, Sitemap $sitemap = null, string $key = null) |
83
|
|
|
{ |
84
|
54 |
|
if (is_null($sitemap)) |
85
|
6 |
|
return null; |
86
|
|
|
|
87
|
54 |
|
if ( ! $sitemap->isExceeded()) |
88
|
54 |
|
return static::render($format, 'sitemap', ['sitemap' => $sitemap]); |
89
|
|
|
|
90
|
12 |
|
$chunks = $sitemap->chunk(); |
91
|
|
|
|
92
|
12 |
|
if (is_null($key)) |
93
|
12 |
|
return static::renderSitemapIndex($format, $chunks); |
94
|
|
|
|
95
|
6 |
|
return static::renderSitemap($format, $chunks->get($key)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Render sitemap index. |
100
|
|
|
* |
101
|
|
|
* @param string $format |
102
|
|
|
* @param \Illuminate\Support\Collection $sitemaps |
103
|
|
|
* |
104
|
|
|
* @throws \Throwable |
105
|
|
|
* |
106
|
|
|
* @return string|null |
107
|
|
|
*/ |
108
|
48 |
|
protected function renderSitemapIndex(string $format, Collection $sitemaps): ?string |
109
|
|
|
{ |
110
|
48 |
|
return static::render($format, 'sitemap-index', compact('sitemaps')); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Render the file. |
115
|
|
|
* |
116
|
|
|
* @param string $format |
117
|
|
|
* @param string $type |
118
|
|
|
* @param array $data |
119
|
|
|
* |
120
|
|
|
* @throws \Throwable |
121
|
|
|
* |
122
|
|
|
* @return string|null |
123
|
|
|
*/ |
124
|
60 |
|
protected function render(string $format, string $type, array $data): ?string |
125
|
|
|
{ |
126
|
60 |
|
$format = strtolower(trim($format)); |
127
|
|
|
|
128
|
60 |
|
switch ($format) { |
129
|
60 |
|
case 'xml': |
130
|
30 |
|
case 'rss': |
131
|
42 |
|
return static::renderXml($format, $type, $data); |
132
|
|
|
|
133
|
24 |
|
case 'txt': |
134
|
18 |
|
return static::renderView($type, $format, $data); |
135
|
|
|
|
136
|
|
|
default: |
137
|
6 |
|
return null; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Render the xml file. |
143
|
|
|
* |
144
|
|
|
* @param string $format |
145
|
|
|
* @param string $type |
146
|
|
|
* @param array $data |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
42 |
|
protected function renderXml(string $format, string $type, array $data): string |
151
|
|
|
{ |
152
|
|
|
return tap(new DOMDocument('1.0'), function (DOMDocument $document) use ($format, $type, $data) { |
153
|
42 |
|
$document->preserveWhiteSpace = false; |
154
|
42 |
|
$document->formatOutput = true; |
155
|
42 |
|
$document->loadXML( |
156
|
42 |
|
static::renderView($type, $format, $data) |
157
|
|
|
); |
158
|
42 |
|
})->saveXML(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Render with illuminate. |
163
|
|
|
* |
164
|
|
|
* @param string $type |
165
|
|
|
* @param string $format |
166
|
|
|
* @param array $data |
167
|
|
|
* |
168
|
|
|
* @throws \Throwable |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
54 |
|
protected function renderView(string $type, string $format, array $data): string |
173
|
|
|
{ |
174
|
54 |
|
return view("sitemap::{$type}.{$format}", $data)->render(); |
|
|
|
|
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: