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; } }