1
|
|
|
<?php |
2
|
|
|
namespace CsvCombine\Export; |
3
|
|
|
|
4
|
|
|
use CsvCombine\Export\AppExport; |
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* FixedLengthExport code license: |
9
|
|
|
* |
10
|
|
|
* @copyright Copyright (C) 2017 hagiwara. |
11
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
12
|
|
|
* @author hagiwara |
13
|
|
|
*/ |
14
|
|
|
class FixedLengthExport extends AppExport { |
15
|
|
|
|
16
|
|
|
/* |
17
|
|
|
* make 固定長の作成アクション |
18
|
|
|
* |
19
|
|
|
* @param array $list 出力のための配列(二次元配列が基本) |
20
|
|
|
* @param array $filePath 出力のための固定長の設定(各カラムのバイト数) |
21
|
|
|
* @param array $fixedOptions 出力のための固定長の設定(各カラムのバイト数) |
22
|
|
|
* @param array $options 下記パラメータを必要に応じて設定 |
23
|
|
|
* line_feed_code 改行コード(デフォルトは\r\n) |
24
|
|
|
* directory 一時保存ディレクトリ(デフォルトはTMP,最終的に削除をする) |
25
|
|
|
* export_encoding 出力するファイルのエンコード(デフォルトはSJIS-win |
26
|
|
|
* array_encoding 入力する配列のエンコード(デフォルトはUTF-8 |
27
|
|
|
* extra_fixed_options 出力のための固定長の設定(列によって桁数が異なる場合の設定) |
28
|
|
|
*/ |
29
|
|
|
public function make($list, $filePath, $fixedOptions, $options = []) |
30
|
|
|
{ |
31
|
|
|
$fp = fopen($filePath, 'w'); |
32
|
|
|
fwrite($fp, $this->makeText($list, $fixedOptions, $options)); |
33
|
|
|
fclose($fp); |
34
|
|
|
|
35
|
|
|
return true; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/* |
39
|
|
|
* getRawData ファイルに出力した生テキストデータを取得 |
40
|
|
|
* @param array $list 出力のための配列(二次元配列が基本) |
41
|
|
|
* @param array $fixedOptions 出力のための固定長の設定(各カラムのバイト数) |
42
|
|
|
* @param array $options 下記パラメータを必要に応じて設定 |
43
|
|
|
* file_name 出力ファイル名(デフォルトはexport.txt) |
44
|
|
|
* line_feed_code 改行コード(デフォルトは\r\n) |
45
|
|
|
* directory 一時保存ディレクトリ(デフォルトはTMP,最終的に削除をする) |
46
|
|
|
* export_encoding 出力するファイルのエンコード(デフォルトはSJIS-win |
47
|
|
|
* array_encoding 入力する配列のエンコード(デフォルトはUTF-8 |
48
|
|
|
* extra_fixed_options 出力のための固定長の設定(列によって桁数が異なる場合の設定) |
49
|
|
|
*/ |
50
|
|
|
public function getRawData($list, $fixedOptions, $options = []) |
51
|
|
|
{ |
52
|
|
|
return $this->makeText($list, $fixedOptions, $options); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/* |
56
|
|
|
* makeText ファイルに出力した生テキストデータを取得 |
57
|
|
|
* @param array $list 出力のための配列(二次元配列が基本) |
58
|
|
|
* @param array $fixedOptions 出力のための固定長の設定(各カラムのバイト数) |
59
|
|
|
* @param array $options 下記パラメータを必要に応じて設定 |
60
|
|
|
* file_name 出力ファイル名(デフォルトはexport.txt) |
61
|
|
|
* line_feed_code 改行コード(デフォルトは\r\n) |
62
|
|
|
* directory 一時保存ディレクトリ(デフォルトはTMP,最終的に削除をする) |
63
|
|
|
* export_encoding 出力するファイルのエンコード(デフォルトはSJIS-win |
64
|
|
|
* array_encoding 入力する配列のエンコード(デフォルトはUTF-8 |
65
|
|
|
* extra_fixed_options 出力のための固定長の設定(列によって桁数が異なる場合の設定) |
66
|
|
|
*/ |
67
|
|
|
private function makeText($list, $fixedOptions, $options) |
68
|
|
|
{ |
69
|
|
|
$options = $this->getOptions($options); |
70
|
|
|
extract($options); |
71
|
|
|
|
72
|
|
|
mb_convert_variables($exportEncoding, $arrayEncoding, $list); |
73
|
|
|
|
74
|
|
|
// keyを振りなおしておく。 |
75
|
|
|
$list = array_merge($list); |
76
|
|
|
$listCount = count($list); |
77
|
|
|
//$listにカンマか"がいた時の対応 |
78
|
|
|
$returnText = ''; |
79
|
|
|
foreach ($list as $row => $listVal) { |
80
|
|
|
$columnOptions = $this->getColumnOptions($fixedOptions, $row, $extraFixedOptions, $listCount); |
81
|
|
|
|
82
|
|
|
foreach ($columnOptions as $fixedOptionKey => $fixedInfo) { |
83
|
|
|
$returnText .= $this->valueSet($fixedOptionKey, $listVal, $fixedInfo); |
84
|
|
|
} |
85
|
|
|
$returnText .= $lineFeedCode; |
86
|
|
|
} |
87
|
|
|
return $returnText; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* getColumnOptions |
92
|
|
|
* カラムオプションの取得 |
93
|
|
|
* @author hagiwara |
94
|
|
|
*/ |
95
|
|
|
private function getColumnOptions($fixedOptions, $row, $extraFixedOptions, $listCount) |
96
|
|
|
{ |
97
|
|
|
$columnOptions = $fixedOptions; |
98
|
|
View Code Duplication |
if (array_key_exists($row + 1, $extraFixedOptions)) { |
|
|
|
|
99
|
|
|
$columnOptions = $extraFixedOptions[$row + 1]; |
100
|
|
|
} elseif (array_key_exists($row - $listCount, $extraFixedOptions)) { |
101
|
|
|
$columnOptions = $extraFixedOptions[$row - $listCount]; |
102
|
|
|
} |
103
|
|
|
return $columnOptions; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* valueSet |
108
|
|
|
* カラムごとの値のセット |
109
|
|
|
* @author hagiwara |
110
|
|
|
*/ |
111
|
|
|
private function valueSet($fixedOptionKey, $listVal, $fixedInfo) |
112
|
|
|
{ |
113
|
|
|
// 存在チェック |
114
|
|
|
if (!array_key_exists($fixedOptionKey, $listVal)) { |
115
|
|
|
//必要なデータが存在しないエラー |
116
|
|
|
throw new Exception('data not exist'); |
117
|
|
|
} else if (strlen($listVal[$fixedOptionKey]) > $fixedInfo['length']) { |
118
|
|
|
throw new Exception('length error'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// typeごとの値のセット |
122
|
|
|
if ($fixedInfo['type'] == 'text') { |
123
|
|
|
$returnText = str_pad($listVal[$fixedOptionKey], $fixedInfo['length']); |
124
|
|
|
} elseif ($fixedInfo['type'] == 'integer') { |
125
|
|
|
$returnText = sprintf('%0' . $fixedInfo['length'] . 's', ($listVal[$fixedOptionKey])); |
126
|
|
|
} else { |
127
|
|
|
throw new Exception('type error'); |
128
|
|
|
} |
129
|
|
|
return $returnText; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.