<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;

/**
 * This is the model class for table "document_template".
 *
 * @property int $id
 * @property string $title
 * @property string $template_body
 * @property int $status
 * @property string $created_at
 * @property string $updated_at
 */
class DocumentTemplate extends \yii\db\ActiveRecord
{
    private $fieldPattern = '/\${(№?\s*\w*|(\w*\s?)*)}/';

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'document_template';
    }

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'createdAtAttribute' => 'created_at',
                'updatedAtAttribute' => 'updated_at',
                'value' => new Expression('NOW()'),
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['status', 'title', 'template_body'], 'required'],
            [['template_body'], 'string'],
            [['status'], 'integer'],
            [['created_at', 'updated_at'], 'safe'],
            [['title'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Название',
            'template_body' => 'Тело шаблона',
            'status' => 'Статус',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    public function getFields()
    {
        preg_match_all($this->fieldPattern, $this->template_body,$out);
        return $out[0];
    }
}