|
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
|
|
|
public function __construct($loc, \DateTimeImmutable $last_mod = null, $change_freq = null, $priority = null) |
|
21
|
|
|
{ |
|
22
|
|
|
// priority from loc |
|
23
|
|
|
if (!$priority) { |
|
|
|
|
|
|
24
|
|
|
$priority = $this->priorityFromLoc($loc); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
// change freq from last mod |
|
28
|
|
|
if (!$change_freq && $last_mod instanceof \DateTimeImmutable) { |
|
|
|
|
|
|
29
|
|
|
$change_freq = $this->changeFreqFromLastMod($last_mod); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
// change freq from priority |
|
33
|
|
|
if (!$change_freq) { |
|
|
|
|
|
|
34
|
|
|
$change_freq = $this->changeFreqFromPriority($priority); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
parent::__construct($loc, $last_mod, $change_freq, $priority); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $loc |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
private function priorityFromLoc($loc) |
|
46
|
|
|
{ |
|
47
|
|
|
// number of slashes |
|
48
|
|
|
$num = count(array_filter(explode('/', trim($loc, '/')))); |
|
49
|
|
|
|
|
50
|
|
|
if (!$num) { |
|
51
|
|
|
return '1.0'; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (($p = (10 - $num) / 10) > 0) { |
|
55
|
|
|
return '0.'.($p * 10); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return '0.1'; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param \DateTimeImmutable $last_mod |
|
63
|
|
|
* |
|
64
|
|
|
* @return string|null |
|
65
|
|
|
*/ |
|
66
|
|
|
private function changeFreqFromLastMod(\DateTimeImmutable $last_mod) |
|
67
|
|
|
{ |
|
68
|
|
|
if ($last_mod < new \DateTimeImmutable('-1 year')) { |
|
69
|
|
|
return self::CHANGE_FREQ_YEARLY; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ($last_mod < new \DateTimeImmutable('-1 month')) { |
|
73
|
|
|
return self::CHANGE_FREQ_MONTHLY; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return null; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $priority |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
private function changeFreqFromPriority($priority) |
|
85
|
|
|
{ |
|
86
|
|
|
switch ($priority) { |
|
87
|
|
|
case '1.0': |
|
88
|
|
|
return self::CHANGE_FREQ_HOURLY; |
|
89
|
|
|
case '0.9': |
|
90
|
|
|
return self::CHANGE_FREQ_DAILY; |
|
91
|
|
|
case '0.8': |
|
92
|
|
|
return self::CHANGE_FREQ_DAILY; |
|
93
|
|
|
case '0.7': |
|
94
|
|
|
return self::CHANGE_FREQ_WEEKLY; |
|
95
|
|
|
case '0.6': |
|
96
|
|
|
return self::CHANGE_FREQ_WEEKLY; |
|
97
|
|
|
case '0.5': |
|
98
|
|
|
return self::CHANGE_FREQ_WEEKLY; |
|
99
|
|
|
case '0.4': |
|
100
|
|
|
return self::CHANGE_FREQ_MONTHLY; |
|
101
|
|
|
case '0.3': |
|
102
|
|
|
return self::CHANGE_FREQ_MONTHLY; |
|
103
|
|
|
case '0.2': |
|
104
|
|
|
return self::CHANGE_FREQ_YEARLY; |
|
105
|
|
|
case '0.1': |
|
106
|
|
|
return self::CHANGE_FREQ_YEARLY; |
|
107
|
|
|
case '0.0': |
|
108
|
|
|
return self::CHANGE_FREQ_NEVER; |
|
109
|
|
|
default: |
|
110
|
|
|
return null; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: