1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Sitemap\Tags; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use DateTime; |
7
|
|
|
|
8
|
|
|
class Url extends Tag |
9
|
|
|
{ |
10
|
|
|
const CHANGE_FREQUENCY_ALWAYS = 'always'; |
11
|
|
|
const CHANGE_FREQUENCY_HOURLY = 'hourly'; |
12
|
|
|
const CHANGE_FREQUENCY_DAILY = 'daily'; |
13
|
|
|
const CHANGE_FREQUENCY_WEEKLY = 'weekly'; |
14
|
|
|
const CHANGE_FREQUENCY_MONTHLY = 'monthly'; |
15
|
|
|
const CHANGE_FREQUENCY_YEARLY = 'yearly'; |
16
|
|
|
const CHANGE_FREQUENCY_NEVER = 'never'; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
public $url = ''; |
20
|
|
|
|
21
|
|
|
/** @var \Carbon\Carbon */ |
22
|
|
|
public $lastModificationDate; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
public $changeFrequency; |
26
|
|
|
|
27
|
|
|
/** @var float */ |
28
|
|
|
public $priority = 0.8; |
29
|
|
|
|
30
|
|
|
/** @var array */ |
31
|
|
|
public $alternates = []; |
32
|
|
|
|
33
|
|
|
public static function create(string $url): self |
34
|
|
|
{ |
35
|
|
|
return new static($url); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function __construct(string $url) |
39
|
|
|
{ |
40
|
|
|
$this->url = $url; |
41
|
|
|
|
42
|
|
|
$this->lastModificationDate = Carbon::now(); |
43
|
|
|
|
44
|
|
|
$this->changeFrequency = static::CHANGE_FREQUENCY_DAILY; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $url |
49
|
|
|
* |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function setUrl(string $url = '') |
53
|
|
|
{ |
54
|
|
|
$this->url = $url; |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param \DateTime $lastModificationDate |
61
|
|
|
* |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function setLastModificationDate(DateTime $lastModificationDate) |
65
|
|
|
{ |
66
|
|
|
$this->lastModificationDate = $lastModificationDate; |
|
|
|
|
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $changeFrequency |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function setChangeFrequency(string $changeFrequency) |
77
|
|
|
{ |
78
|
|
|
$this->changeFrequency = $changeFrequency; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param float $priority |
85
|
|
|
* |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function setPriority(float $priority) |
89
|
|
|
{ |
90
|
|
|
$this->priority = max(0, min(1, $priority)); |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param Alternate $alternate |
97
|
|
|
* |
98
|
|
|
* @param string $url |
99
|
|
|
* @param string $locale |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function addAlternate(string $url, string $locale = '') |
103
|
|
|
{ |
104
|
|
|
$this->alternates[] = new Alternate($url, $locale); |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function path(): string |
113
|
|
|
{ |
114
|
|
|
return parse_url($this->url)['path'] ?? ''; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param int|null $index |
119
|
|
|
* |
120
|
|
|
* @return array|null|string |
121
|
|
|
*/ |
122
|
|
|
public function segments(int $index = null) |
123
|
|
|
{ |
124
|
|
|
$segments = collect(explode('/', $this->path())) |
125
|
|
|
->filter(function ($value) { |
126
|
|
|
return $value !== ''; |
127
|
|
|
}) |
128
|
|
|
->values() |
129
|
|
|
->toArray(); |
130
|
|
|
|
131
|
|
|
if (! is_null($index)) { |
132
|
|
|
return $this->segment($index); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $segments; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param int $index |
140
|
|
|
* |
141
|
|
|
* @return string|null |
142
|
|
|
*/ |
143
|
|
|
public function segment(int $index) |
144
|
|
|
{ |
145
|
|
|
return $this->segments()[$index - 1] ?? null; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.