123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\View\Page;
- use Magento\Framework\App;
- /**
- * Page title
- *
- * @api
- * @since 100.0.2
- */
- class Title
- {
- /**
- * Default title glue
- */
- const TITLE_GLUE = ' / ';
- /**
- * @var \Magento\Framework\App\Config\ScopeConfigInterface
- */
- private $scopeConfig;
- /**
- * @var string[]
- */
- private $prependedValues = [];
- /**
- * @var string[]
- */
- private $appendedValues = [];
- /**
- * @var string
- */
- private $textValue;
- /**
- * @param App\Config\ScopeConfigInterface $scopeConfig
- */
- public function __construct(
- App\Config\ScopeConfigInterface $scopeConfig
- ) {
- $this->scopeConfig = $scopeConfig;
- }
- /**
- * Set page title
- *
- * @param string $title
- * @return $this
- */
- public function set($title)
- {
- $this->textValue = $title;
- return $this;
- }
- /**
- * Retrieve title element text (encoded)
- *
- * @return string
- */
- public function get()
- {
- return join(self::TITLE_GLUE, $this->build());
- }
- /**
- * Same as getTitle(), but return only first item from chunk
- *
- * @return mixed
- */
- public function getShort()
- {
- $title = $this->build();
- return reset($title);
- }
- /**
- * Same as getShort(), but return title without prefix and suffix
- * @return mixed
- */
- public function getShortHeading()
- {
- $title = $this->build(false);
- return reset($title);
- }
- /**
- * @param bool $withConfigValues
- * @return array
- */
- protected function build($withConfigValues = true)
- {
- return array_merge(
- $this->prependedValues,
- [$withConfigValues ? $this->addConfigValues($this->textValue) : $this->textValue],
- $this->appendedValues
- );
- }
- /**
- * @param string $title
- * @return string
- */
- protected function addConfigValues($title)
- {
- $preparedTitle = $this->scopeConfig->getValue(
- 'design/head/title_prefix',
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- ) . ' ' . $title . ' ' . $this->scopeConfig->getValue(
- 'design/head/title_suffix',
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- );
- return trim($preparedTitle);
- }
- /**
- * Retrieve default title text
- *
- * @return string
- */
- public function getDefault()
- {
- $defaultTitle = $this->scopeConfig->getValue(
- 'design/head/default_title',
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- );
- return $this->addConfigValues($defaultTitle);
- }
- /**
- * @param string $suffix
- * @return void
- */
- public function append($suffix)
- {
- $this->appendedValues[] = $suffix;
- }
- /**
- * @param string $prefix
- * @return void
- */
- public function prepend($prefix)
- {
- array_unshift($this->prependedValues, $prefix);
- }
- /**
- * Unset title
- *
- * @return void
- */
- public function unsetValue()
- {
- $this->textValue = null;
- }
- }
|