Created
October 11, 2012 01:34
-
-
Save vexus2/3869608 to your computer and use it in GitHub Desktop.
[PHP]ISO8601形式日付などのフォーマットを行う汎用クラス
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * フォーマット変換ユーティリティクラス | |
| */ | |
| class CommonFormatter | |
| { | |
| /** | |
| * 日付のフォーマット配列 | |
| */ | |
| private static $DATE_FORMAT_ARRAY = array( | |
| CommonValidator::DATE_FORMAT_YMD => self::JPN_DATE_FORMAT_YMD, | |
| CommonValidator::DATE_FORMAT_YMDHIS => self::JPN_DATE_FORMAT_YMDHIS, | |
| CommonValidator::DATE_FORMAT_YMD_H_I_S => self::JPN_DATE_FORMAT_YMDHIS, | |
| CommonValidator::DATE_FORMAT_Y_M_D => self::JPN_DATE_FORMAT_YMD, | |
| CommonValidator::DATE_FORMAT_Y_M_D_H_I_S => self::JPN_DATE_FORMAT_YMDHIS | |
| ); | |
| /** | |
| * 日付のフォーマット | |
| */ | |
| const JPN_DATE_FORMAT_YMD = '%Y年%m月%d日'; | |
| const JPN_DATE_FORMAT_YMDHIS = '%Y年%m月%d日 %H時%M分%S秒'; | |
| /** | |
| * パラメータ文字列への変換処理<br> | |
| * 指定された配列をparam1=value¶m2=valueの形に変換し、返却する<br> | |
| * 変換する際、記号はURLエンコードする | |
| * | |
| * @param array $array パラメータ文字列に変換する配列 | |
| * @throws CommonException 指定された変換対象の配列が配列ではなかった場合 | |
| * @throws CommonException パラメータ文字列への変換処理が失敗した場合 | |
| * @return string パラメータ文字列 | |
| */ | |
| public static function toParamString($array) | |
| { | |
| if (is_array($array)) { | |
| $string = ''; | |
| $first = true; | |
| foreach ($array as $key => $value) { | |
| if (CommonValidator::checkString($key) && CommonValidator::checkString($value)) { | |
| if (!$first) { | |
| $string .= '&'; | |
| } | |
| $string .= rawurlencode($key); | |
| $string .= '='; | |
| $string .= rawurlencode($value); | |
| if ($first) { | |
| $first = false; | |
| } | |
| } else { | |
| throw new CommonException(ResultCode::ERR_PARAMETER, ReasonCode::CODE_003, MessageConstants::COMMON_FORMATTER_TO_PARAM_STRING_ERROR); | |
| } | |
| } | |
| return $string; | |
| } else { | |
| throw new CommonException(ResultCode::ERR_PARAMETER, ReasonCode::CODE_003, MessageConstants::COMMON_FORMATTER_TO_PARAM_STRING_ERROR); | |
| } | |
| } | |
| /** | |
| * XMLへの変換処理<br> | |
| * 指定された配列をXMLに変換し、返却する<br> | |
| * 配列以外にも引数に指定された場合、それぞれに応じてXML出力内容を変更する | |
| * | |
| * @param array $array XML形式に変換する配列 | |
| * @param string $default_tag デフォルトのタグ名 | |
| * @param string $rootName ルートのタグ名 | |
| * @param string $resultCode リザルトコード | |
| * @param string $reasonCode リーズンコード | |
| * @throws CommonException 指定された変換対象の配列が配列ではなかった場合 | |
| * @throws CommonException XMLへの変換処理が失敗した場合 | |
| * @return string xml形式の文字列 | |
| */ | |
| public static function toXml($array = array(), $default_tag = 'none', $rootName = ApplicationConstants::COMMON_XML_ROOT_NAME, $resultCode = ResultCode::SUCCESS, $reasonCode = ReasonCode::SUCCESS) | |
| { | |
| if (is_array($array)) { | |
| if (is_null($default_tag)) { | |
| $default_tag = 'none'; | |
| } | |
| if (is_null($rootName)) { | |
| $rootName = ApplicationConstants::COMMON_XML_ROOT_NAME; | |
| } | |
| $option = array( | |
| "linebreak" => '', | |
| "addDecl" => true, | |
| "encoding" => ApplicationConstants::COMMON_XML_ENCODING, | |
| "defaultTagName" => $default_tag, | |
| "rootName" => $rootName | |
| ); | |
| // 共通情報を付加 | |
| $outArray = array_merge(self::_getCodeArray($resultCode, $reasonCode), $array); | |
| $serializer = new XML_Serializer($option); | |
| $result = $serializer->serialize($outArray); | |
| if ($result) { | |
| $xml = $serializer->getSerializedData(); | |
| return $xml; | |
| } else { | |
| throw new CommonException(ResultCode::ERR_PARAMETER, ReasonCode::CODE_003, MessageConstants::COMMON_FORMATTER_TO_XML_ERROR); | |
| } | |
| } else { | |
| throw new CommonException(ResultCode::ERR_PARAMETER, ReasonCode::CODE_003, MessageConstants::COMMON_FORMATTER_TO_XML_ERROR); | |
| } | |
| } | |
| /** | |
| * ISO8601形式日付の整形処理<br> | |
| * ISO8601形式の日付を整形し、返却する | |
| * | |
| * @param string $timeInterval ISO8601形式の日付 | |
| * @throws CommonException 日付の整形処理が失敗した場合 | |
| * @return string 整形された日付 | |
| */ | |
| public static function toFormatDate($timeInterval) | |
| { | |
| try { | |
| $formatDate = null; | |
| $di = new DateInterval($timeInterval); | |
| // 年 | |
| $year = $di->format('%y'); | |
| if ($year != 0) { | |
| $formatDate .= $year . '年'; | |
| } | |
| // 月 | |
| $month = $di->format('%m'); | |
| if ($month != 0) { | |
| $formatDate .= $month . 'ヶ月'; | |
| } | |
| // 日 | |
| $day = $di->format('%d'); | |
| if ($day != 0) { | |
| $formatDate .= $day . '日'; | |
| } | |
| // 時 | |
| $hour = $di->format('%h'); | |
| if ($hour != 0) { | |
| $formatDate .= $hour . '時間'; | |
| } | |
| // 分 | |
| $minute = $di->format('%i'); | |
| if ($minute != 0) { | |
| $formatDate .= $minute . '分'; | |
| } | |
| // 秒 | |
| $second = $di->format('%s'); | |
| if ($second != 0) { | |
| $formatDate .= $second . '秒'; | |
| } | |
| // 日付が設定されない場合はエラー | |
| if (is_null($formatDate)) { | |
| throw new CommonException(ResultCode::ERR_PARAMETER, ReasonCode::CODE_001, MessageConstants::COMMON_FORMATTER_TO_FORMAT_DATE_ERROR); | |
| } | |
| return $formatDate; | |
| } catch (Exception $e) { | |
| throw new CommonException(ResultCode::ERR_PARAMETER, ReasonCode::CODE_001, MessageConstants::COMMON_FORMATTER_TO_FORMAT_DATE_ERROR); | |
| } | |
| } | |
| /** | |
| * 日付のフォーマットを行う | |
| * | |
| * @param string(日付) $arg 日付チェック済みの文字列 | |
| * @param string $format 引数のフォーマット | |
| * @return unknown|string 対象のフォーマットであれば、フォーマット済みの文字列 | |
| */ | |
| public static function transformDate($arg, $format = 'Y-m-d') | |
| { | |
| if (empty($arg)) { | |
| return $arg; | |
| } | |
| // フォーマット取得 | |
| if(isset(self::$DATE_FORMAT_ARRAY[$format])){ | |
| $tmpFormat = self::$DATE_FORMAT_ARRAY[$format]; | |
| if(!is_null($tmpFormat)){ | |
| $format = $tmpFormat; | |
| } | |
| }else{ | |
| // 不正なフォーマットの場合は何もしない | |
| return $arg; | |
| } | |
| // フォーマットした値を返却 | |
| $date = new CommonDate($arg); | |
| if($date){ | |
| return $date->format($format); | |
| } else { | |
| return $arg; | |
| } | |
| } | |
| /** | |
| * 結果、理由コード配列の取得処理 | |
| * 指定された結果、理由コードを元に結果、理由コード配列を生成し、返却する | |
| * | |
| * @param string $resultCode 結果コード | |
| * @param string $reasonCode 理由コード | |
| * @return array 結果コード、理由コードを格納した連想配列 | |
| */ | |
| private static function _getCodeArray($resultCode, $reasonCode) | |
| { | |
| return array( | |
| 'resultCode' => $resultCode, | |
| 'reasonCode' => $reasonCode, | |
| 'processDateTime'=>date(CommonValidator::DATE_FORMAT_Y_M_D_H_I_S)); | |
| } | |
| /** | |
| * ISO8601形式の日付を現在時刻に加算し、返却する | |
| * | |
| * @param string $timeInterval ISO8601形式の日付 | |
| * @throws CommonException 日付の整形処理が失敗した場合 | |
| * @return string 現在時刻にInterval値を加算した日付 | |
| */ | |
| public static function addIntervalDate($timeInterval) | |
| { | |
| try { | |
| $formatDate = new Zend_Date(); | |
| $di = new DateInterval($timeInterval); | |
| // 年 | |
| $year = $di->format('%y'); | |
| if ($year != 0) { | |
| $formatDate = $formatDate->addYear($year); | |
| } | |
| // 月 | |
| $month = $di->format('%m'); | |
| if ($month != 0) { | |
| $formatDate = $formatDate->addMonth($month); | |
| } | |
| // 日 | |
| $day = $di->format('%d'); | |
| if ($day != 0) { | |
| $formatDate = $formatDate->addDay($day); | |
| } | |
| // 時 | |
| $hour = $di->format('%h'); | |
| if ($hour != 0) { | |
| $formatDate = $formatDate->addHour($hour); | |
| } | |
| // 分 | |
| $minute = $di->format('%i'); | |
| if ($minute != 0) { | |
| $formatDate = $formatDate->addMinute($minute); | |
| } | |
| // 秒 | |
| $second = $di->format('%s'); | |
| if ($second != 0) { | |
| $formatDate = $formatDate->addSecond($second); | |
| } | |
| return $formatDate->get('y-MM-dd HH:mm:ss'); | |
| } catch (Exception $e) { | |
| throw new CommonException(ResultCode::ERR_PARAMETER, ReasonCode::CODE_001, MessageConstants::COMMON_FORMATTER_TO_FORMAT_DATE_ERROR); | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment