<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/3/19
 * Time: 下午10:46
 */
require_once (BASE_ROOT_PATH . '/helper/history_helper.php');

class search_param
{
    private $mKeyWord;
    private $mBrandId;
    private $mHotId;
    private $mOrder;
    private $mSort;

    public function __construct($param)
    {
        $this->mKeyWord = '';
        $this->mBrandId = 0;
        $this->mHotId = 0;

        if (!empty(trim($param['keyword']))) {
            $keywod = trim(urldecode($param['keyword']));
            if ($this->check($keywod)) {
                $this->mKeyWord = trim(urldecode($param['keyword']));
                $helper = new history_helper();
                $helper->add_word($this->mKeyWord);
            }
        }
        if (intval($param['brand_id']) > 0) {
            $this->mBrandId = intval($param['brand_id']);
        }
        if (!empty(trim($param['hot_id']))) {
            $this->mHotId = intval($param['hot_id']);
        }

        //下面两个参数和客户端定义反了
        $this->mOrder = 'click';
        if (!empty($param['sort']))
        {
            $order = strtolower(trim($param['sort']));
            if(in_array($order,array('click','salenum','price'))) {
                $this->mOrder = $order;
            }
        }

        $this->mSort = 'desc';
        if (!empty($param['order']))
        {
            $sort = strtolower(trim($param['order']));
            if(in_array($sort,array('asc','desc'))) {
                $this->mSort = $sort;
            }
        }
    }

    public function format($page_no = null,$page_size = null)
    {
        if (empty($this->mKeyWord) && $this->mBrandId == 0 && $this->mHotId == 0) {
            return false;
        } else {
            $result = [];
            $result['keyword'] = $this->mKeyWord;
            $result['brand_id'] = $this->mBrandId;
            $result['hot_id'] = $this->mHotId;
            $result['order'] = $this->mOrder;
            $result['sort'] = $this->mSort;
            $result['page_no'] = $page_no;
            $result['page_size'] = $page_size;

            return $result;
        }
    }

    public function brand_id()
    {
        return $this->mBrandId;
    }

    private function check($keyword)
    {
        $words = search\filter::filter($keyword);
        if (empty($words)) {
            return false;
        } else {
            return true;
        }
    }
}