| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Sonar\Common\Imports; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Symfony\Component\Yaml\Yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | abstract class AbstractImport | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     protected $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     private $filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 | 8 |  |     public function __construct(Filesystem $filesystem = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 8 |  |         $this->config = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 | 8 |  |         $this->filesystem = $filesystem ? $filesystem : new Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 8 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 | 5 |  |     public function setConfig($config_file, $is_force = false) | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 | 5 |  |         $config = Yaml::parse($config_file); | 
            
                                                                        
                            
            
                                    
            
            
                | 21 | 5 |  |         if (is_array($config) === false || $is_force) { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 | 1 |  |             $config = Yaml::parse($this->filesystem->get($config_file)); | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 1 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 5 |  |         if (is_array($config)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 5 |  |             $this->config = array_merge($this->config, $config); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 5 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 1 |  |     public function getConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 1 |  |         return $this->config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 5 |  |     public function setModels($models, $csv) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 5 |  |         foreach ($models as $table => $model) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 5 |  |             if (is_array($model) == false ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 5 |  |             $total = count($model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 5 |  |             for ($i = 0; $i < $total; $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 5 |  |                 if (isset($this->config[$table][$i])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 4 |  |                     $this->setModel($model[$i], $this->config[$table][$i], $csv, $table); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 2 |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |                     throw new \Exception('設定ファイルが正しくないか、構成が異なっています。table=' . $table); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 2 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 4 |  |     public function setModel($model, $config, $csv, $table) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 4 |  |         foreach ($config as $key => $rec) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 4 |  |             $col = $this->getCsvCol($rec); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 4 |  |             $func = ( isset($rec['func']) === true && $rec['func'] ) ? $rec['func'] : null;  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 4 |  |             if (method_exists($this, $func) === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |                 $this->$func($model, $key, $csv, $col); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 4 |  |             } elseif ( $func && method_exists($this, $func) === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |                 throw new \Exception(get_class($this) . 'に関数=' . $func . 'が実装されていません。(table=' . $table . ')'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 4 |  |             } elseif (isset($rec['csv']) === true && isset($csv[$col]) === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 3 |  |                 $model->$key = $csv[$col]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 3 |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  |                 $model->$key = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 4 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 3 |  |         $model->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 4 |  |     private function getCsvCol($rec) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 4 |  |         $col = isset($rec['csv']) ? $rec['csv'] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 4 |  |         if (strpos($col, ",") !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |             $col = explode(",", $col); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 4 |  |         } elseif ( is_numeric($col) === true ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 2 |  |             $col = ($col + 0 ) -1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 2 |  |         }  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 4 |  |         return $col; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  | 
            
                        
When comparing two booleans, it is generally considered safer to use the strict comparison operator.