Skip to content

Commit

Permalink
feat: add string helper && add SupportFunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
forecho committed Jul 17, 2020
1 parent fff800b commit d55f15d
Show file tree
Hide file tree
Showing 18 changed files with 290 additions and 58 deletions.
Empty file added .editorconfig
Empty file.
11 changes: 11 additions & 0 deletions .gitignore
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*
48 changes: 39 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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"
]
},
```
Expand All @@ -69,7 +70,7 @@ then run
$ composer dump
```

**SearchModel**
### SearchModel

示例一

Expand Down Expand Up @@ -103,7 +104,7 @@ return $this->render('index', [
```


**FileTarget**
### FileTarget

Can achieve results:`@app/runtime/logs/error/20151223_app.log`

Expand Down Expand Up @@ -193,7 +194,7 @@ change config file, main.php
```


**ResponseHandler**
### ResponseHandler

RESTful Response Handler, change config file `main.php`:

Expand All @@ -211,7 +212,7 @@ RESTful Response Handler, change config file `main.php`:
]
```

**Migration**
### Migration

```php
<?php
Expand Down Expand Up @@ -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);
```


……
31 changes: 0 additions & 31 deletions Setup.php

This file was deleted.

41 changes: 23 additions & 18 deletions composer.json
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.
23 changes: 23 additions & 0 deletions GlobalFunctions.php → src/GlobalFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* description:
*/

use yii\helpers\VarDumper;
use yii\web\Response;

if (!function_exists('app')) {
Expand Down Expand Up @@ -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);
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
54 changes: 54 additions & 0 deletions src/Setup.php
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 '';
}
}
110 changes: 110 additions & 0 deletions src/String.php
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);
}
}
}
Loading

0 comments on commit d55f15d

Please sign in to comment.