Completed
Push — master ( df901c...e128b1 )
by satoru
10s
created

FixedLengthImportForm::loadData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
3
namespace CsvCombine\Form;
4
5
use Cake\Form\Form;
6
7
class FixedLengthImportForm extends Form
8
{
9
    private $_defaultOptions = [
10
        'line_feed_code' => "\r\n",
11
        'directory' => TMP,
12
        'array_encoding' => 'UTF-8',
13
        'import_encoding' => 'SJIS-win',
14
        'extra_fixed_options' => []
15
    ];
16
17
    /*
18
     * loadData 固定長読み込みアクション
19
     *
20
     * @param string $fileName 固定長テキストファイ
21
     * @param array $column_list 各カラム情報(name:カラム名,length:バイト数)
0 ignored issues
show
Bug introduced by
There is no parameter named $column_list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
22
     * @param array $options 下記パラメータを必要に応じて設定
23
     * line_feed_code 改行コード(デフォルトは\r\n)
24
     * array_encoding 出力するする配列のエンコード(デフォルトはUTF-8
25
     * import_encoding 入力するテキストのエンコード(デフォルトはSJIS-win
26
     * extra_fixed_options 出力のための固定長の設定(列によって桁数が異なる場合の設定)
27
     */
28
    public function loadData($fileName, $fixed_options, $options = [])
29
    {
30
        $options = array_merge($this->_defaultOptions,$options);
31
        extract($options);
32
33
        $fp = fopen($fileName,'r');
34
        $data = fread($fp, filesize($fileName));
35
        fclose($fp);
36
37
        return $this->loadDataBody($data, $fixed_options, $options);
38
    }
39
40
    /*
41
     * loadDataBody 固定長内容読み込みアクション
42
     *
43
     * @param string $data 固定長テキストデータ
44
     * @param array $column_list 各カラム情報(name:カラム名,length:バイト数)
0 ignored issues
show
Bug introduced by
There is no parameter named $column_list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
45
     * @param array $options 下記パラメータを必要に応じて設定
46
     * line_feed_code 改行コード(デフォルトは\r\n)
47
     * array_encoding 出力するする配列のエンコード(デフォルトはUTF-8
48
     * import_encoding 入力するテキストのエンコード(デフォルトはSJIS-win
49
     * extra_fixed_options 出力のための固定長の設定(列によって桁数が異なる場合の設定)
50
     */
51
    public function loadDataBody($data, $fixed_options, $options = [])
52
    {
53
        $options = array_merge($this->_defaultOptions,$options);
54
        extract($options);
55
        
56
        $return_info = [];
57
        //まずは分割
58
        $data_explode = explode($line_feed_code, $data);
59
        $list_count = count($data_explode);
60
        foreach ($data_explode as $row => $text) {
61
            //空行は無視
62
            if (strlen($text) === 0) {
63
                continue;
64
            }
65
            $start_point = 0;
66
            $column_list = $fixed_options;
67 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...
68
                $column_list = $extra_fixed_options[$row + 1];
69
            } elseif (array_key_exists($row - $list_count + 1, $extra_fixed_options)) {
70
                $column_list = $extra_fixed_options[$row - $list_count + 1];
71
            }
72
73
            foreach ($column_list as $column_info) {
74
                $return_info[$row][$column_info['name']] = rtrim(substr($text, $start_point, $column_info['length']));
75
                $start_point += $column_info['length'];
76
            }
77
        }
78
79
        //最後にまとめて文字コードを変換
80
        mb_convert_variables($array_encoding, $import_encoding, $return_info);
81
82
        return $return_info;
83
    }
84
}
85