1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* GpsLab component. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace GpsLab\Component\Sitemap\Url; |
11
|
|
|
|
12
|
|
|
class SmartUrl extends Url |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param string $loc |
16
|
|
|
* @param \DateTimeImmutable|null $last_mod |
17
|
|
|
* @param string|null $change_freq |
18
|
|
|
* @param string|null $priority |
19
|
|
|
*/ |
20
|
37 |
|
public function __construct($loc, \DateTimeImmutable $last_mod = null, $change_freq = null, $priority = null) |
21
|
|
|
{ |
22
|
|
|
// priority from loc |
23
|
37 |
|
if (!$priority) { |
24
|
18 |
|
$priority = $this->getPriorityFromLoc($loc); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
// change freq from last mod |
28
|
37 |
|
if (!$change_freq && $last_mod instanceof \DateTimeImmutable) { |
29
|
3 |
|
$change_freq = $this->getChangeFreqFromLastMod($last_mod); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// change freq from priority |
33
|
37 |
|
if (!$change_freq) { |
34
|
28 |
|
$change_freq = $this->getChangeFreqFromPriority($priority); |
35
|
|
|
} |
36
|
|
|
|
37
|
37 |
|
parent::__construct($loc, $last_mod, $change_freq, $priority); |
38
|
37 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $loc |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
18 |
|
private function getPriorityFromLoc($loc) |
46
|
|
|
{ |
47
|
|
|
// number of slashes |
48
|
18 |
|
$num = count(array_filter(explode('/', trim($loc, '/')))); |
49
|
|
|
|
50
|
18 |
|
if (!$num) { |
51
|
6 |
|
return '1.0'; |
52
|
|
|
} |
53
|
|
|
|
54
|
12 |
|
if (($p = (10 - $num) / 10) > 0) { |
55
|
10 |
|
return '0.'.($p * 10); |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
return '0.1'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param \DateTimeImmutable $last_mod |
63
|
|
|
* |
64
|
|
|
* @return string|null |
65
|
|
|
*/ |
66
|
3 |
|
private function getChangeFreqFromLastMod(\DateTimeImmutable $last_mod) |
67
|
|
|
{ |
68
|
3 |
|
if ($last_mod < new \DateTimeImmutable('-1 year')) { |
69
|
1 |
|
return self::CHANGE_FREQ_YEARLY; |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
if ($last_mod < new \DateTimeImmutable('-1 month')) { |
73
|
1 |
|
return self::CHANGE_FREQ_MONTHLY; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $priority |
81
|
|
|
* |
82
|
|
|
* @return string|null |
83
|
|
|
*/ |
84
|
28 |
|
private function getChangeFreqFromPriority($priority) |
85
|
|
|
{ |
86
|
|
|
$change_freq_priority = [ |
87
|
28 |
|
'1.0' => self::CHANGE_FREQ_HOURLY, |
88
|
28 |
|
'0.9' => self::CHANGE_FREQ_DAILY, |
89
|
28 |
|
'0.8' => self::CHANGE_FREQ_DAILY, |
90
|
28 |
|
'0.7' => self::CHANGE_FREQ_WEEKLY, |
91
|
28 |
|
'0.6' => self::CHANGE_FREQ_WEEKLY, |
92
|
28 |
|
'0.5' => self::CHANGE_FREQ_WEEKLY, |
93
|
28 |
|
'0.4' => self::CHANGE_FREQ_MONTHLY, |
94
|
28 |
|
'0.3' => self::CHANGE_FREQ_MONTHLY, |
95
|
28 |
|
'0.2' => self::CHANGE_FREQ_YEARLY, |
96
|
28 |
|
'0.1' => self::CHANGE_FREQ_YEARLY, |
97
|
28 |
|
'0.0' => self::CHANGE_FREQ_NEVER, |
98
|
|
|
]; |
99
|
|
|
|
100
|
28 |
|
if (isset($change_freq_priority[$priority])) { |
101
|
27 |
|
return $change_freq_priority[$priority]; |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
return null; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|