Blame view

common/components/parsers/CustomConverter.php 3.41 KB
28253169   Mihail   add converter as ...
1
  <?php
bedb55fe   Mihail   fixed Image class
2
  namespace common\components\parsers;
34d480b7   Mihail   temp commit - wor...
3
  use common\components\CustomVarDamp;
28253169   Mihail   add converter as ...
4
  use yii\multiparser\Converter;
34d480b7   Mihail   temp commit - wor...
5
  use backend\models\Details;
28253169   Mihail   add converter as ...
6
7
8
9
10
11
12
13
  
  class CustomConverter extends Converter {
  
      /**
       * @param $value_arr - двумерный массив значений, которому нужно присвоить ключи
       * @param $key_array - ключи для вложенного массива
       * @return array - таблица с проименованными колонками
       */
943f3ebe   Mihail   fixed convert to ...
14
      public static function convertToAssocArray ( array $value_arr, array $key_array, $key_prefix = '' )
28253169   Mihail   add converter as ...
15
      {
8894c93a   Mihail   add Importers fil...
16
          // очистка служебного префикса в массиве заголовков
28253169   Mihail   add converter as ...
17
          if ($key_prefix) {
943f3ebe   Mihail   fixed convert to ...
18
              array_walk( $key_array,  function ( &$value, $key, $key_prefix ){ return str_replace( $key_prefix, '',$value ); }, $key_prefix );
8894c93a   Mihail   add Importers fil...
19
20
              //уберем пустые элементы
              $key_array = array_filter($key_array,  function ($value){ return $value !==''; });
28253169   Mihail   add converter as ...
21
22
          }
  
943f3ebe   Mihail   fixed convert to ...
23
24
25
26
27
28
          $key_array = array_flip($key_array);
  //        \common\components\CustomVarDamp::dump($value_arr);
  //        \common\components\CustomVarDamp::dumpAndDie($key_array);
  
          array_walk( $value_arr,
              function ( &$value, $key, $key_array) {
28253169   Mihail   add converter as ...
29
                  $res = $value;
943f3ebe   Mihail   fixed convert to ...
30
31
                  foreach ($res as $sub_key => $sub_value) {
                      if (isset($key_array[$sub_key])) {
28253169   Mihail   add converter as ...
32
                          // если такой ключ в базовом массиве (массиве ключей) есть, то заменим новым, иначе просто удалим
943f3ebe   Mihail   fixed convert to ...
33
                          $new_key = $key_array[$sub_key];
28253169   Mihail   add converter as ...
34
                          if( !array_key_exists( $new_key , $res ) ){
943f3ebe   Mihail   fixed convert to ...
35
                              $res[ $new_key ] = $value[$sub_key];
28253169   Mihail   add converter as ...
36
37
                          }
                      }
943f3ebe   Mihail   fixed convert to ...
38
39
40
                      unset( $res[$sub_key] );
                      $value = $res;
                      //\common\components\CustomVarDamp::dump($value);
28253169   Mihail   add converter as ...
41
42
                  }
  
943f3ebe   Mihail   fixed convert to ...
43
                  //return $res;
28253169   Mihail   add converter as ...
44
              },
943f3ebe   Mihail   fixed convert to ...
45
46
47
               $key_array);
  
          return $value_arr;
28253169   Mihail   add converter as ...
48
49
      }
  
dd60c760   Mihail   add menu and chec...
50
51
52
53
54
55
      /**
       * @param $value_arr - двумерный массив к которому нужно добавить колонки
       * @param $add_array - массив с колонками (ключи) и занчениями колонок
       * @return mixed
       */
      public function addColumns ( array $value_arr ,  array $add_array )
8894c93a   Mihail   add Importers fil...
56
57
58
      {
          $i = 0;
          while ($i < count($value_arr)) {
9dd0fbe4   Mihail   add writing data ...
59
60
61
              foreach ($add_array as $add_key => $add_value) {
                  $value_arr[$i][$add_key] = $add_value;
              }
8894c93a   Mihail   add Importers fil...
62
63
              $i++;
          }
8894c93a   Mihail   add Importers fil...
64
65
          return $value_arr;
      }
34d480b7   Mihail   temp commit - wor...
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  
      public static function convertToDetails ( array $row )
      {
          // присвоим полный артикул
          $row['FULL_ARTICLE'] = $row['ARTICLE'];
  
          $details_model = new Details();
          // проверим все ли обязательные колонки были указаны пользователем
          $details_model->load(['Details' => $row]);
  
          if (!$details_model->validate()){
              $errors = '';
              foreach ( $details_model->errors as $key => $arr_errors ) {
                  $errors .= "Аттрибут $key - " . implode( ' , ', $arr_errors );
              }
              throw new \ErrorException( $errors );
          }
          return $row;
      }
  
  
  
28253169   Mihail   add converter as ...
88
  }