-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add string helper && add SupportFunctions
- Loading branch information
Showing
18 changed files
with
290 additions
and
58 deletions.
There are no files selected for viewing
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
*.DS_Store | ||
/vendor | ||
/coverage | ||
sftp-config.json | ||
composer.lock | ||
.subsplit | ||
.php_cs.cache | ||
.idea | ||
/index.php | ||
/config.php | ||
./phpunit* |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
<?php | ||
|
@@ -240,4 +241,33 @@ class m170810_084615_create_post extends Migration | |
|
||
} | ||
``` | ||
…… | ||
|
||
### StringHelper | ||
|
||
```php | ||
String::after('@', '[email protected]'); // 'online.ge' | ||
|
||
String::afterLast('[', 'sin[90]*cos[180]');// '180]' | ||
|
||
String::before('@', '[email protected]'); // 'biohazard' | ||
|
||
String::beforeLast('[', 'sin[90]*cos[180]'); // 'sin[90]*cos' | ||
|
||
String::between('@', '.', '[email protected]'); // 'online' | ||
|
||
String::betweenLast('[', ']', 'sin[90]*cos[180]'); // '180' | ||
``` | ||
|
||
### Setup | ||
|
||
```php | ||
|
||
Setup::toFen(100); // 10000 | ||
Setup::toYuan(100); // 1 | ||
|
||
|
||
Setup::errorMessage($model->firstErrors); | ||
``` | ||
|
||
|
||
…… |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "[email protected]" | ||
} | ||
], | ||
"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": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"../../../vendor/yiisoft/yii2": "~2.0.0", | ||
"ext-bcmath": "*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"yiier\\helpers\\": "src" | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* author : forecho <[email protected]> | ||
* 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 ''; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
/** | ||
* author : forecho <[email protected]> | ||
* createTime : 2020/2/18 13:10 | ||
* description: | ||
*/ | ||
|
||
namespace yiier\helpers; | ||
|
||
class String | ||
{ | ||
/** | ||
* after('@', '[email protected]'); | ||
* 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('@', '[email protected]'); | ||
* 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('@', '.', '[email protected]'); | ||
* 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); | ||
} | ||
} | ||
} |
Oops, something went wrong.