SeoProduct.php 3.08 KB
<?php

namespace frontend\components;

use artbox\core\models\Alias;
use artbox\core\models\interfaces\AliasInterface;
use yii\base\Object;

class SeoProduct extends Object implements AliasInterface
{
    /**
     * @var int
     */
    public $languageId;

    /**
     * @var string
     */
    public $productTitle;

    public $productPrice;

    /**
     * @var string
     */
    public $tel =  '(044) 490 70 07';

    /**
     * @var Alias
     */
    public $productAlias;

    /**
     * @var string
     */
    protected $title = '';

    /**
     * @var string
     */
    protected $description = '';

    /**
     * @var string
     */
    protected $h1 = '';

    /**
     * @var string
     */
    protected $robots = '';

    /**
     * @var string
     */
    protected $text = '';

    public function init()
    {
        $this->generateData();

        $this->generateRobots();
    }

    public function getFields(): string
    {
        return '';
    }
    public function getSeoText(): string
    {
        return $this->text ?? '';
    }
    public function getId()
    {
        return 0;
    }
    public function getTitle(): string
    {
        return $this->title ?? '';
    }

    public function getDesc(): string
    {
        return $this->description ?? '';
    }

    public function getH1(): string
    {
        return $this->h1 ?? '';
    }

    public function getText(): string
    {
        return $this->text ?? '';
    }

    public function getRobots(): string
    {
        return $this->robots ?? '';
    }
    private function selectH1()
    {
        if (!empty($this->productAlias->h1)) {
            $this->h1 = $this->productAlias->h1;
        } else {
            $this->h1 = $this->productTitle;
        }
    }
    protected function selectDescription() {
//        if (!empty($this->productAlias->description)) {
//            $this->description = $this->productAlias->description;
//        } else {
            $this->description = 'Купити '.$this->h1.' в інтернет-магазині ACDC | Тільки оригінал > Доступні ціни > Покупка в кредит > Швидка доставка по Україні';
//        }
    }
    protected function selectTitle()
    {
//        if (!empty($this->productAlias->title)) {         // у всех продуктов задан алиас, пришлось убрать кастомные алиасы,
//            $this->title = $this->productAlias->title;    // чтобы применить массовые
//        } else {
            $this->title = $this->h1.' купити в Україні: Київ, Харків, Одесса - ціна, фото, відгуки | інтернет-магазин ACDC';
//        }
    }

    private function generateData()
    {
        $this->selectH1();
        $this->selectTitle();
        $this->selectDescription();
    }
    private function generateRobots()
    {
        if(!empty($this->productAlias->robots)) {
            $this->robots = $this->productAlias->robots;
        } else {
            $this->robots =  'index, follow';
        }
    }
}