AbstractProvider.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: sunmoon<i@liming.me>
  5. * Date: 2019/07/29
  6. * Time: 上午9:39
  7. */
  8. namespace common\libs\logging\operate\provider;
  9. use common\helpers\Tool;
  10. use yii\base\Component;
  11. use yii\helpers\Json;
  12. use common\libs\logging\operate\valueType\Config as ValueTypeConfig;
  13. abstract class AbstractProvider extends Component {
  14. /**
  15. * 基本数据
  16. * @var null
  17. */
  18. public $data = null;
  19. /**
  20. * 默认data为一个数组,就是form提交的数据
  21. * 如果data不是数组,说明传入的data是用来筛选修改、删除前的数据,那么dataType必须传和和data对应的字段名
  22. * 如果是批量修改、删除、插入,看BatchProvider
  23. * @var string
  24. */
  25. public $dataType = null;
  26. /**
  27. * @var null
  28. */
  29. public $fetchClass = null;
  30. /**
  31. * @var array
  32. */
  33. public $attrLabels = [];
  34. /**
  35. * @var array
  36. */
  37. public $result = [];
  38. /**
  39. * @var string
  40. */
  41. public $select = '';
  42. /**
  43. * AbstractParser constructor.
  44. * @param array $config
  45. */
  46. public function __construct($config = []) {
  47. parent::__construct($config);
  48. if(!$this->dataType){
  49. $this->dataType = 'array';
  50. }
  51. }
  52. /**
  53. * 处理数据
  54. * @param $attrLabel
  55. * @param $value
  56. * @return array
  57. */
  58. public function handleData($attrLabel, $value){
  59. if(!is_array($attrLabel)){
  60. $name = $attrLabel;
  61. if(is_array($value) || is_object($value)){
  62. $value = Json::encode($value);
  63. }
  64. }else{
  65. [$name, $value] = $this->handleComplexValue($attrLabel, $value);
  66. }
  67. return [
  68. 'label' => $name,
  69. 'value' => $value,
  70. ];
  71. }
  72. /**
  73. * 处理复杂的数据
  74. * @param $attrLabel
  75. * @param $value
  76. * @return array
  77. */
  78. public function handleComplexValue($attrLabel, $value){
  79. $valueTypes = ValueTypeConfig::getValues();
  80. $data = isset($attrLabel['data']) ? $attrLabel['data'] : [];
  81. $result = '';
  82. if(in_array($attrLabel['type'], $valueTypes)){
  83. $name = Tool::toCamelize($attrLabel['type']);
  84. $class = '\common\libs\logging\operate\valueType\\' . ucfirst($name);
  85. if(class_exists($class)){
  86. $result = (new $class([
  87. 'data' => isset($attrLabel['data']) ? $attrLabel['data'] : [],
  88. 'value' => $value,
  89. ]))->result();
  90. }
  91. }else if($attrLabel['type'] instanceof \Closure){
  92. $result = call_user_func($attrLabel['type'], [
  93. 'data' => $data,
  94. 'value' => $value,
  95. ]);
  96. }else{
  97. if(is_array($value) || is_object($value)){
  98. $result = Json::encode($value);
  99. }else{
  100. $result = $value;
  101. }
  102. }
  103. return [$attrLabel['label'], $result];
  104. }
  105. /**
  106. * 获取标签
  107. * @return bool
  108. */
  109. public function getLabels(){
  110. if(!$this->fetchClass){
  111. return false;
  112. }
  113. $fetchClass = $this->fetchClass;
  114. $class = new $fetchClass();
  115. $this->attrLabels = $class->attrLabelsWithLogType();
  116. return true;
  117. }
  118. /**
  119. * 判断是否需要组合数据
  120. * 如:会员在前台修改开户行时,地区的字段是分开的,分为BANK_PROVINCE, BANK_CITY, BANK_COUNTY
  121. * 我需要把他们三个组合在一起现实为"所在地区"
  122. * 'BANK_PROVINCE' => [
  123. 'label' => '银行所在地区',
  124. 'type' => ValueTypeConfig::AREA_TYPE,
  125. 'compose' => [
  126. 'BANK_CITY', 'BANK_COUNTY',
  127. ],
  128. ],
  129. * @param $attrLabel
  130. * @return bool
  131. */
  132. public function needCompose($attrLabel){
  133. if(isset($attrLabel['compose']) && !empty($attrLabel['compose'])){
  134. return true;
  135. }
  136. return false;
  137. }
  138. }