PHPでバッチまたは単一のファイルをエンコーディング変換するプログラム

PHPコード:
<?php

class ConvertEncode {

/**
* 変換エンコーディング
* @var string
*/
private $_to_encoding;

/**
*変換前エンコーディング
* @var string
*/
private $_from_encoding;

/**
* 単一のファイルまたはディレクトリを変換
* @var string
*/
private $_path;

/**
* 指定されたディレクトリを設定
* @var boolean
*/
private $_directory;

/**
* 再帰トラバーサルを判断
* @var boolean
*/
private $_recursion;

/**
*全ての変換されるファイルを保存
* @var array
*/
private $_files = array();

/**
* コンストラクタ関数
*/
public function __construct() {
if( ! function_exists('mb_convert_encoding’) ) {
throw new ConvertException('mbstring extension be required’);
}
}

/**
* 変換ディレクトリまたは単一のファイルを設定
* @param string $path  ディレクトリまたはファイル
*/
public function setPath($path, $is_dir = false, $rec = false) {
$this->_path = $path;
$this->_directory = $is_dir;
$this->_recursion = $rec;
return true;
}

/**
* 変換前コーディングおよびエンコーディング
* @param string $encode_from 変換前コーディング
* @param string $encode_to  変換エンコーディング
* @return boolean
*/
public function setEncode($encode_from, $encode_to) {
$this->_from_encoding = $encode_from;
$this->_to_encoding = $encode_to;
return true;
}

/**
* ディレクトリの設定によってコーディングを変換
* @return boolean
*/
public function convert() {
if($this->_directory ) {
return $this->_convertDirectory();
}
return $this->_convertFile();
}

/**
* ファイルを変換
* @throws ConvertException
* @return boolean
*/
private function _convertFile() {
if( ! file_exists($this->_path) ) {
$message = $this->_path . ' 存在しません.’;
throw new ConvertException($message);
}
if( ! is_file($this->_path) ) {
$message = $this->_path . ' ファイではありません.’;
throw new ConvertException($message);
}
if( ! $this->_isWR() ) {
$message = $this->_path . '読み取りおよび書き込み権限が必要.’;
throw new ConvertException($message);
}
$file_real_path = realpath($this->_path);
$file_content_from = file_get_contents( $file_real_path );
if( mb_check_encoding($file_content_from, $this->_from_encoding) ) {
$file_content_to = mb_convert_encoding( $file_content_from, $this->_to_encoding, $this->_from_encoding );
file_put_contents( $file_real_path, $file_content_to );
}
return true;

}

/**
* ディレクトリを変換
* @throws ConvertException
* @return boolean
*/
private function _convertDirectory() {
if( ! file_exists($this->_path) ) {
$message = $this->_path . ' does not exist.’;
throw new ConvertException($message);
}
if( ! is_dir($this->_path) ) {
$message = $this->_path . ' ディレクトリではありません.’;
throw new ConvertException($message);
}
if( ! $this->_isWR() ) {
$message = $this->_path . ' must can be read and write.’;
throw new ConvertException($message);
}
$this->_scanDirFiles();
if( empty($this->_files) ) {
$message = $this->_path . ' 空のディレクトリである.’;
throw new ConvertException($message);
}
foreach( $this->_files as $value ) {
$file_content_from = file_get_contents( $value );
if( mb_check_encoding($file_content_from, $this->_from_encoding) ) {
$file_content_to = mb_convert_encoding( $file_content_from, $this->_to_encoding, $this->_from_encoding );
file_put_contents( $value, $file_content_to );
}
}
return true;
}

/**
*ファイルやディレクトリを読み書きできるかどうかを判断
* @return boolean 書き込みの場合true、それ以外falseを返す
*/
private function _isWR() {
if( is_readable($this->_path) && is_writable($this->_path) ) {
return true;
}
return false;
}

/**
* ディレクトリをトラバースして全てのファイルを見つける
* @return boolean
*/
private function _scanDirFiles($dir = ") {
$base_path = empty( $dir ) ? realpath($this->_path) . DIRECTORY_SEPARATOR : realpath($dir) . DIRECTORY_SEPARATOR;
$files_tmp = empty( $dir ) ? scandir($this->_path) : scandir($dir);
foreach( $files_tmp as $value ) {
if( $value == '.’ || $value == '..’ || ( strpos($value, '.’) === 0 ) ) {
continue;
}
$value = $base_path . $value;
if( is_dir($value) ) {
if( $this->_recursion ) {
$this->_scanDirFiles($value);
}
}
elseif( is_file($value) ) {
$this->_files[] = $value;
}
}
return true;
}
}

/**
* 変換例外処理
*
*/
class ConvertException extends Exception {

}

PHP

Posted by arkgame