SeoComponent.php 5.9 KB
<?php
    
    namespace artbox\core\components;

    use artbox\core\exceptions\AliasOverwriteException;
    use artbox\core\models\interfaces\AliasInterface;
    use yii\base\Object;
    use yii\db\ActiveRecord;
    use yii\helpers\Json;

    /**
     * Class SeoComponent
     *
*@package artbox\core\components
     * @property AliasInterface $alias
     * @property string         $title
     * @property string         $desc
     * @property string         $h1
     * @property string         $text
     * @property string         $robots
     * @property integer        $aliasId
     * @property boolean        $loaded
     */
    class SeoComponent extends Object
    {
        /**
         * Property to show wether Alias was loaded or not
         *
         * @var bool
         */
        protected $loaded = false;
    
        /**
         * Data that will be replaced in seo fields
         *
         * @var array
         */
        protected $fields = [];
    
        /**
         * Seo title
         *
         * @var string
         */
        protected $title;
    
        /**
         * meta description
         *
         * @var string
         */
        protected $desc;
    
        /**
         * h1 tag on pages
         *
         * @var string
         */
        protected $h1;
    
        /**
         * Seo text
         *
         * @var string
         */
        protected $text;
    
        /**
         * The alias model that contains seo data
         * sets in Seo url manager
         *
         * @var AliasInterface
         */
        protected $alias;
    
        /**
         * ActiveRecord instance that contains actual data for replacing
         *
         * @var ActiveRecord
         */
        protected $model;
    
        /**
         * Getter for protected property
         *
         * @return bool
         */
        public function getLoaded(): bool
        {
            return $this->loaded;
        }


        /**
         * Setter for protected property
         *
         * @return void
         */
        public function setLoaded(bool $status)
        {
            $this->loaded = $status;
        }
    
        /**
         * Setting alias model
         *
         * @param \artbox\core\models\interfaces\AliasInterface $alias
         *
         * @throws \artbox\core\exceptions\AliasOverwriteException
         */
        public function setAlias(AliasInterface $alias)
        {
            if ($this->loaded) {
                throw new AliasOverwriteException();
            }
        
            $this->alias = $alias;
        
            $this->title = $alias->getTitle();
            $this->desc = $alias->getDesc();
            $this->h1 = $alias->getH1();
            $this->text = $alias->getText();
        
            $this->loaded = true;
        }
    
        /**
         * @return int
         */
        public function getAliasId()
        {
            return $this->alias->getId();
        }

        /**
         * @return AliasInterface
         */
        public function getAlias()
        {
            return $this->alias;
        }
    
        /**
         * @return string
         */
        public function getTitle()
        {
            return $this->title;
        }
    
        /**
         * @return string
         */
        public function getDesc()
        {
            return $this->desc;
        }
    
        /**
         * @return string
         */
        public function getH1()
        {
            return $this->h1;
        }
    
        /**
         * @return string
         */
        public function getText()
        {
            return $this->text;
        }
    
        /**
         * @return string
         */
        public function getRobots()
        {
            if (!empty($this->alias) && !empty($this->alias->getRobots())) {
                return $this->alias->getRobots();
            }
    
            return 'all';
        }
    
        /**
         * Setting up ActiveRecord model and starting to generate data
         *
         * @param \yii\db\ActiveRecord $model
         */
        public function setModel(ActiveRecord $model)
        {
            $this->model = $model;
        
            $this->generateData();
        }
    
        /**
         * Trying to get field values with all dependecies
         * if it fails fill it with empty string
         *
         * @return void
         */
        protected function populateFields()
        {
            $fields = array_keys(Json::decode($this->alias->getFields()) ? : []);
            foreach ($fields as $field) {
                $params = explode('.', $field);
            
                $chain = $this->model;
            
                foreach ($params as $param) {
                    if (!empty($chain->$param)) {
                        $chain = $chain->$param;
                    } else {
                        $chain = '';
                        break;
                    }
                }
                $this->fields[ '[[' . $field . ']]' ] = $chain;
            }
        }
    
        /**
         * Populating self data with modified information from Alias model
         *
         * @return void
         */
        protected function generateData()
        {
            $this->populateFields();
        
            $title = $this->alias->getTitle();
            $desc = $this->alias->getDesc();
            $h1 = $this->alias->getH1();
            $text = $this->alias->getSeoText();
        
            foreach ($this->fields as $search => $replace) {
                $title = str_replace($search, $replace, $title);
                $desc = str_replace($search, $replace, $desc);
                $h1 = str_replace($search, $replace, $h1);
                $text = str_replace($search, $replace, $text);
            }
        
            $this->title = $title;
            $this->desc = $desc;
            $this->h1 = $h1;
            $this->text = $text;
        }
    }