1
|
|
|
<?php |
2
|
|
|
namespace Sonar\Common\Imports; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Yaml\Yaml; |
5
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
6
|
|
|
use File; |
7
|
|
|
|
8
|
|
|
abstract class AbstractImport |
9
|
|
|
{ |
10
|
|
|
protected $config; |
11
|
|
|
|
12
|
7 |
|
public function __construct() |
13
|
|
|
{ |
14
|
7 |
|
$this->config = []; |
15
|
7 |
|
} |
16
|
|
|
|
17
|
4 |
|
public function setConfig($config_file) |
18
|
|
|
{ |
19
|
4 |
|
$config = Yaml::parse($config_file); |
20
|
4 |
|
if ( is_array($config) === false ) { |
21
|
|
|
$config = Yaml::parse(File::get($config_file)); |
22
|
|
|
} |
23
|
4 |
|
if ( is_array($config) ) { |
24
|
4 |
|
$this->config = array_merge($this->config,$config); |
25
|
4 |
|
} |
26
|
4 |
|
} |
27
|
1 |
|
public function getConfig() |
28
|
|
|
{ |
29
|
1 |
|
return $this->config; |
30
|
|
|
} |
31
|
4 |
|
public function setModels($models,$csv) |
32
|
|
|
{ |
33
|
4 |
|
foreach ( $models as $table => $model ) { |
34
|
4 |
|
if ( is_array($model) ) { |
35
|
4 |
|
$total = count($model); |
36
|
4 |
|
for($i=0;$i<$total;$i++) { |
37
|
4 |
|
if ( isset($this->config[$table][$i]) ) { |
38
|
3 |
|
$this->setModel($model[$i],$this->config[$table][$i],$csv,$table); |
39
|
1 |
|
} else { |
40
|
1 |
|
throw new \Exception('設定ファイルが正しくないか、構成が異なっています。table=' . $table); |
41
|
|
|
} |
42
|
1 |
|
} |
43
|
1 |
|
} |
44
|
1 |
|
} |
45
|
1 |
|
return true; |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
public function setModel($model,$config,$csv,$table) |
|
|
|
|
49
|
|
|
{ |
50
|
3 |
|
foreach ( $config as $key => $rec ) { |
51
|
3 |
|
if ( isset($rec['func']) === true && $rec['func'] ) { |
52
|
2 |
|
$func = $rec['func']; |
53
|
2 |
|
$col = isset($rec['csv']) ? $rec['csv'] : null; |
54
|
2 |
|
if ( strpos($col,",") !== false ) { |
55
|
2 |
|
$col = explode(",",$col); |
56
|
2 |
|
} |
57
|
2 |
|
if ( method_exists($this,$func) === true ) { |
58
|
1 |
|
$this->$func($model,$key,$csv,$col); |
59
|
1 |
|
} else { |
60
|
1 |
|
throw new \Exception(get_class($this) . 'に関数=' . $func . 'が実装されていません。'); |
61
|
|
|
} |
62
|
3 |
|
} elseif ( isset($rec['csv']) && is_numeric($rec['csv']) === true && isset($csv[($rec['csv']+0)-1]) ) { |
63
|
2 |
|
$model->$key = $csv[($rec['csv']+0)-1]; |
64
|
3 |
|
} elseif ( isset($rec['csv']) && is_numeric($rec['csv']) === false && isset($csv[$rec['csv']]) ) { |
65
|
|
|
$model->$key = $csv[$rec['csv']]; |
66
|
|
|
} else { |
67
|
1 |
|
$model->$key = null; |
68
|
|
|
} |
69
|
3 |
|
} |
70
|
2 |
|
$model->save(); |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.