SingleProvider.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 yii\base\Exception;
  10. class SingleProvider extends AbstractProvider {
  11. /**
  12. * 标准化
  13. * @param null $data
  14. * @return bool
  15. */
  16. public function normalize($data = null){
  17. if(!$data){
  18. return false;
  19. }
  20. if(!$this->getLabels()){
  21. return false;
  22. }
  23. $this->result = [];
  24. foreach($data as $key => $value){
  25. if(!isset($this->attrLabels[$key])) continue;
  26. if($this->needCompose($this->attrLabels[$key])){
  27. $value = [$value];
  28. foreach($this->attrLabels[$key]['compose'] as $field){
  29. if(isset($data[$field])){
  30. $value[] = $data[$field];
  31. }
  32. }
  33. }
  34. $this->result[$key] = $this->handleData($this->attrLabels[$key], $value);
  35. }
  36. return true;
  37. }
  38. /**
  39. * @return bool
  40. */
  41. public function singleGetData(){
  42. if(!$this->data){
  43. return false;
  44. }
  45. // 说明传入的是delete id
  46. // data必须和dateType对应
  47. if(is_string($this->data)){
  48. if(!$this->dataType){
  49. return false;
  50. }
  51. $fetchClass = $this->fetchClass;
  52. if($this->select){
  53. $row = $fetchClass::find()->select($this->select)->is($this->data, $this->dataType)->asArray()->one();
  54. }else{
  55. $row = $fetchClass::find()->selectNoText()->is($this->data, $this->dataType)->asArray()->one();
  56. }
  57. $this->normalize($row);
  58. unset($fetchClass, $row);
  59. }else{
  60. $this->normalize($this->data);
  61. }
  62. return true;
  63. }
  64. public function beforeUpdate(){
  65. $this->singleGetData();
  66. }
  67. public function afterUpdate(){
  68. $this->singleGetData();
  69. }
  70. public function beforeDelete(){
  71. $this->singleGetData();
  72. }
  73. public function afterInsert(){
  74. $this->singleGetData();
  75. }
  76. }