diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..e69de29 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6efb01e --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +*.DS_Store +/vendor +/coverage +sftp-config.json +composer.lock +.subsplit +.php_cs.cache +.idea +/index.php +/config.php +./phpunit* \ No newline at end of file diff --git a/README.md b/README.md index 21df05a..91562cb 100644 --- a/README.md +++ b/README.md @@ -31,14 +31,14 @@ to the require section of your `composer.json` file. Method Listing ----- -**arrayShift** +### arrayShift ```php ArrayHelper::arrayShift([0 => 'a', 2 => 'c', 1 => 'b']); // [2 => 'c', 1 => 'b'] ``` -**saveAll** +### saveAll ```php $rows = []; @@ -51,14 +51,15 @@ if (!ModelHelper::saveAll(Post::tableName(), $rows)) { } ``` -**Global Functions** +### Global Functions change `composer.json` file, add this: ``` "autoload": { "files": [ - "vendor/yiier/yii2-helpers/GlobalFunctions.php" + "vendor/yiier/yii2-helpers/GlobalFunctions.php", + "vendor/yiier/yii2-helpers/SupportFunctions.php" ] }, ``` @@ -69,7 +70,7 @@ then run $ composer dump ``` -**SearchModel** +### SearchModel 示例一 @@ -103,7 +104,7 @@ return $this->render('index', [ ``` -**FileTarget** +### FileTarget Can achieve results:`@app/runtime/logs/error/20151223_app.log` @@ -193,7 +194,7 @@ change config file, main.php ``` -**ResponseHandler** +### ResponseHandler RESTful Response Handler, change config file `main.php`: @@ -211,7 +212,7 @@ RESTful Response Handler, change config file `main.php`: ] ``` -**Migration** +### Migration ```php firstErrors); +``` + + +…… \ No newline at end of file diff --git a/Setup.php b/Setup.php deleted file mode 100644 index 7ac0ce6..0000000 --- a/Setup.php +++ /dev/null @@ -1,31 +0,0 @@ - - * createTime : 2016/1/11 14:47 - * description: - */ - -namespace yiier\helpers; - -class Setup -{ - /** - * 人民币元转换成分 - * @param $data - * @return mixed - */ - public static function toFen($data) - { - return bcmul((float)$data, 100); - } - - /** - * 分转换成人民币元(保留两位小数) - * @param $data integer - * @return float - */ - public static function toYuan($data) - { - return bcdiv($data, 100, 2); - } -} diff --git a/composer.json b/composer.json index 41827c0..8514245 100644 --- a/composer.json +++ b/composer.json @@ -1,21 +1,26 @@ { - "name": "yiier/yii2-helpers", - "description": "Helpers for Yii2", - "type": "yii2-extension", - "keywords": ["yii2","extension","helpers"], - "license": "BSD-4-Clause", - "authors": [ - { - "name": "forecho", - "email": "caizhenghai@gmail.com" - } - ], - "require": { - "yiisoft/yii2": "~2.0.0" - }, - "autoload": { - "psr-4": { - "yiier\\helpers\\": "" - } + "name": "yiier/yii2-helpers", + "description": "Helpers for Yii2", + "type": "yii2-extension", + "keywords": [ + "yii2", + "extension", + "helpers" + ], + "license": "BSD-4-Clause", + "authors": [ + { + "name": "forecho", + "email": "caizhenghai@gmail.com" } + ], + "require": { + "../../../vendor/yiisoft/yii2": "~2.0.0", + "ext-bcmath": "*" + }, + "autoload": { + "psr-4": { + "yiier\\helpers\\": "src" + } + } } diff --git a/ArrayHelper.php b/src/ArrayHelper.php similarity index 100% rename from ArrayHelper.php rename to src/ArrayHelper.php diff --git a/DateHelper.php b/src/DateHelper.php similarity index 100% rename from DateHelper.php rename to src/DateHelper.php diff --git a/FileTarget.php b/src/FileTarget.php similarity index 100% rename from FileTarget.php rename to src/FileTarget.php diff --git a/GlobalFunctions.php b/src/GlobalFunctions.php similarity index 92% rename from GlobalFunctions.php rename to src/GlobalFunctions.php index e404ba3..b7095f2 100644 --- a/GlobalFunctions.php +++ b/src/GlobalFunctions.php @@ -6,6 +6,7 @@ * description: */ +use yii\helpers\VarDumper; use yii\web\Response; if (!function_exists('app')) { @@ -161,3 +162,25 @@ function pr($message, $debug = true) } } } + +if (!function_exists('dump')) { + /** + * @param mixed $var + */ + function dump($var) + { + VarDumper::dump($var); + } +} + + +if (!function_exists('dd')) { + /** + * @param mixed $var + */ + function dd($var) + { + VarDumper::dump($var); + die(1); + } +} diff --git a/MailHelper.php b/src/MailHelper.php similarity index 100% rename from MailHelper.php rename to src/MailHelper.php diff --git a/Migration.php b/src/Migration.php similarity index 100% rename from Migration.php rename to src/Migration.php diff --git a/ModelHelper.php b/src/ModelHelper.php similarity index 100% rename from ModelHelper.php rename to src/ModelHelper.php diff --git a/ResponseHandler.php b/src/ResponseHandler.php similarity index 100% rename from ResponseHandler.php rename to src/ResponseHandler.php diff --git a/SearchModel.php b/src/SearchModel.php similarity index 100% rename from SearchModel.php rename to src/SearchModel.php diff --git a/Security.php b/src/Security.php similarity index 100% rename from Security.php rename to src/Security.php diff --git a/src/Setup.php b/src/Setup.php new file mode 100644 index 0000000..01035d6 --- /dev/null +++ b/src/Setup.php @@ -0,0 +1,54 @@ + + * createTime : 2016/1/11 14:47 + * description: + */ + +namespace yiier\helpers; + +class Setup +{ + /** + * 分转换成人民币元(保留两位小数) + * @param $data integer + * @return float + */ + public static function toYuan($data) + { + if (in_array($data, [null, 0])) { + return $data; + } + + return bcdiv($data, 100, 2); + } + + + /** + * 人民币元转换成分 + * @param $data integer + * @return float + */ + public static function toFen($data) + { + if (in_array($data, [null, 0])) { + return $data; + } + + return (int)bcmul((float)$data, 100); + } + + + /** + * 错误信息 + * @param array $error + * @return string + */ + public static function errorMessage($error) + { + if (is_array($error)) { + return array_values($error)[0]; + } + return ''; + } +} diff --git a/src/String.php b/src/String.php new file mode 100644 index 0000000..c6b6fa7 --- /dev/null +++ b/src/String.php @@ -0,0 +1,110 @@ + + * createTime : 2020/2/18 13:10 + * description: + */ + +namespace yiier\helpers; + +class String +{ + /** + * after('@', 'biohazard@online.ge'); + * returns 'online.ge' + * @param string $str + * @param string $text + * @return false|string + */ + public static function after(string $str, string $text) + { + if (!is_bool(strpos($text, $str))) { + return substr($text, strpos($text, $str) + strlen($str)); + } + return $text; + } + + /** + * afterLast('[', 'sin[90]*cos[180]'); + * returns '180]' + * @param string $str + * @param string $text + * @return false|string + */ + public static function afterLast(string $str, string $text) + { + if (!is_bool(self::strrevpos($text, $str))) { + return substr($text, self::strrevpos($text, $str) + strlen($str)); + } + } + + /** + * before('@', 'biohazard@online.ge'); + * returns 'biohazard' + * @param string $str + * @param string $text + * @return false|string + */ + public static function before(string $str, string $text) + { + if (strpos($text, $str) === false) { + return $text; + } + return substr($text, 0, strpos($text, $str)); + } + + /** + * beforeLast('[', 'sin[90]*cos[180]'); + * returns 'sin[90]*cos' + * @param string $str + * @param string $text + * @return false|string + */ + public static function beforeLast(string $str, string $text) + { + return substr($text, 0, self::strrevpos($text, $str)); + } + + /** + * between('@', '.', 'biohazard@online.ge'); + * returns 'online' + * @param string $str1 + * @param string $str2 + * @param string $text + * @return false|string + */ + public static function between(string $str1, string $str2, string $text) + { + return self::before($str2, self::after($str1, $text)); + } + + /** + * betweenLast('[', ']', 'sin[90]*cos[180]'); + * returns '180' + * @param string $str1 + * @param string $str2 + * @param string $text + * @return false|string + */ + public static function betweenLast(string $str1, string $str2, string $text) + { + return self::afterLast($str1, self::beforeLast($str2, $text)); + } + + + /** + * use strrevpos function in case your php version does not include it + * @param string $text + * @param string $str + * @return bool|false|int + */ + protected static function strrevpos(string $text, string $str) + { + $revPos = strpos(strrev($text), strrev($str)); + if ($revPos === false) { + return false; + } else { + return strlen($text) - $revPos - strlen($str); + } + } +} diff --git a/src/SupportFunctions.php b/src/SupportFunctions.php new file mode 100644 index 0000000..de6f3e6 --- /dev/null +++ b/src/SupportFunctions.php @@ -0,0 +1,30 @@ +