Skip to content

Commit

Permalink
ReformatCode
Browse files Browse the repository at this point in the history
  • Loading branch information
RyujiAMANO committed Nov 27, 2015
1 parent 98ac501 commit 26d4e7f
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 39 deletions.
11 changes: 11 additions & 0 deletions Config/Migration/1448508969_count_default_0.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
<?php
/**
* CountDefaut0
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/

/**
* Class CountDefault0
*/
class CountDefault0 extends CakeMigration {

/**
Expand Down
26 changes: 16 additions & 10 deletions Test/Case/Utility/CsvFileReaderTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
/**
* Created by PhpStorm.
* User: ryuji
* Date: 2015/11/26
* Time: 14:44
* CsvFileReaderTest
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/

App::uses('NetCommonsCakeTestCase', 'NetCommons.TestSuite');
Expand All @@ -17,18 +18,23 @@
*/
class CsvFileReaderTest extends NetCommonsCakeTestCase {

/**
* Fixtures
*
* @var array
*/
/**
* Fixtures
*
* @var array
*/
public $fixtures = [];

/**
* test read
*
* @return void
*/
public function testRead() {
$csvFilePath = dirname(dirname(__DIR__)) . '/Fixture/sample_csv_excel2010.csv';
$csvReader = new CsvFileReader($csvFilePath);

foreach($csvReader as $line){
foreach ($csvReader as $line) {
debug($line);
}
}
Expand Down
38 changes: 25 additions & 13 deletions Utility/CsvFileReader.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,50 @@
<?php
/**
* Created by PhpStorm.
* User: ryuji
* Date: 2015/11/26
* Time: 14:37
* CsvFileReader
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/

// ε(     v ゚ω゚) < 改行コードCR(Excel for Mac)への対応
// ε(     v ゚ω゚) <最後に空行までよみこまれちゃうことをへの対処

App::uses('NetCommonsFile', 'Files.Utility');

class CsvFileReader extends SplFileObject{
/**
* Class CsvFileReader
*/
class CsvFileReader extends SplFileObject {

/**
* @var File 文字コード変換したテンポラリファイル
*/
protected $_tmpFile;

/**
* CsvFileReader constructor.
*
* @param string|File $filePath CSVファイルのFileインスタンスかファイルパス
*/
public function __construct($filePath) {
if (is_a($filePath, 'File')){
if (is_a($filePath, 'File')) {
$filePath = $filePath->path;
}
$tmp = NetCommonsFile::convertSjisWin2Utf8($filePath);
$tmp = NetCommonsFile::getTemporaryFileConvertSjisWin2Utf8($filePath);
$path = $tmp->path;
parent::__construct($path);
$this->setFlags(SplFileObject::READ_CSV);
}

/**
* valid SplFileObjectでCSVフィルを読ませると最終行の空配列で返るのでそれの抑止
*
* @return bool
*/
/**
* valid SplFileObjectでCSVフィルを読ませると最終行の空配列で返るのでそれの抑止
*
* @return bool
*/
public function valid() {
$var = parent::current();
if($var === array(null)){
if ($var === array(null)) {
return false;
}
return true;
Expand Down
29 changes: 19 additions & 10 deletions Utility/NetCommonsFile.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
<?php
/**
* Created by PhpStorm.
* User: ryuji
* Date: 2015/11/26
* Time: 16:44
* NetCommonsFile
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/

App::uses('TemporaryFile', 'Files.Utility');

/**
* Class NetCommonsFile
*/
class NetCommonsFile {
public static function convertSjisWin2Utf8($filePath) {

/**
* 指定されたファイルの文字コードをSjis-winからUTF-8に変換したテンポラリファイルを作成して返す
*
* @param string $filePath ファイルパス
* @return TemporaryFile 文字コードをUTF8に変換したテンポラリファイル
*/
public static function getTemporaryFileConvertSjisWin2Utf8($filePath) {
$tmp = new TemporaryFile();
$fp = fopen($filePath, 'r');
while(($line = fgets($fp)) !== false){
while (($line = fgets($fp)) !== false) {
$line = mb_convert_encoding($line, 'UTF-8', 'sjis-win');
$tmp->append($line);
}
return $tmp;
}

public static function getTemporaryFileConvertSjisWin2Utf8($filePath) {

}
}
28 changes: 22 additions & 6 deletions Utility/TemporaryFile.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
<?php
/**
* Created by PhpStorm.
* User: ryuji
* Date: 2015/11/26
* Time: 16:44
* TemporaryFile
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/

App::uses('TemporaryFolder', 'Files.Utility');
class TemporaryFile extends File{

/**
* Class TemporaryFile
*/
class TemporaryFile extends File {

/**
* @var TemporaryFolder テンポラリファイルを配置するテンポラリフォルダ
*/
protected $_tmpFolder;

/**
* TemporaryFile constructor.
*
* @param string $folderPath テンポラリフォルダを作成するフォルダパス。指定されなければテンポラリフォルダを作成する
*/
public function __construct($folderPath = null) {
if($folderPath === null){
if ($folderPath === null) {
$this->_tmpFolder = new TemporaryFolder();
$folderPath = $this->_tmpFolder->path;
}
Expand Down

0 comments on commit 26d4e7f

Please sign in to comment.