Skip to content

Commit ab6ab52

Browse files
committed
init
0 parents  commit ab6ab52

File tree

7 files changed

+389
-0
lines changed

7 files changed

+389
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
!.gitkeep
2+
vendor/*
3+
composer.lock

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
Soli helpers
2+
------------
3+
4+
常用的 PHP「辅助」函数。
5+
6+
## 字符串
7+
8+
### camelize
9+
10+
`camelize` 函数将给定字符串转换为 `驼峰格式`
11+
12+
echo camelize('coco_bongo'); // CocoBongo
13+
echo camelize('co_co-bon_go', '-'); // Co_coBon_go
14+
echo camelize('co_co-bon_go', '_-'); // CoCoBonGo
15+
16+
### uncamelize
17+
18+
`uncamelize` 函数将给定的字符串转换为 `蛇形格式`
19+
20+
echo uncamelize('CocoBongo'); // coco_bongo
21+
echo uncamelize('CocoBongo', '-'); // coco-bongo
22+
23+
### lower
24+
25+
`lower` 函数将给定的字符串转换为 `小写`
26+
27+
echo lower('HELLO'); // hello
28+
29+
### upper
30+
31+
`upper` 函数将给定的字符串转换为 `大写`
32+
33+
echo upper('hello'); // HELLO
34+
35+
### starts_with
36+
37+
`starts_with` 函数判断给定的字符串的`开头`是否是指定值:
38+
39+
echo starts_with('Hello', 'He'); // true
40+
echo starts_with('Hello', 'he'); // false
41+
42+
### ends_with
43+
44+
`ends_with` 函数判断给定的字符串`结尾`是否是指定的内容:
45+
46+
echo ends_with('Hello', 'llo'); // true
47+
echo ends_with('Hello', 'LLO'); // false
48+
49+
### contains
50+
51+
`contains` 函数判断字符串是否`包含`指定的值:
52+
53+
echo contains('Hello', 'ell'); // true
54+
echo contains('Hello', 'hll'); // false
55+
echo contains('Hello', ['hll', 'ell']); // true
56+
echo contains('Hello', ['hll', '']); // false
57+
58+
## JSON
59+
60+
### is_json
61+
62+
echo is_json('{"data":123}'); // true
63+
echo is_json('{data:123}'); // false

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "soliphp/helpers",
3+
"description": "Soli PHP helpers.",
4+
"type": "library",
5+
"keywords": ["helpers", "soliphp", "soli"],
6+
"homepage": "https://github.com/soliphp/helpers",
7+
"support": {
8+
"issues": "https://github.com/soliphp/helpers/issues",
9+
"source": "https://github.com/soliphp/helpers"
10+
},
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "ueaner",
15+
"email": "ueaner@gmail.com"
16+
}
17+
],
18+
"require": {
19+
"php": ">=5.5.0",
20+
"ext-mbstring": "*"
21+
},
22+
"require-dev": {
23+
"squizlabs/php_codesniffer": "^3.0",
24+
"phpunit/phpunit": "^5.7|^6.0"
25+
},
26+
"autoload": {
27+
"files": [
28+
"src/helpers.php"
29+
]
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"Soli\\Tests\\": "tests/"
34+
}
35+
},
36+
"minimum-stability": "dev"
37+
}

phpcs.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Soli Coding Standard">
3+
<description>Soli Coding Standard</description>
4+
5+
<!-- display progress -->
6+
<arg value="p"/>
7+
<!-- display sniff codes -->
8+
<arg value="s"/>
9+
<!-- use colors in output -->
10+
<arg name="colors"/>
11+
<!-- file extensions -->
12+
<arg name="extensions" value="php"/>
13+
14+
<!-- inherit rules from: -->
15+
<rule ref="PSR2"/>
16+
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
17+
<rule ref="PSR1">
18+
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols"/>
19+
</rule>
20+
21+
<!-- Paths to check -->
22+
<file>src</file>
23+
<file>tests</file>
24+
</ruleset>

phpunit.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
processIsolation="false"
9+
stopOnFailure="false"
10+
syntaxCheck="false"
11+
verbose="true"
12+
>
13+
<php>
14+
<ini name="xdebug.show_exception_trace" value="0"/>
15+
<ini name="error_reporting" value="-1"/>
16+
<ini name="display_errors" value="1"/>
17+
</php>
18+
19+
<testsuites>
20+
<testsuite name="Soli Test Suite">
21+
<directory suffix="Test.php">./tests/</directory>
22+
</testsuite>
23+
</testsuites>
24+
25+
<filter>
26+
<whitelist processUncoveredFilesFromWhitelist="true">
27+
<directory suffix=".php">./src/</directory>
28+
</whitelist>
29+
</filter>
30+
31+
<logging>
32+
<log type="coverage-clover" target="/tmp/phpunit.coverage.xml"/>
33+
<log type="junit" target="/tmp/phpunit.logfile.xml" logIncompleteSkipped="false"/>
34+
<log type="coverage-html" target="/tmp/phpunit.report" lowUpperBound="35" highLowerBound="70"/>
35+
</logging>
36+
</phpunit>

src/helpers.php

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
3+
if (!function_exists('camelize')) { // @codeCoverageIgnore
4+
/**
5+
* Converts strings to camelize style
6+
*
7+
* <code>
8+
* echo camelize('coco_bongo'); // CocoBongo
9+
* echo camelize('co_co-bon_go', '-'); // Co_coBon_go
10+
* echo camelize('co_co-bon_go', '_-'); // CoCoBonGo
11+
* </code>
12+
*
13+
* @param string $str
14+
* @param string $delimiter
15+
* @return string
16+
*/
17+
function camelize($str, $delimiter = '_')
18+
{
19+
$str = ucwords(str_replace(str_split($delimiter), ' ', $str));
20+
21+
return str_replace(' ', '', $str);
22+
}
23+
}
24+
25+
if (!function_exists('uncamelize')) { // @codeCoverageIgnore
26+
/**
27+
* Uncamelize strings which are camelized
28+
*
29+
* <code>
30+
* echo uncamelize('CocoBongo'); // coco_bongo
31+
* echo uncamelize('CocoBongo', '-'); // coco-bongo
32+
* </code>
33+
*
34+
* @param string $str
35+
* @param string $delimiter
36+
* @return string
37+
*/
38+
function uncamelize($str, $delimiter = '_')
39+
{
40+
if (!ctype_lower($str)) {
41+
$str = preg_replace('/\s+/u', '', $str);
42+
$str = lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $str));
43+
}
44+
45+
return $str;
46+
}
47+
}
48+
49+
if (!function_exists('lower')) { // @codeCoverageIgnore
50+
/**
51+
* Convert the given string to lower-case.
52+
*
53+
* <code>
54+
* echo lower('HELLO'); // hello
55+
* </code>
56+
*
57+
* @param string $str
58+
* @return string
59+
*/
60+
function lower($str)
61+
{
62+
return mb_strtolower($str, 'UTF-8');
63+
}
64+
}
65+
66+
if (!function_exists('upper')) { // @codeCoverageIgnore
67+
/**
68+
* Convert the given string to upper-case.
69+
*
70+
* <code>
71+
* echo upper('hello'); // HELLO
72+
* </code>
73+
*
74+
* @param string $str
75+
* @return string
76+
*/
77+
function upper($str)
78+
{
79+
return mb_strtoupper($str, 'UTF-8');
80+
}
81+
}
82+
83+
if (!function_exists('starts_with')) { // @codeCoverageIgnore
84+
/**
85+
* Check if a string starts with a given string
86+
*
87+
* <code>
88+
* echo starts_with('Hello', 'He'); // true
89+
* echo starts_with('Hello', 'he'); // false
90+
* </code>
91+
*
92+
* @param string $haystack
93+
* @param string $needle
94+
* @return bool
95+
*/
96+
function starts_with($haystack, $needle)
97+
{
98+
return (string)$needle === substr($haystack, 0, strlen($needle));
99+
}
100+
}
101+
102+
if (!function_exists('ends_with')) { // @codeCoverageIgnore
103+
/**
104+
* Check if a string ends with a given string
105+
*
106+
* <code>
107+
* echo ends_with('Hello', 'llo'); // true
108+
* echo ends_with('Hello', 'LLO'); // false
109+
* </code>
110+
*
111+
* @param string $haystack
112+
* @param string $needle
113+
* @return bool
114+
*/
115+
function ends_with($haystack, $needle)
116+
{
117+
return (string)$needle === substr($haystack, -strlen($needle));
118+
}
119+
}
120+
121+
if (!function_exists('contains')) { // @codeCoverageIgnore
122+
/**
123+
* Determine if a given string contains a given substring.
124+
*
125+
* <code>
126+
* echo contains('Hello', 'ell'); // true
127+
* echo contains('Hello', 'hll'); // false
128+
* echo contains('Hello', ['hll', 'ell']); // true
129+
* echo contains('Hello', ['hll', '']); // false
130+
* </code>
131+
*
132+
* @param string $haystack
133+
* @param string|array $needles
134+
* @return bool
135+
*/
136+
function contains($haystack, $needles)
137+
{
138+
foreach ((array) $needles as $needle) {
139+
if ($needle !== '' && mb_strpos($haystack, $needle) !== false) {
140+
return true;
141+
}
142+
}
143+
144+
return false;
145+
}
146+
}
147+
148+
if (!function_exists('is_json')) { // @codeCoverageIgnore
149+
/**
150+
* Check if a string is JSON
151+
*
152+
* <code>
153+
* echo is_json('{"data":123}'); // true
154+
* echo is_json('{data:123}'); // false
155+
* </code>
156+
*
157+
* @param string $str
158+
* @return bool
159+
*/
160+
function is_json($str)
161+
{
162+
if (is_string($str)) {
163+
json_decode($str);
164+
return (json_last_error() == JSON_ERROR_NONE);
165+
}
166+
return false;
167+
}
168+
}

