<?php

namespace common\models;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "filter_cache".
 *
 * @property integer $id
 * @property integer $options_key
 * @property integer $category_id
 * @property integer $depth
 * @property integer $count
 */
class FilterCache extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'filter_cache';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[ 'category_id', 'depth', 'count'], 'integer'],
            [['options_key'], 'string']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'options_key' => 'Options Key',
            'category_id' => 'Category ID',
            'depth' => 'Depth',
            'count' => 'Count',
        ];
    }

    /**
     * @param array $optionsIdArray
     * @return string
     */
    static function createCacheKey($optionsIdArray){
        if(!empty($optionsIdArray)){
            sort($optionsIdArray);
            $string = implode('',$optionsIdArray);
            $key = md5($string);
            return $key;
        } else {
            return 0;
        }
    }


    /**
     * @param $groups
     * @param $params
     * @return array
     */

    static function convertAliasToId($groups,$params){
        $optionsAlias = ArrayHelper::index($groups,  'option_alias');
        $convertedParams = [];
        foreach($params as $key => $options){
            foreach($options as $option){
                if(isset($optionsAlias[$option]) && !in_array($optionsAlias[$option]['tax_option_id'],$convertedParams)){
                    $convertedParams[] =  $optionsAlias[$option]['tax_option_id'];
                }
            }
        }
        return  $convertedParams;

    }

}
