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

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