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
|
|
|
namespace GpsLab\Component\Sitemap\Uri; |
10
|
|
|
|
11
|
|
|
class SmartUrl extends SimpleUri |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param string $loc |
15
|
|
|
*/ |
16
|
|
|
public function __construct($loc) |
17
|
|
|
{ |
18
|
|
|
parent::__construct($loc); |
19
|
|
|
|
20
|
|
|
// set priority from loc |
21
|
|
|
if ($this->getPriority() == self::DEFAULT_PRIORITY) { |
22
|
|
|
$num = count(array_filter(explode('/', trim($loc, '/')))); |
23
|
|
|
if (!$num) { |
24
|
|
|
$this->setPriority('1.0'); |
25
|
|
|
} elseif (($p = (10 - $num) / 10) > 0) { |
26
|
|
|
$this->setPriority('0.'.($p * 10)); |
27
|
|
|
} else { |
28
|
|
|
$this->setPriority('0.1'); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param \DateTime $last_mod |
35
|
|
|
* |
36
|
|
|
* @return SmartUri |
37
|
|
|
*/ |
38
|
|
|
public function setLastMod(\DateTime $last_mod) |
39
|
|
|
{ |
40
|
|
|
parent::setLastMod($last_mod); |
41
|
|
|
|
42
|
|
|
// set change freq from last mod |
43
|
|
|
if ($this->getChangeFreq() == self::DEFAULT_CHANGE_FREQ) { |
44
|
|
|
if ($last_mod < new \DateTime('-1 year')) { |
45
|
|
|
$this->setChangeFreq(self::CHANGE_FREQ_YEARLY); |
46
|
|
|
} elseif ($last_mod < new \DateTime('-1 month')) { |
47
|
|
|
$this->setChangeFreq(self::CHANGE_FREQ_MONTHLY); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $priority |
56
|
|
|
* |
57
|
|
|
* @return SmartUri |
58
|
|
|
*/ |
59
|
|
|
public function setPriority($priority) |
60
|
|
|
{ |
61
|
|
|
parent::setPriority($priority); |
62
|
|
|
|
63
|
|
|
// set change freq from priority |
64
|
|
|
if ($this->getChangeFreq() == self::DEFAULT_CHANGE_FREQ && $priority == '1.0') { |
65
|
|
|
$this->setChangeFreq(self::CHANGE_FREQ_DAILY); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|