app/Plugin/YamatoPayment42/Util/CommonUtil.php line 233

Open in your IDE?
  1. <?php
  2. namespace Plugin\YamatoPayment42\Util;
  3. use Eccube\Util\StringUtil;
  4. class CommonUtil
  5. {
  6.     /**
  7.      * unSerializeした配列を返す
  8.      *
  9.      * @param string $data
  10.      * @return mixed
  11.      */
  12.     public static function unSerializeData($data)
  13.     {
  14.         return unserialize($data);
  15.     }
  16.     /**
  17.      * serializeした文字列を返す
  18.      *
  19.      * @param array $data
  20.      * @return string
  21.      */
  22.     public static function serializeData($data)
  23.     {
  24.         if(is_array($data) == false) {
  25.             $data = [$data];
  26.         }
  27.         return serialize($data);
  28.     }
  29.     public static function getArrayToHash($array$useKeyName = [])
  30.     {
  31.         if(is_array($array) == false) {
  32.             $array[] = $array;
  33.         }
  34.         ksort($array);
  35.         $hashBaseStr '';
  36.         foreach($array as $key => $val) {
  37.             if(empty($useKeyName) || in_array($key$useKeyName)) {
  38.                 $hashBaseStr .= $key.":".$val.",";
  39.             }
  40.         }
  41.         if($hashBaseStr) {
  42.             return hash("sha256"$hashBaseStr);
  43.         }
  44.         return '';
  45.     }
  46.     /**
  47.      * 連想配列をエンコードする
  48.      *
  49.      * @param array $param
  50.      * @param string $encod default:UTF-8
  51.      * @return array
  52.      */
  53.     public static function paramConvertEncoding(array $param$encod 'UTF-8')
  54.     {
  55.         if(is_array($param) == false) {
  56.             $param = [$param];
  57.         }
  58.         $return = [];
  59.         foreach ($param as $key => $value) {
  60.             // UTF-8以外は文字コード変換を行う
  61.             $detectEncode StringUtil::characterEncoding(substr($value06));
  62.             $return[$key] = ($detectEncode != $encod) ? mb_convert_encoding($value$encod$detectEncode) : $value;
  63.         }
  64.         return $return;
  65.     }
  66.     /**
  67.      * エンコードチェック
  68.      * $char_code > SJIS-win > $char_code で欠落のないことを確認する
  69.      *
  70.      * @param array $listData
  71.      * @param string $char_code 文字コード
  72.      * @return array $listData
  73.      */
  74.     public static function checkEncode(array $listData$char_code 'UTF-8')
  75.     {
  76.         foreach ($listData as $key => $val) {
  77.             //未設定、配列、単語空白以外の場合はスキップ
  78.             if (!$val || is_array($val) || preg_match('/^[\w\s]+$/i'$val)) {
  79.                 continue;
  80.             }
  81.             //CHAR_CODE > SJIS-WIN > CHAR_CODEで欠落のないことを確認
  82.             $temp mb_convert_encoding($val'SJIS-win'$char_code);
  83.             $temp mb_convert_encoding($temp$char_code'SJIS-win');
  84.             if ($val !== $temp) {
  85.                 $temp mb_convert_encoding($val$char_code'SJIS-win');
  86.                 $temp mb_convert_encoding($temp'SJIS-win'$char_code);
  87.                 if ($val === $temp) {
  88.                     $listData[$key] = mb_convert_encoding($val$char_code'SJIS-win');
  89.                 } else {
  90.                     $listData[$key] = 'unknown encoding strings';
  91.                 }
  92.             }
  93.         }
  94.         return $listData;
  95.     }
  96.     /**
  97.      * 禁止文字を全角スペースに置換する。
  98.      *
  99.      * @param string $value 対象文字列
  100.      * @param string $encoding
  101.      * @return string 結果
  102.      */
  103.     public static function convertProhibitedChar($value$encoding 'utf-8')
  104.     {
  105.         $ret $value;
  106.         for ($i 0$i mb_strlen($value); $i++) {
  107.             $tmp mb_substr($value$i1$encoding);
  108.             if (self::isProhibitedChar($tmp)) {
  109.                 $ret str_replace($tmp" "$ret);
  110.             }
  111.         }
  112.         return $ret;
  113.     }
  114.     /**
  115.      * 禁止文字か判定を行う。
  116.      *
  117.      * @param string $value 判定対象
  118.      * @return boolean 禁止文字の場合、true
  119.      */
  120.     public static function isProhibitedChar($value)
  121.     {
  122.         $check_char mb_convert_encoding($value"SJIS-win""UTF-8");
  123.         $listProhibited = ['815C''8160''8161''817C''8191''8192''81CA'];
  124.         foreach ($listProhibited as $prohibited) {
  125.             if (hexdec($prohibited) == hexdec(bin2hex($check_char))) {
  126.                 return true;
  127.             }
  128.         }
  129.         if (hexdec('8740') <= hexdec(bin2hex($check_char)) && hexdec('879E') >= hexdec(bin2hex($check_char))) {
  130.             return true;
  131.         }
  132.         if ((hexdec('ED40') <= hexdec(bin2hex($check_char)) && hexdec('ED9E') >= hexdec(bin2hex($check_char)))
  133.          || (hexdec('ED9F') <= hexdec(bin2hex($check_char)) && hexdec('EDFC') >= hexdec(bin2hex($check_char)))
  134.          || (hexdec('EE40') <= hexdec(bin2hex($check_char)) && hexdec('EE9E') >= hexdec(bin2hex($check_char)))
  135.          || (hexdec('FA40') <= hexdec(bin2hex($check_char)) && hexdec('FA9E') >= hexdec(bin2hex($check_char)))
  136.          || (hexdec('FA9F') <= hexdec(bin2hex($check_char)) && hexdec('FAFC') >= hexdec(bin2hex($check_char)))
  137.          || (hexdec('FB40') <= hexdec(bin2hex($check_char)) && hexdec('FB9E') >= hexdec(bin2hex($check_char)))
  138.          || (hexdec('FB9F') <= hexdec(bin2hex($check_char)) && hexdec('FBFC') >= hexdec(bin2hex($check_char)))
  139.          || (hexdec('FC40') <= hexdec(bin2hex($check_char)) && hexdec('FC4B') >= hexdec(bin2hex($check_char)))
  140.         ) {
  141.             return true;
  142.         }
  143.         if ((hexdec('EE9F') <= hexdec(bin2hex($check_char)) && hexdec('EEFC') >= hexdec(bin2hex($check_char)))
  144.          || (hexdec('F040') <= hexdec(bin2hex($check_char)) && hexdec('F9FC') >= hexdec(bin2hex($check_char)))
  145.         ) {
  146.             return true;
  147.         }
  148.         return false;
  149.     }
  150.     /**
  151.      * 禁止半角記号を半角スペースに変換する。
  152.      *
  153.      * @param string $value
  154.      * @return string 変換した値
  155.      */
  156.     public static function convertProhibitedKigo($value$rep " ")
  157.     {
  158.         $listProhibitedKigo = [
  159.             '!''"''$''%''&''\'''('')''+'',''.'';''=''?''[''\\'']''^''_''`''{''|''}''~',
  160.         ];
  161.         foreach ($listProhibitedKigo as $prohibitedKigo) {
  162.             if (strstr($value$prohibitedKigo)) {
  163.                 $value str_replace($prohibitedKigo$rep$value);
  164.             }
  165.         }
  166.         return $value;
  167.     }
  168.     /**
  169.      * 文字列から指定バイト数を切り出す。
  170.      *
  171.      * @param string $value
  172.      * @param integer $len
  173.      * @return string 結果
  174.      */
  175.     public static function subString($value$len)
  176.     {
  177.         $ret '';
  178.         $value mb_convert_encoding($value"SJIS-win""UTF-8");
  179.         for ($i 1$i <= mb_strlen($value); $i++) {
  180.             $tmp mb_substr($value0$i"SJIS-win");
  181.             if (strlen($tmp) <= $len) {
  182.                 $ret mb_convert_encoding($tmp"UTF-8""SJIS-win");
  183.             } else {
  184.                 break;
  185.             }
  186.         }
  187.         return $ret;
  188.     }
  189.     /**
  190.      * 端末区分を取得する
  191.      *
  192.      *  1:スマートフォン
  193.      *  2:PC
  194.      *  3:携帯電話
  195.      *
  196.      * @return integer 端末区分
  197.      */
  198.     public static function getDeviceDivision()
  199.     {
  200.         $result 2;
  201. //         if (self::isSmartPhone()) {
  202. //             $result = 1;
  203. //         }
  204.         return $result;
  205.     }
  206.     /**
  207.      * 日付(YYYYMMDD)をフォーマットして返す
  208.      *
  209.      * @param string $format
  210.      * @param integer $number 日付(YYYYMMDD)
  211.      * @return string フォーマット後の日付
  212.      */
  213.     public static function getDateFromNumber($format 'Ymd'$number)
  214.     {
  215.         $number = (string)$number;
  216.         $shortFlag = (strlen($number) < 9) ? true false;
  217.         $year substr($number04);
  218.         $month substr($number42);
  219.         $day substr($number62);
  220.         $hour = ($shortFlag) ? '0' substr($number82);
  221.         $minute = ($shortFlag) ? '0' substr($number102);
  222.         $second = ($shortFlag) ? '0' substr($number122);
  223.         return (checkdate($month$day$year))? date($formatmktime($hour$minute$second$month$day$year)) : date($format);
  224.     }
  225.     public static function printLog($message$data = [])
  226.     {
  227.         if(is_array($message)) {
  228.             $message print_r($message,true);
  229.         }
  230.         if($data) {
  231.             $message .= ($message "\n" "") . print_r($datatrue);
  232.         }
  233.         logs('YamatoPayment42')->info($message);
  234.     }
  235.     /**
  236.      * 送り先区分を取得する
  237.      *
  238.      * @param Order $Order 受注データ
  239.      * @param $plugin_setting プラグイン設定 0: 同梱しない、1: 同梱する
  240.      * @return int $sendDiv 請求書の送り先 0:自分送り、1:自分以外、2:商品に同梱
  241.      */
  242.     public static function decideSendDiv($Order$plugin_setting)
  243.     {
  244.         if ($Order->isMultiple()) {
  245.             return 1;
  246.         }
  247.         if (self::isGift($Order)) {
  248.             return 1;
  249.         }
  250.         if ($plugin_setting === 0) {
  251.             return 0;
  252.         } 
  253.         return 2;
  254.     }
  255.     /**
  256.      * 注文者住所と配送先住所・氏名が同一か判定する
  257.      */
  258.     public static function isGift($Order) {
  259.         $Shippings $Order->getShippings();
  260.         // 複数配送は判定対象外
  261.         if ($Order->isMultiple()) {
  262.             return false;
  263.         }
  264.         if (
  265.             $Order->getName01() != $Shippings[0]['name01']
  266.             || $Order->getName02() != $Shippings[0]['name02']
  267.             || $Order->getKana01() != $Shippings[0]['kana01']
  268.             || $Order->getKana02() != $Shippings[0]['kana02']
  269.             || $Order->getPhoneNumber() != $Shippings[0]['phone_number']
  270.             || $Order->getPostalCode() != $Shippings[0]['postal_code']
  271.             || $Order->getPref() != $Shippings[0]['pref']
  272.             || $Order->getAddr01() != $Shippings[0]['addr01']
  273.             || $Order->getAddr02() != $Shippings[0]['addr02']
  274.         ) {
  275.             return true;
  276.         } else {
  277.             return false;
  278.         } 
  279.     }
  280.     /**
  281.      * 半角→全角変換
  282.      *
  283.      * @param string $data
  284.      * @param string $encoding
  285.      * @return string
  286.      */
  287.     public static function convHalfToFull($data$encoding 'utf-8')
  288.     {
  289.         $data str_replace('"','”',$data);
  290.         $data str_replace("'","’",$data);
  291.         $data str_replace("\\","¥",$data);
  292.         $data str_replace("~","~",$data);
  293.         $data mb_convert_kana($data'KVAS'$encoding);
  294.         return $data;
  295.     }
  296.     /**
  297.      * 11桁の数列をハイフン区切りの表示用文字列にパースする
  298.      * 
  299.      * @param int $number
  300.      * @return string $dispPhoneNumber
  301.      */
  302.     public static function parsePhoneNumber($number)
  303.     {
  304.         // 電話番号を表示用文字列にパース
  305.         $splitPhoneNumber01 substr($number03);
  306.         $splitPhoneNumber02 substr($number34);
  307.         $splitPhoneNumber03 substr($number74);
  308.         $dispPhoneNumber $splitPhoneNumber01 '-' $splitPhoneNumber02 '-' $splitPhoneNumber03
  309.         
  310.         return $dispPhoneNumber;
  311.     }
  312. }