src/Eccube/Util/StringUtil.php line 286

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Util;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. class StringUtil
  15. {
  16.     /**
  17.      * The MIT License (MIT)
  18.      *
  19.      * Copyright (c) <Taylor Otwell>
  20.      *
  21.      * Permission is hereby granted, free of charge, to any person obtaining a copy
  22.      * of this software and associated documentation files (the "Software"), to deal
  23.      * in the Software without restriction, including without limitation the rights
  24.      * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  25.      * copies of the Software, and to permit persons to whom the Software is
  26.      * furnished to do so, subject to the following conditions:
  27.      *
  28.      * The above copyright notice and this permission notice shall be included in
  29.      * all copies or substantial portions of the Software.
  30.      *
  31.      * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  32.      * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  33.      * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  34.      * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  35.      * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  36.      * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  37.      * THE SOFTWARE
  38.      *
  39.      * Generate a more truly "random" alpha-numeric string.
  40.      *
  41.      * @param  int $length
  42.      *
  43.      * @return string
  44.      *
  45.      * @throws \RuntimeException
  46.      */
  47.     public static function random($length 16)
  48.     {
  49.         if (function_exists('openssl_random_pseudo_bytes')) {
  50.             $bytes openssl_random_pseudo_bytes($length 2);
  51.             if ($bytes === false) {
  52.                 throw new \RuntimeException('Unable to generate random string.');
  53.             }
  54.             return substr(str_replace(['/''+''='], ''base64_encode($bytes)), 0$length);
  55.         }
  56.         return static::quickRandom($length);
  57.     }
  58.     /**
  59.      * The MIT License (MIT)
  60.      *
  61.      * Copyright (c) <Taylor Otwell>
  62.      *
  63.      * Permission is hereby granted, free of charge, to any person obtaining a copy
  64.      * of this software and associated documentation files (the "Software"), to deal
  65.      * in the Software without restriction, including without limitation the rights
  66.      * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  67.      * copies of the Software, and to permit persons to whom the Software is
  68.      * furnished to do so, subject to the following conditions:
  69.      *
  70.      * The above copyright notice and this permission notice shall be included in
  71.      * all copies or substantial portions of the Software.
  72.      *
  73.      * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  74.      * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  75.      * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  76.      * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  77.      * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  78.      * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  79.      * THE SOFTWARE
  80.      *
  81.      * Generate a "random" alpha-numeric string.
  82.      *
  83.      * Should not be considered sufficient for cryptography, etc.
  84.      *
  85.      * @param  int $length
  86.      *
  87.      * @return string
  88.      */
  89.     public static function quickRandom($length 16)
  90.     {
  91.         $pool '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  92.         return substr(str_shuffle(str_repeat($pool$length)), 0$length);
  93.     }
  94.     /**
  95.      * 改行コードの変換
  96.      *
  97.      * @param $value
  98.      * @param string $lf
  99.      *
  100.      * @return string
  101.      */
  102.     public static function convertLineFeed($value$lf "\n")
  103.     {
  104.         if (empty($value)) {
  105.             return '';
  106.         }
  107.         return strtr($valuearray_fill_keys(["\r\n""\r""\n"], $lf));
  108.     }
  109.     /**
  110.      * 文字コードの判定
  111.      *
  112.      * @param string $value
  113.      *
  114.      * @return string
  115.      */
  116.     public static function characterEncoding($value$encoding = ['UTF-8''SJIS''EUC-JP''ASCII''JIS''sjis-win'])
  117.     {
  118.         foreach ($encoding as $encode) {
  119.             if (mb_check_encoding($value$encode)) {
  120.                 return $encode;
  121.             }
  122.         }
  123.         return null;
  124.     }
  125.     /**
  126.      * 指定した文字列以上ある場合、「...」を付加する
  127.      * lengthに7を指定すると、「1234567890」は「1234567...」と「...」を付与して出力される
  128.      *
  129.      * @param string $value
  130.      * @param int $length
  131.      * @param string $end
  132.      *
  133.      * @return string
  134.      */
  135.     public static function ellipsis($value$length 100$end '...')
  136.     {
  137.         if (mb_strlen($value) <= $length) {
  138.             return $value;
  139.         }
  140.         return rtrim(mb_substr($value0$length'UTF-8')).$end;
  141.     }
  142.     /**
  143.      * 現在からの経過時間を書式化する.
  144.      *
  145.      * @param $date
  146.      *
  147.      * @return string
  148.      */
  149.     public static function timeAgo($date)
  150.     {
  151.         if (empty($date)) {
  152.             return '';
  153.         }
  154.         $now = new \DateTime();
  155.         if (!($date instanceof \DateTime)) {
  156.             $date = new \DateTime($date);
  157.         }
  158.         $diff $date->diff($nowtrue);
  159.         if ($diff->0) {
  160.             // return $date->format("Y/m/d H:i");
  161.             return $date->format('Y/m/d');
  162.         }
  163.         if ($diff->== || $diff->days 0) {
  164.             if ($diff->days <= 31) {
  165.                 return $diff->days.'日前';
  166.             }
  167.             // return $date->format("Y/m/d H:i");
  168.             return $date->format('Y/m/d');
  169.         }
  170.         if ($diff->0) {
  171.             return $diff->h.'時間前';
  172.         }
  173.         if ($diff->0) {
  174.             return $diff->i.'分前';
  175.         }
  176.         return $diff->s.'秒前';
  177.     }
  178.     /**
  179.      * 変数が空白かどうかをチェックする.
  180.      *
  181.      * 引数 $value が空白かどうかをチェックする. 空白の場合は true.
  182.      * 以下の文字は空白と判断する.
  183.      * - ' ' (ASCII 32 (0x20)), 通常の空白
  184.      * - "\t" (ASCII 9 (0x09)), タブ
  185.      * - "\n" (ASCII 10 (0x0A)), リターン
  186.      * - "\r" (ASCII 13 (0x0D)), 改行
  187.      * - "\0" (ASCII 0 (0x00)), NULバイト
  188.      * - "\x0B" (ASCII 11 (0x0B)), 垂直タブ
  189.      *
  190.      * 引数 $value がオブジェクト型、配列の場合は非推奨とし、 E_USER_DEPRECATED をスローする.
  191.      * EC-CUBE2系からの互換性、ビギナー層を配慮し、以下のような実装とする.
  192.      * 引数 $value が配列の場合は, 空の配列の場合 true を返す.
  193.      * 引数 $value が ArrayCollection::isEmpty() == true の場合 true を返す.
  194.      * 引数 $value が上記以外のオブジェクト型の場合は false を返す.
  195.      *
  196.      * 引数 $greedy が true の場合は, 全角スペース, ネストした空の配列も
  197.      * 空白と判断する.
  198.      *
  199.      * @param string $value チェック対象の変数. 文字型以外も使用できるが、非推奨.
  200.      * @param boolean $greedy '貧欲'にチェックを行う場合 true, デフォルト false
  201.      *
  202.      * @return boolean $value が空白と判断された場合 true
  203.      */
  204.     public static function isBlank($value$greedy false)
  205.     {
  206.         $deprecated '\Eccube\Util\StringUtil::isBlank() の第一引数は文字型、数値を使用してください';
  207.         // テストカバレッジを上げるために return の前で trigger_error をスローしている
  208.         if (is_object($value)) {
  209.             if ($value instanceof ArrayCollection) {
  210.                 if ($value->isEmpty()) {
  211.                     @trigger_error($deprecatedE_USER_DEPRECATED);
  212.                     return true;
  213.                 } else {
  214.                     @trigger_error($deprecatedE_USER_DEPRECATED);
  215.                     return false;
  216.                 }
  217.             }
  218.             @trigger_error($deprecatedE_USER_DEPRECATED);
  219.             return false;
  220.         }
  221.         if (is_array($value)) {
  222.             if ($greedy) {
  223.                 if (empty($value)) {
  224.                     @trigger_error($deprecatedE_USER_DEPRECATED);
  225.                     return true;
  226.                 }
  227.                 $array_result true;
  228.                 foreach ($value as $in) {
  229.                     $array_result self::isBlank($in$greedy);
  230.                     if (!$array_result) {
  231.                         @trigger_error($deprecatedE_USER_DEPRECATED);
  232.                         return false;
  233.                     }
  234.                 }
  235.                 @trigger_error($deprecatedE_USER_DEPRECATED);
  236.                 return $array_result;
  237.             } else {
  238.                 @trigger_error($deprecatedE_USER_DEPRECATED);
  239.                 return empty($value);
  240.             }
  241.         }
  242.         if ($greedy) {
  243.             $value preg_replace('/ /'''$value);
  244.         }
  245.         $value trim($value);
  246.         if (strlen($value) > 0) {
  247.             return false;
  248.         }
  249.         return true;
  250.     }
  251.     /**
  252.      * @param $value
  253.      *
  254.      * @return bool
  255.      */
  256.     public static function isNotBlank($value$greedy false)
  257.     {
  258.         return !self::isBlank($value$greedy);
  259.     }
  260.     /**
  261.      * 両端にある全角スペース、半角スペースを取り除く
  262.      *
  263.      * @param $value
  264.      *
  265.      * @return string
  266.      */
  267.     public static function trimAll($value)
  268.     {
  269.         if ($value === '') {
  270.             return '';
  271.         }
  272.         if ($value === 0) {
  273.             return 0;
  274.         }
  275.         if ($value == null) {
  276.             return null;
  277.         }
  278.         return preg_replace('/(^\s+)|(\s+$)/u'''$value);
  279.     }
  280.     /**
  281.      * envファイルのコンテンツを更新または追加する.
  282.      *
  283.      * @param string $env
  284.      * @param array $replacement
  285.      *
  286.      * @return string
  287.      */
  288.     public static function replaceOrAddEnv($env, array $replacement)
  289.     {
  290.         foreach ($replacement as $key => $value) {
  291.             $pattern '/^('.$key.')=(.*)/m';
  292.             if (preg_match($pattern$env)) {
  293.                 $env preg_replace($pattern'$1='.$value$env);
  294.                 if ('\\' === DIRECTORY_SEPARATOR) {
  295.                     // The m modifier of the preg functions converts the end-of-line to '\n'
  296.                     $env self::convertLineFeed($env"\r\n");
  297.                 }
  298.             } else {
  299.                 $env .= PHP_EOL."${key}=${value}";
  300.             }
  301.         }
  302.         return $env;
  303.     }
  304. }