|
1
|
|
|
<?php namespace Arcanedev\LaravelSitemap\Entities; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelSitemap\Contracts\Entities\Sitemap as SitemapContract; |
|
4
|
|
|
use Illuminate\Support\Collection; |
|
5
|
|
|
use Arcanedev\LaravelSitemap\Contracts\Entities\Url as UrlContract; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Sitemap |
|
9
|
|
|
* |
|
10
|
|
|
* @package Arcanedev\LaravelSitemap\Entities |
|
11
|
|
|
* @author ARCANEDEV <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Sitemap implements SitemapContract |
|
14
|
|
|
{ |
|
15
|
|
|
/* ----------------------------------------------------------------- |
|
16
|
|
|
| Properties |
|
17
|
|
|
| ----------------------------------------------------------------- |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
/** @var string|null */ |
|
21
|
|
|
protected $path; |
|
22
|
|
|
|
|
23
|
|
|
/** @var \Illuminate\Support\Collection */ |
|
24
|
|
|
protected $urls; |
|
25
|
|
|
|
|
26
|
|
|
/* ----------------------------------------------------------------- |
|
27
|
|
|
| Constructor |
|
28
|
|
|
| ----------------------------------------------------------------- |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Sitemap constructor. |
|
33
|
|
|
*/ |
|
34
|
42 |
|
public function __construct() |
|
35
|
|
|
{ |
|
36
|
42 |
|
$this->urls = new Collection; |
|
37
|
42 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/* ----------------------------------------------------------------- |
|
40
|
|
|
| Main Methods |
|
41
|
|
|
| ----------------------------------------------------------------- |
|
42
|
|
|
*/ |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Set the sitemap path. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $path |
|
48
|
|
|
* |
|
49
|
|
|
* @return self |
|
50
|
|
|
*/ |
|
51
|
24 |
|
public function setPath($path) |
|
52
|
|
|
{ |
|
53
|
24 |
|
$this->path = $path; |
|
54
|
|
|
|
|
55
|
24 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get the sitemap path. |
|
60
|
|
|
* |
|
61
|
|
|
* @return string|null |
|
62
|
|
|
*/ |
|
63
|
8 |
|
public function getPath() |
|
64
|
|
|
{ |
|
65
|
8 |
|
return $this->path; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get the sitemap's URLs. |
|
70
|
|
|
* |
|
71
|
|
|
* @return \Illuminate\Support\Collection |
|
72
|
|
|
*/ |
|
73
|
26 |
|
public function getUrls() |
|
74
|
|
|
{ |
|
75
|
26 |
|
return $this->urls; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get a URL instance by its loc. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $loc |
|
82
|
|
|
* @param mixed|null $default |
|
83
|
|
|
* |
|
84
|
|
|
* @return \Arcanedev\LaravelSitemap\Entities\Url|null |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function getUrl($loc, $default = null) |
|
87
|
|
|
{ |
|
88
|
2 |
|
return $this->getUrls()->get($loc, $default); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Add a sitemap URL to the collection. |
|
93
|
|
|
* |
|
94
|
|
|
* @param \Arcanedev\LaravelSitemap\Contracts\Entities\Url $url |
|
95
|
|
|
* |
|
96
|
|
|
* @return $this |
|
97
|
|
|
*/ |
|
98
|
36 |
|
public function add(UrlContract $url) |
|
99
|
|
|
{ |
|
100
|
36 |
|
$this->urls->put($url->getLoc(), $url); |
|
101
|
|
|
|
|
102
|
36 |
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Create and Add a sitemap URL to the collection. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $loc |
|
109
|
|
|
* @param callable $callback |
|
110
|
|
|
* |
|
111
|
|
|
* @return self |
|
112
|
|
|
*/ |
|
113
|
22 |
|
public function create($loc, callable $callback) |
|
114
|
|
|
{ |
|
115
|
22 |
|
return $this->add(tap(Url::make($loc), $callback)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Check if the url exists in the sitemap items. |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $url |
|
122
|
|
|
* |
|
123
|
|
|
* @return bool |
|
124
|
|
|
*/ |
|
125
|
4 |
|
public function has($url) |
|
126
|
|
|
{ |
|
127
|
4 |
|
return $this->urls->has($url); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get the urls' count. |
|
132
|
|
|
* |
|
133
|
|
|
* @return int |
|
134
|
|
|
*/ |
|
135
|
8 |
|
public function count() |
|
136
|
|
|
{ |
|
137
|
8 |
|
return $this->urls->count(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Get the collection of items as a plain array. |
|
142
|
|
|
* |
|
143
|
|
|
* @return array |
|
144
|
|
|
*/ |
|
145
|
8 |
|
public function toArray() |
|
146
|
|
|
{ |
|
147
|
8 |
|
return $this->getUrls()->values()->toArray(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Get the sitemap and its urls as JSON. |
|
152
|
|
|
* |
|
153
|
|
|
* @param int $options |
|
154
|
|
|
* |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
2 |
|
public function toJson($options = 0) |
|
158
|
|
|
{ |
|
159
|
2 |
|
return json_encode($this->jsonSerialize(), $options); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Convert the object into something JSON serializable. |
|
164
|
|
|
* |
|
165
|
|
|
* @return array |
|
166
|
|
|
*/ |
|
167
|
2 |
|
public function jsonSerialize() |
|
168
|
|
|
{ |
|
169
|
2 |
|
return $this->toArray(); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|