<?php
/**
 * Created by PhpStorm.
 * Author: ljt
 * DateTime: 2016/10/20 11:49
 * Description:
 */

namespace backend\components;


use common\models\Circle;
use common\models\CircleCat;
use common\modules\user\models\User;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;

class Formatter extends \yii\i18n\Formatter
{
    /**
     * 格式化用户名
     * @param $value
     * @return mixed
     * @author nodelog
     */
    public function asUser($value)
    {
        return $this->asAdmin($value);
    }

    public function asAdmin($value)
    {
        return ArrayHelper::getValue(User::findOne($value), 'nickname', '');
    }

    public function asColor($value, $color = 'default')
    {
        if ($color instanceof \Closure) {
            $color = call_user_func($color, $value);
        }
        return Html::tag('span', $value, ['class' => 'text-' . $color]);
    }

    /**
     * 添加后缀
     * @param $value
     * @param string $suffix
     * @return string
     * @author nodelog
     */
    public function asSuffix($value, $suffix = '')
    {
        return Html::tag('span', $value . $suffix);
    }

    /**
     * 添加前缀
     * @param $value
     * @param string $prefix
     * @return string
     * @author nodelog
     */
    public function asPrefix($value, $prefix = '')
    {
        return Html::tag('span', $prefix . $value);
    }

    /**
     * 多张图片格式化
     * @param $values
     * @param array $options
     * @return string
     * @author nodelog
     */
    public function asImages($values, $options = [])
    {
        $html = '';
        foreach ($values as $value) {
            $html .= parent::asImage($value, $options);
        }

        return $html;
    }

    public function asCircleCat($value)
    {
        return ArrayHelper::getValue(CircleCat::findOne($value), 'title', '');
    }


    public function asCircleType($value)
    {
        return Circle::$typeList[$value];
    }



}