vendor/sonata-project/seo-bundle/src/Seo/SeoPage.php line 107

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\SeoBundle\Seo;
  12. /**
  13.  * http://en.wikipedia.org/wiki/Meta_element.
  14.  */
  15. class SeoPage implements SeoPageInterface
  16. {
  17.     /**
  18.      * @var string
  19.      */
  20.     protected $title;
  21.     /**
  22.      * @var array<string, array<string, mixed>>
  23.      */
  24.     protected $metas;
  25.     /**
  26.      * @var array<string, string>
  27.      */
  28.     protected $htmlAttributes;
  29.     /**
  30.      * @var string
  31.      */
  32.     protected $linkCanonical;
  33.     /**
  34.      * @var string
  35.      */
  36.     protected $separator;
  37.     /**
  38.      * @var array<string, string>
  39.      */
  40.     protected $headAttributes;
  41.     /**
  42.      * @var array<string, string>
  43.      */
  44.     protected $langAlternates;
  45.     /**
  46.      * @var array<string, string>
  47.      */
  48.     protected $oembedLinks;
  49.     /**
  50.      * @param string $title
  51.      */
  52.     public function __construct($title '')
  53.     {
  54.         $this->title $title;
  55.         $this->metas = [
  56.             'http-equiv' => [],
  57.             'name' => [],
  58.             'schema' => [],
  59.             'charset' => [],
  60.             'property' => [],
  61.         ];
  62.         $this->htmlAttributes = [];
  63.         $this->headAttributes = [];
  64.         $this->linkCanonical '';
  65.         $this->separator ' ';
  66.         $this->langAlternates = [];
  67.         $this->oembedLinks = [];
  68.     }
  69.     public function setTitle($title)
  70.     {
  71.         $this->title $title;
  72.         return $this;
  73.     }
  74.     public function addTitle($title)
  75.     {
  76.         $this->title $title.$this->separator.$this->title;
  77.         return $this;
  78.     }
  79.     public function getTitle()
  80.     {
  81.         return $this->title;
  82.     }
  83.     public function getMetas()
  84.     {
  85.         return $this->metas;
  86.     }
  87.     public function addMeta($type$name/* string */ $content, array $extras = [])
  88.     {
  89.         if (!\is_string($content)) {
  90.             @trigger_error(sprintf(
  91.                 'Passing meta content of type %s in %s is deprecated since version 2.8 and will be unsupported in version 3. Please cast the value to a string first.',
  92.                 \gettype($content),
  93.                 __METHOD__
  94.             ), \E_USER_DEPRECATED);
  95.         }
  96.         if (!isset($this->metas[$type])) {
  97.             $this->metas[$type] = [];
  98.         }
  99.         $this->metas[$type][$name] = [$content$extras];
  100.         return $this;
  101.     }
  102.     public function hasMeta($type$name)
  103.     {
  104.         return isset($this->metas[$type][$name]);
  105.     }
  106.     public function removeMeta($type$name)
  107.     {
  108.         unset($this->metas[$type][$name]);
  109.         return $this;
  110.     }
  111.     public function setMetas(array $metadatas)
  112.     {
  113.         $this->metas = [];
  114.         foreach ($metadatas as $type => $metas) {
  115.             if (!\is_array($metas)) {
  116.                 throw new \RuntimeException('$metas must be an array');
  117.             }
  118.             foreach ($metas as $name => $meta) {
  119.                 [$content$extras] = $this->normalize($meta);
  120.                 $this->addMeta($type$name$content$extras);
  121.             }
  122.         }
  123.         return $this;
  124.     }
  125.     public function setHtmlAttributes(array $attributes)
  126.     {
  127.         $this->htmlAttributes $attributes;
  128.         return $this;
  129.     }
  130.     public function addHtmlAttributes($name$value)
  131.     {
  132.         $this->htmlAttributes[$name] = $value;
  133.         return $this;
  134.     }
  135.     public function removeHtmlAttributes($name)
  136.     {
  137.         unset($this->htmlAttributes[$name]);
  138.         return $this;
  139.     }
  140.     public function getHtmlAttributes()
  141.     {
  142.         return $this->htmlAttributes;
  143.     }
  144.     public function hasHtmlAttribute($name)
  145.     {
  146.         return isset($this->htmlAttributes[$name]);
  147.     }
  148.     public function setHeadAttributes(array $attributes)
  149.     {
  150.         $this->headAttributes $attributes;
  151.         return $this;
  152.     }
  153.     public function addHeadAttribute($name$value)
  154.     {
  155.         $this->headAttributes[$name] = $value;
  156.         return $this;
  157.     }
  158.     public function removeHeadAttribute($name)
  159.     {
  160.         unset($this->headAttributes[$name]);
  161.         return $this;
  162.     }
  163.     public function getHeadAttributes()
  164.     {
  165.         return $this->headAttributes;
  166.     }
  167.     public function hasHeadAttribute($name)
  168.     {
  169.         return isset($this->headAttributes[$name]);
  170.     }
  171.     public function setLinkCanonical($link)
  172.     {
  173.         $this->linkCanonical $link;
  174.         return $this;
  175.     }
  176.     public function getLinkCanonical()
  177.     {
  178.         return $this->linkCanonical;
  179.     }
  180.     public function removeLinkCanonical()
  181.     {
  182.         $this->linkCanonical '';
  183.     }
  184.     public function setSeparator($separator)
  185.     {
  186.         $this->separator $separator;
  187.         return $this;
  188.     }
  189.     public function setLangAlternates(array $langAlternates)
  190.     {
  191.         $this->langAlternates $langAlternates;
  192.         return $this;
  193.     }
  194.     public function addLangAlternate($href$hrefLang)
  195.     {
  196.         $this->langAlternates[$href] = $hrefLang;
  197.         return $this;
  198.     }
  199.     public function removeLangAlternate($href)
  200.     {
  201.         unset($this->langAlternates[$href]);
  202.         return $this;
  203.     }
  204.     public function hasLangAlternate($href)
  205.     {
  206.         return isset($this->langAlternates[$href]);
  207.     }
  208.     public function getLangAlternates()
  209.     {
  210.         return $this->langAlternates;
  211.     }
  212.     public function addOEmbedLink($title$link)
  213.     {
  214.         $this->oembedLinks[$title] = $link;
  215.         return $this;
  216.     }
  217.     public function getOEmbedLinks()
  218.     {
  219.         return $this->oembedLinks;
  220.     }
  221.     /**
  222.      * @param string|array $meta
  223.      */
  224.     private function normalize($meta): array
  225.     {
  226.         if (\is_string($meta)) {
  227.             return [$meta, []];
  228.         }
  229.         return $meta;
  230.     }
  231. }