<?php namespace Cate\Api;
use Dever;
class Icon
{
    public function getList()
    {
        $id = Dever::input('id');
        $name = Dever::input('name');
        $where = array();
        if ($name && $name != '{name}') {
            $where['search_name'] = array('like', $name);
        }
        $list = array();
        if ($id) {
            $info = Dever::db('icon', 'cate')->find($id);
            if ($info) {
                $this->getSvg($info);
                $info['class'] = 'show-selector-active';
                $list[] = $info;
            }
        }
        $set['num'] = 12;
        $data = Dever::db('icon', 'cate')->select($where, $set);
        foreach ($data as $k => $v) {
            if ($id && $v['id'] == $id) {
                continue;
            }
            $this->getSvg($v);
            $list[] = $v;
        }
        return array('list' => $list, 'total' => Dever::page('total'));
    }

    private function getSvg(&$v)
    {
        $v['type'] = 'svg';
        $pattern = '/(width|height): [^;]+;/';
        $replacement = 'width: 30px; height: 30px;';
        $v['svg'] = preg_replace($pattern, $replacement, $v['svg']);
        $v['class'] = '';
    }
}