vendor/presta/sitemap-bundle/Sitemap/Url/UrlConcrete.php line 94

Open in your IDE?
  1. <?php
  2. /**
  3. * This file is part of the PrestaSitemapBundle package.
  4. *
  5. * (c) PrestaConcept <www.prestaconcept.net>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Presta\SitemapBundle\Sitemap\Url;
  11. use DateTimeInterface;
  12. use Presta\SitemapBundle\Sitemap\Utils;
  13. /**
  14. * Class used for managing url entities
  15. *
  16. * @author Christophe Dolivet
  17. * @author David Epely
  18. */
  19. class UrlConcrete implements Url
  20. {
  21. const CHANGEFREQ_ALWAYS = 'always';
  22. const CHANGEFREQ_HOURLY = 'hourly';
  23. const CHANGEFREQ_DAILY = 'daily';
  24. const CHANGEFREQ_WEEKLY = 'weekly';
  25. const CHANGEFREQ_MONTHLY = 'monthly';
  26. const CHANGEFREQ_YEARLY = 'yearly';
  27. const CHANGEFREQ_NEVER = 'never';
  28. /**
  29. * @var string
  30. */
  31. protected $loc;
  32. /**
  33. * @var DateTimeInterface|null
  34. */
  35. protected $lastmod;
  36. /**
  37. * @var string|null
  38. */
  39. protected $changefreq;
  40. /**
  41. * @var float|null
  42. */
  43. protected $priority;
  44. /**
  45. * Construct a new basic url
  46. *
  47. * @param string $loc Absolute url
  48. * @param DateTimeInterface|null $lastmod Last modification date
  49. * @param string|null $changefreq Change frequency
  50. * @param float|null $priority Priority
  51. */
  52. public function __construct($loc, DateTimeInterface $lastmod = null, $changefreq = null, $priority = null)
  53. {
  54. $this->setLoc($loc);
  55. $this->setLastmod($lastmod);
  56. $this->setChangefreq($changefreq);
  57. $this->setPriority($priority);
  58. }
  59. /**
  60. * @param string $loc
  61. *
  62. * @return UrlConcrete
  63. */
  64. public function setLoc($loc)
  65. {
  66. $this->loc = $loc;
  67. return $this;
  68. }
  69. /**
  70. * @return string
  71. */
  72. public function getLoc()
  73. {
  74. return $this->loc;
  75. }
  76. /**
  77. * @param DateTimeInterface|null $lastmod
  78. *
  79. * @return UrlConcrete
  80. */
  81. public function setLastmod(DateTimeInterface $lastmod = null)
  82. {
  83. $this->lastmod = $lastmod;
  84. return $this;
  85. }
  86. /**
  87. * @return DateTimeInterface|null
  88. */
  89. public function getLastmod()
  90. {
  91. return $this->lastmod;
  92. }
  93. /**
  94. * Define the change frequency of this entry
  95. *
  96. * @param string|null $changefreq Define the change frequency
  97. *
  98. * @return UrlConcrete
  99. */
  100. public function setChangefreq($changefreq = null)
  101. {
  102. $frequencies = [
  103. self::CHANGEFREQ_ALWAYS,
  104. self::CHANGEFREQ_HOURLY,
  105. self::CHANGEFREQ_DAILY,
  106. self::CHANGEFREQ_WEEKLY,
  107. self::CHANGEFREQ_MONTHLY,
  108. self::CHANGEFREQ_YEARLY,
  109. self::CHANGEFREQ_NEVER,
  110. null,
  111. ];
  112. if (!in_array($changefreq, $frequencies)) {
  113. throw new \RuntimeException(
  114. sprintf(
  115. 'The value "%s" is not supported by the option changefreq. See http://www.sitemaps.org/protocol.html#xmlTagDefinitions',
  116. $changefreq
  117. )
  118. );
  119. }
  120. $this->changefreq = $changefreq;
  121. return $this;
  122. }
  123. /**
  124. * return the change frequency
  125. *
  126. * @return string|null
  127. */
  128. public function getChangefreq()
  129. {
  130. return $this->changefreq;
  131. }
  132. /**
  133. * Define the priority of this entry
  134. *
  135. * @param float|string|int|null $priority Define the priority
  136. *
  137. * @return UrlConcrete
  138. */
  139. public function setPriority($priority = null)
  140. {
  141. if ($priority === null) {
  142. return $this;
  143. }
  144. if (is_string($priority) || is_int($priority)) {
  145. $priority = (float)$priority;
  146. }
  147. if (is_float($priority) && $priority >= 0 && $priority <= 1) {
  148. $this->priority = round($priority, 1);
  149. } else {
  150. throw new \RuntimeException(
  151. sprintf(
  152. 'The value "%s" is not supported by the option priority, it must be a numeric between 0.0 and 1.0. See http://www.sitemaps.org/protocol.html#xmlTagDefinitions',
  153. $priority
  154. )
  155. );
  156. }
  157. return $this;
  158. }
  159. /**
  160. * @return float|null
  161. */
  162. public function getPriority()
  163. {
  164. return $this->priority;
  165. }
  166. /**
  167. * @inheritdoc
  168. */
  169. public function toXml()
  170. {
  171. $xml = '<url><loc>' . Utils::encode($this->getLoc()) . '</loc>';
  172. if ($this->getLastmod()) {
  173. $xml .= '<lastmod>' . $this->getLastmod()->format('c') . '</lastmod>';
  174. }
  175. if ($this->getChangefreq()) {
  176. $xml .= '<changefreq>' . $this->getChangefreq() . '</changefreq>';
  177. }
  178. if ($this->getPriority()) {
  179. $xml .= '<priority>' . number_format($this->getPriority(), 1) . '</priority>';
  180. }
  181. $xml .= '</url>';
  182. return $xml;
  183. }
  184. /**
  185. * @inheritdoc
  186. */
  187. public function getCustomNamespaces()
  188. {
  189. return []; // basic url has no namespace. see decorated urls
  190. }
  191. }