1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the DpnXmlSitemapBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Björn Fromme <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the Resources/meta/LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Dpn\XmlSitemapBundle\Sitemap; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Kevin Bond <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Entry |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $url; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string|null |
26
|
|
|
*/ |
27
|
|
|
protected $lastMod = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string|null |
31
|
|
|
*/ |
32
|
|
|
protected $changeFreq = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var float|null |
36
|
|
|
*/ |
37
|
|
|
protected $priority = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param mixed $lastMod |
41
|
|
|
* |
42
|
|
|
* @return string|null |
43
|
|
|
*/ |
44
|
27 |
|
public static function normalizeLastMod($lastMod) |
45
|
|
|
{ |
46
|
27 |
|
if ($lastMod instanceof \DateTime) { |
47
|
1 |
|
return $lastMod->format('Y-m-d'); |
48
|
|
|
} |
49
|
|
|
|
50
|
26 |
|
if (1 === preg_match('/^\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}[\+|\-]\d{2}:\d{2}$/', $lastMod) || |
51
|
25 |
|
1 === preg_match('/^\d{4}\-\d{2}\-\d{2}$/', $lastMod) |
52
|
26 |
|
) { |
53
|
2 |
|
return $lastMod; |
54
|
|
|
} |
55
|
|
|
|
56
|
24 |
|
return null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param mixed $priority |
61
|
|
|
* |
62
|
|
|
* @return float|null |
63
|
|
|
*/ |
64
|
27 |
|
public static function normalizePriority($priority) |
65
|
|
|
{ |
66
|
27 |
|
if (true === is_numeric($priority)) { |
67
|
4 |
|
$priority = round(floatval($priority), 1); |
68
|
4 |
|
if (0 <= $priority && 1 >= $priority) { |
69
|
2 |
|
return $priority; |
70
|
|
|
} |
71
|
2 |
|
} |
72
|
|
|
|
73
|
25 |
|
return null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param mixed $changeFreq |
78
|
|
|
* |
79
|
|
|
* @return string|null |
80
|
|
|
*/ |
81
|
27 |
|
public static function normalizeChangeFreq($changeFreq) |
82
|
|
|
{ |
83
|
27 |
|
$changeFreq = strtolower($changeFreq); |
84
|
|
|
|
85
|
27 |
|
if (in_array($changeFreq, array('always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never'))) { |
86
|
2 |
|
return $changeFreq; |
87
|
|
|
} |
88
|
|
|
|
89
|
26 |
|
return null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $url |
94
|
|
|
* @param null|string $lastMod |
95
|
|
|
* @param null|string $changeFreq |
96
|
|
|
* @param null|float $priority |
97
|
|
|
*/ |
98
|
28 |
|
public function __construct($url, $lastMod = null, $changeFreq = null, $priority = null) |
99
|
|
|
{ |
100
|
28 |
|
$components = parse_url($url); |
101
|
|
|
|
102
|
28 |
|
if (!isset($components['scheme']) || !isset($components['host'])) { |
103
|
1 |
|
throw new \InvalidArgumentException(sprintf('The url "%s" is not absolute.', $url)); |
104
|
|
|
} |
105
|
|
|
|
106
|
27 |
|
$this->url = htmlspecialchars($url); |
107
|
|
|
|
108
|
27 |
|
$this->lastMod = self::normalizeLastMod($lastMod); |
109
|
27 |
|
$this->changeFreq = self::normalizeChangeFreq($changeFreq); |
110
|
27 |
|
$this->priority = self::normalizePriority($priority); |
111
|
27 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
18 |
|
public function getUrl() |
117
|
|
|
{ |
118
|
18 |
|
return $this->url; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return null|string |
123
|
|
|
*/ |
124
|
16 |
|
public function getLastMod() |
125
|
|
|
{ |
126
|
16 |
|
return $this->lastMod; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return null|string |
131
|
|
|
*/ |
132
|
17 |
|
public function getChangeFreq() |
133
|
|
|
{ |
134
|
17 |
|
return $this->changeFreq; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return null|float |
139
|
|
|
*/ |
140
|
16 |
|
public function getPriority() |
141
|
|
|
{ |
142
|
16 |
|
return $this->priority; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|