Completed
Push — master ( e128b1...4a7081 )
by satoru
03:16 queued 36s
created

FixedLengthExportComponent::getRawData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace CsvCombine\Controller\Component;
3
use Cake\Controller\Component;
4
use Cake\Core\Configure;
5
use Cake\Event\Event;
6
use Cake\Network\Exception\MethodNotAllowedException;
7
8
9
/**
10
 * FixedLengthExportComponent  code license:
11
 *
12
 * @copyright Copyright (C) 2011 hagiwara.
13
 * @since CakePHP(tm) v 1.3
14
 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
15
 */
16
class FixedLengthExportComponent extends Component {
17
18
    private $_controller;
19
    private $_defaultOptions = [
20
        'file_name' => 'export.txt',
21
        'line_feed_code' => "\r\n",
22
        'directory' => TMP,
23
        'export_encoding' => 'SJIS-win',
24
        'array_encoding' => 'UTF-8',
25
        'extra_fixed_options' => []
26
    ];
27
    private $_textData = '';
28
29
    /**
30
     * コンポーネント初期化
31
     *
32
     * @access public
33
     */
34
    public function startup(Event $event) {
35
        $this->_controller = $event->subject();
36
    }
37
38
    /*
39
     * export 固定長の出力アクション
40
     *
41
     * @param array $list 出力のための配列(二次元配列が基本)
42
     * @param array $fixed_options 出力のための固定長の設定(各カラムのバイト数及び型)
43
     * @param array $options 下記パラメータを必要に応じて設定
44
     * file_name 出力ファイル名(デフォルトはexport.txt)
45
     * line_feed_code 改行コード(デフォルトは\r\n)
46
     * $directory 一時保存ディレクトリ(デフォルトはTMP,最終的に削除をする)
47
     * export_encoding 出力するファイルのエンコード(デフォルトはSJIS-win
48
     * array_encoding 入力する配列のエンコード(デフォルトはUTF-8
49
     * extra_fixed_options 出力のための固定長の設定(列によって桁数が異なる場合の設定)
50
     */
51
    public function export($list, $fixed_options, $options)
52
    {
53
        $options = array_merge($this->_defaultOptions,$options);
54
        extract($options);
55
56
        //layoutを切って autoRenderも外しておく
57
        $this->_controller->viewBuilder()->layout('ajax');
58
        $this->_controller->autoRender = false;
59
60
        //headerのセット
61
        $save_directory = $this->make($list, $fixed_options, $options);
62
        $basename = basename($save_directory);
63
        $this->_controller->response->file($save_directory, ['download' => true, 'name' => $basename]);
64
    }
65
66
    /*
67
     * make 固定長の作成アクション
68
     *
69
     * @param array $list 出力のための配列(二次元配列が基本)
70
     * @param array $fixed_options 出力のための固定長の設定(各カラムのバイト数)
71
     * @param array $options 下記パラメータを必要に応じて設定
72
     * file_name 出力ファイル名(デフォルトはexport.txt)
73
     * line_feed_code 改行コード(デフォルトは\r\n)
74
     * $directory 一時保存ディレクトリ(デフォルトはTMP,最終的に削除をする)
75
     * export_encoding 出力するファイルのエンコード(デフォルトはSJIS-win
76
     * array_encoding 入力する配列のエンコード(デフォルトはUTF-8
77
     * extra_fixed_options 出力のための固定長の設定(列によって桁数が異なる場合の設定)
78
     */
79
    public function make($list, $fixed_options, $options)
80
    {
81
        Configure::write('debug', 0);
82
        ini_set("memory_limit", -1);
83
        set_time_limit(0);
84
85
        $this->_textData = '';
86
        $options = array_merge($this->_defaultOptions,$options);
87
        extract($options);
88
89
        mb_convert_variables($export_encoding, $array_encoding, $list);
90
91
        // keyを振りなおしておく。
92
        $list = array_merge($list);
93
        $list_count = count($list);
94
        //$listにカンマか"がいた時の対応
95
        $return_text = '';
96
        foreach ($list as $row => $list_val) {
97
            $column_options = $fixed_options;
98 View Code Duplication
            if (array_key_exists($row + 1, $extra_fixed_options)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
99
                $column_options = $extra_fixed_options[$row + 1];
100
            } elseif (array_key_exists($row - $list_count, $extra_fixed_options)) {
101
                $column_options = $extra_fixed_options[$row - $list_count];
102
            }
103
104
            foreach ($column_options as $fixed_option_key => $fixed_info) {
105
                if (!array_key_exists($fixed_option_key, $list_val)) {
106
                    //必要なデータが存在しないエラー
107
                    throw new MethodNotAllowedException('data not exist');
108
                } else if (strlen($list_val[$fixed_option_key]) > $fixed_info['length']) {
109
                    throw new MethodNotAllowedException('length error');
110
                }
111
112
                if ($fixed_info['type'] == 'text') {
113
                    $return_text .= str_pad($list_val[$fixed_option_key], $fixed_info['length']);
114
                } elseif ($fixed_info['type'] == 'integer') {
115
                    $return_text .= sprintf('%0' . $fixed_info['length'] . 's', ($list_val[$fixed_option_key]));
116
                } else {
117
                    throw new MethodNotAllowedException('type error');
118
                }
119
            }
120
            $return_text .= $line_feed_code;
121
        }
122
123
        $this->_textData = $return_text;
124
        $save_directory = $directory . $file_name;
125
        $fp = fopen($save_directory, 'w');
126
        fwrite($fp, $return_text);
127
128
        fclose($fp);
129
130
        return $save_directory;
131
    }
132
133
    /*
134
     * getRawData ファイルに出力した生テキストデータを取得
135
     */
136
    public function getRawData()
137
    {
138
        return $this->_textData;
139
    }
140
}
141