|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Sitemap\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
7
|
|
|
use Sludio\HelperBundle\Sitemap\Entity\Url; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Abstract class containing common methods used by Propel and Doctrine providers. |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class AbstractProvider implements ProviderInterface |
|
13
|
|
|
{ |
|
14
|
|
|
protected $router; |
|
15
|
|
|
|
|
16
|
|
|
protected $options = [ |
|
17
|
|
|
'loc' => [], |
|
18
|
|
|
'lastmod' => null, |
|
19
|
|
|
'priority' => null, |
|
20
|
|
|
'changefreq' => null, |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* AbstractProvider constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param RouterInterface $router |
|
27
|
|
|
* @param array $options |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(RouterInterface $router, array $options) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->router = $router; |
|
32
|
|
|
$this->options = array_merge($this->options, $options); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function resultToUrl($result) |
|
36
|
|
|
{ |
|
37
|
|
|
$url = new Url(); |
|
38
|
|
|
$url->setLoc($this->getResultLoc($result)); |
|
39
|
|
|
|
|
40
|
|
|
if ($this->options['priority'] !== null) { |
|
41
|
|
|
$url->setPriority($this->options['priority']); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ($this->options['changefreq'] !== null) { |
|
45
|
|
|
$url->setChangefreq($this->options['changefreq']); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ($this->options['lastmod'] !== null) { |
|
49
|
|
|
$url->setLastmod($this->getColumnValue($result, $this->options['lastmod'])); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $url; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function getResultLoc($result) |
|
56
|
|
|
{ |
|
57
|
|
|
$route = $this->options['loc']['route']; |
|
58
|
|
|
$params = []; |
|
59
|
|
|
|
|
60
|
|
|
if (!isset($this->options['loc']['params'])) { |
|
61
|
|
|
$this->options['loc']['params'] = []; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
foreach ($this->options['loc']['params'] as $key => $column) { |
|
65
|
|
|
$params[$key] = $this->getColumnValue($result, $column); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this->router->generate($route, $params); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function getColumnValue($result, $column) |
|
72
|
|
|
{ |
|
73
|
|
|
$method = 'get'.ucfirst($column); |
|
74
|
|
|
|
|
75
|
|
|
if (!method_exists($result, $method)) { |
|
76
|
|
|
throw new \RuntimeException(sprintf('"%s" method not found in "%s"', $method, get_class($result))); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $result->$method(); |
|
80
|
|
|
} |
|
81
|
|
|
} |