* Date: 2019/07/29 * Time: 上午9:39 */ namespace common\libs\logging\operate\provider; use common\helpers\Tool; use yii\base\Component; use yii\helpers\Json; use common\libs\logging\operate\valueType\Config as ValueTypeConfig; abstract class AbstractProvider extends Component { /** * 基本数据 * @var null */ public $data = null; /** * 默认data为一个数组,就是form提交的数据 * 如果data不是数组,说明传入的data是用来筛选修改、删除前的数据,那么dataType必须传和和data对应的字段名 * 如果是批量修改、删除、插入,看BatchProvider * @var string */ public $dataType = null; /** * @var null */ public $fetchClass = null; /** * @var array */ public $attrLabels = []; /** * @var array */ public $result = []; /** * @var string */ public $select = ''; /** * AbstractParser constructor. * @param array $config */ public function __construct($config = []) { parent::__construct($config); if(!$this->dataType){ $this->dataType = 'array'; } } /** * 处理数据 * @param $attrLabel * @param $value * @return array */ public function handleData($attrLabel, $value){ if(!is_array($attrLabel)){ $name = $attrLabel; if(is_array($value) || is_object($value)){ $value = Json::encode($value); } }else{ [$name, $value] = $this->handleComplexValue($attrLabel, $value); } return [ 'label' => $name, 'value' => $value, ]; } /** * 处理复杂的数据 * @param $attrLabel * @param $value * @return array */ public function handleComplexValue($attrLabel, $value){ $valueTypes = ValueTypeConfig::getValues(); $data = isset($attrLabel['data']) ? $attrLabel['data'] : []; $result = ''; if(in_array($attrLabel['type'], $valueTypes)){ $name = Tool::toCamelize($attrLabel['type']); $class = '\common\libs\logging\operate\valueType\\' . ucfirst($name); if(class_exists($class)){ $result = (new $class([ 'data' => isset($attrLabel['data']) ? $attrLabel['data'] : [], 'value' => $value, ]))->result(); } }else if($attrLabel['type'] instanceof \Closure){ $result = call_user_func($attrLabel['type'], [ 'data' => $data, 'value' => $value, ]); }else{ if(is_array($value) || is_object($value)){ $result = Json::encode($value); }else{ $result = $value; } } return [$attrLabel['label'], $result]; } /** * 获取标签 * @return bool */ public function getLabels(){ if(!$this->fetchClass){ return false; } $fetchClass = $this->fetchClass; $class = new $fetchClass(); $this->attrLabels = $class->attrLabelsWithLogType(); return true; } /** * 判断是否需要组合数据 * 如:会员在前台修改开户行时,地区的字段是分开的,分为BANK_PROVINCE, BANK_CITY, BANK_COUNTY * 我需要把他们三个组合在一起现实为"所在地区" * 'BANK_PROVINCE' => [ 'label' => '银行所在地区', 'type' => ValueTypeConfig::AREA_TYPE, 'compose' => [ 'BANK_CITY', 'BANK_COUNTY', ], ], * @param $attrLabel * @return bool */ public function needCompose($attrLabel){ if(isset($attrLabel['compose']) && !empty($attrLabel['compose'])){ return true; } return false; } }