tests/HelperTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Soli\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class HelperTest extends TestCase
8+
{
9+
public function testCamelize()
10+
{
11+
$this->assertEquals('CocoBongo', camelize('coco_bongo'));
12+
$this->assertEquals('Co_coBon_go', camelize('co_co-bon_go', '-'));
13+
$this->assertEquals('CoCoBonGo', camelize('co_co-bon_go', '_-'));
14+
}
15+
16+
public function testUncamelize()
17+
{
18+
$this->assertEquals('coco_bongo', uncamelize('CocoBongo'));
19+
$this->assertEquals('coco-bongo', uncamelize('CocoBongo', '-'));
20+
}
21+
22+
public function testLower()
23+
{
24+
$this->assertEquals('hello', lower('HELLO'));
25+
}
26+
27+
public function testUpper()
28+
{
29+
$this->assertEquals('HELLO', upper('hello'));
30+
}
31+
32+
public function testStartsWith()
33+
{
34+
$this->assertTrue(starts_with('Hello', 'He'));
35+
$this->assertFalse(starts_with('Hello', 'he'));
36+
}
37+
38+
public function testEndsWith()
39+
{
40+
$this->assertTrue(ends_with('Hello', 'llo'));
41+
$this->assertFalse(ends_with('Hello', 'LLO'));
42+
}
43+
44+
public function testContains()
45+
{
46+
$this->assertTrue(contains('Hello', 'ell'));
47+
$this->assertFalse(contains('Hello', 'hll'));
48+
$this->assertTrue(contains('Hello', ['hll', 'ell']));
49+
$this->assertFalse(contains('Hello', ['hll', '']));
50+
}
51+
52+
public function testIsJson()
53+
{
54+
$this->assertTrue(is_json('{"data":123}'));
55+
$this->assertFalse(is_json('{data:123}'));
56+
$this->assertFalse(is_json(null));
57+
}
58+
}

0 commit comments

Comments
 (0)