Skip to content

Commit b227c86

Browse files
authored
Split hyperf/stringable from hyperf/utils (#5625)
0 parents  commit b227c86

File tree

10 files changed

+2968
-0
lines changed

10 files changed

+2968
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/tests export-ignore
2+
/.github export-ignore
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Close Pull Request
2+
3+
on:
4+
pull_request_target:
5+
types: [opened]
6+
7+
jobs:
8+
run:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: superbrothers/close-pull-request@v3
12+
with:
13+
comment: "Hi, this is a READ-ONLY repository, please submit your PR on the https://github.com/hyperf/hyperf repository.<br><br> This Pull Request will close automatically.<br><br> Thanks! "

.github/workflows/release.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
on:
2+
push:
3+
# Sequence of patterns matched against refs/tags
4+
tags:
5+
- 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10
6+
7+
name: Release
8+
9+
jobs:
10+
release:
11+
name: Release
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v2
16+
- name: Create Release
17+
id: create_release
18+
uses: actions/create-release@v1
19+
env:
20+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21+
with:
22+
tag_name: ${{ github.ref }}
23+
release_name: Release ${{ github.ref }}
24+
draft: false
25+
prerelease: false

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Taylor Otwell
4+
Copyright (c) Hyperf
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

composer.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "hyperf/stringable",
3+
"description": "Hyperf Stringable package which come from illuminate/support",
4+
"license": "MIT",
5+
"keywords": [
6+
"php",
7+
"swoole",
8+
"hyperf",
9+
"stringable"
10+
],
11+
"homepage": "https://hyperf.io",
12+
"support": {
13+
"docs": "https://hyperf.wiki",
14+
"issues": "https://github.com/hyperf/hyperf/issues",
15+
"pull-request": "https://github.com/hyperf/hyperf/pulls",
16+
"source": "https://github.com/hyperf/hyperf"
17+
},
18+
"require": {
19+
"php": ">=8.0",
20+
"hyperf/collection": "~3.0.0",
21+
"hyperf/conditionable": "~3.0.0",
22+
"hyperf/macroable": "~3.0.0",
23+
"hyperf/tappable": "~3.0.0"
24+
},
25+
"suggest": {
26+
"ramsey/uuid": "Required to use uuid and orderedUuid methods.(^4.7|^5.0)",
27+
"symfony/uid": "Required to use ulid method.(^5.0|^6.0)",
28+
"doctrine/inflector": "Required to use plural and singular methods.(^2.0|^3.0)"
29+
},
30+
"autoload": {
31+
"psr-4": {
32+
"Hyperf\\Stringable\\": "src/"
33+
}
34+
},
35+
"autoload-dev": {
36+
"psr-4": {
37+
"HyperfTest\\Stringable\\": "tests/"
38+
}
39+
},
40+
"config": {
41+
"sort-packages": true
42+
},
43+
"extra": {
44+
"branch-alias": {
45+
"dev-master": "3.0-dev"
46+
}
47+
}
48+
}

src/Pluralizer.php

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\Stringable;
13+
14+
use Doctrine\Inflector\CachedWordInflector;
15+
use Doctrine\Inflector\Inflector;
16+
use Doctrine\Inflector\Rules\English;
17+
use Doctrine\Inflector\RulesetInflector;
18+
19+
class Pluralizer
20+
{
21+
/**
22+
* Uncountable word forms.
23+
*/
24+
public static array $uncountable
25+
= [
26+
'audio',
27+
'bison',
28+
'cattle',
29+
'chassis',
30+
'compensation',
31+
'coreopsis',
32+
'data',
33+
'deer',
34+
'education',
35+
'emoji',
36+
'equipment',
37+
'evidence',
38+
'feedback',
39+
'firmware',
40+
'fish',
41+
'furniture',
42+
'gold',
43+
'hardware',
44+
'information',
45+
'jedi',
46+
'kin',
47+
'knowledge',
48+
'love',
49+
'metadata',
50+
'money',
51+
'moose',
52+
'news',
53+
'nutrition',
54+
'offspring',
55+
'plankton',
56+
'pokemon',
57+
'police',
58+
'rain',
59+
'rice',
60+
'series',
61+
'sheep',
62+
'software',
63+
'species',
64+
'swine',
65+
'traffic',
66+
'wheat',
67+
];
68+
69+
protected static ?Inflector $inflector = null;
70+
71+
/**
72+
* Get the plural form of an English word.
73+
*
74+
* @param string $value
75+
* @param int $count
76+
* @return string
77+
*/
78+
public static function plural($value, $count = 2)
79+
{
80+
if ((int) abs($count) === 1 || static::uncountable($value)) {
81+
return $value;
82+
}
83+
84+
$plural = static::getInflector()->pluralize($value);
85+
86+
return static::matchCase($plural, $value);
87+
}
88+
89+
/**
90+
* Get the singular form of an English word.
91+
*
92+
* @param string $value
93+
* @return string
94+
*/
95+
public static function singular($value)
96+
{
97+
$singular = static::getInflector()->singularize($value);
98+
99+
return static::matchCase($singular, $value);
100+
}
101+
102+
public static function setInflector(?Inflector $inflector): void
103+
{
104+
static::$inflector = $inflector;
105+
}
106+
107+
/**
108+
* Get the inflector instance.
109+
*/
110+
public static function getInflector(): Inflector
111+
{
112+
if (is_null(static::$inflector)) {
113+
static::$inflector = new Inflector(
114+
new CachedWordInflector(new RulesetInflector(
115+
English\Rules::getSingularRuleset()
116+
)),
117+
new CachedWordInflector(new RulesetInflector(
118+
English\Rules::getPluralRuleset()
119+
))
120+
);
121+
}
122+
123+
return static::$inflector;
124+
}
125+
126+
/**
127+
* Determine if the given value is uncountable.
128+
*
129+
* @param string $value
130+
* @return bool
131+
*/
132+
protected static function uncountable($value)
133+
{
134+
return in_array(strtolower($value), static::$uncountable);
135+
}
136+
137+
/**
138+
* Attempt to match the case on two strings.
139+
*
140+
* @param string $value
141+
* @param string $comparison
142+
* @return string
143+
*/
144+
protected static function matchCase($value, $comparison)
145+
{
146+
$functions = ['mb_strtolower', 'mb_strtoupper', 'ucfirst', 'ucwords'];
147+
148+
foreach ($functions as $function) {
149+
if (call_user_func($function, $comparison) === $comparison) {
150+
return call_user_func($function, $value);
151+
}
152+
}
153+
154+
return $value;
155+
}
156+
}

0 commit comments

Comments
 (0)