avris / stringer
Requires
- avris/localisator: ^4.0
- mustangostang/spyc: ^0.6
Requires (Dev)
- phpunit/phpunit: ^6.5
- squizlabs/php_codesniffer: ^3.2
- symfony/var-dumper: ^4.0
README
一组有用的字符串相关辅助工具。
安装
composer require avris/stringer
使用
$stringer = (new LocalisatorBuilder())
->registerExtension(new LocalisatorExtension('en'))
->registerExtension(new StringerExtension())
->build(Stringer::class);
如果您将 StringerTwig
注册为 Twig 扩展,您可以使用以下所有辅助工具作为 Twig 过滤器,例如
echo $stringer->timeDiff('2017-03-04 20:56');
也可以用作
{{ '2017-03-04 20:56'|timeDiff }}
一些辅助工具需要针对特定语言进行定制。英语是内置的,可以通过 Avris Polonisator 添加波兰语。将其作为添加更多语言的参考。
convertCase
convertCase 将字符串转换为几种编程约定
$stringer->convertCase('title')
→Title Case
$stringer->convertCase('camel')
→camelCase
$stringer->convertCase('pascal')
→PascalCase
$stringer->convertCase('underscore')
→underscore_case
感谢 Avro
clearUrl
使用 clearUrl 处理的 URL 地址将删除所有不显示给用户的部分
<a href="{{ post.source }}" target="_blank">{{ post.source|clearUrl }}</a>
对于 https://9gag.com/gag/aVOBPxn?ref=fsidebar
,此代码将生成一个有效的链接,但显示为 9gag.com/gag/aVOBPxn
。
numberInWords
函数 numberInWords 接受一个数字,并返回其文字表示的字符串
375 → three hundred seventy-five
-279 → minus two hundred seventy-nine
146 → one hundred fourty-six
10000000 → ten million
5371.18 → five thousand three hundred seventy-one and eighteen hundrenth
5678 → five thousand six hundred seventy-eight
0.1 → one tenth
0.000908 → zero comma zero zero zero nine zero eight
timeInWords
函数 numberInWords 接受一个时间(\DateTime
或可由 \DateTime
解析的字符串)并返回一个表示该时间的字符串。有三种模式可用,细节程度逐渐增加。
$stringer->timeInWords($time, TimeInWords::MODE_NICE)
(默认)
14:15:16 → quarter past two pm
05:13:32 → thirteen past five am
19:49:20 → eleven to eight pm
11:53:11 → seven to noon
$stringer->timeInWords($time, TimeInWords::MODE_SHORT)
:
14:15:16 → two fifteen pm
05:13:32 → five thirteen am
19:49:20 → seven fourty-nine pm
11:53:11 → eleven fifty-three pm
$stringer->timeInWords($time, TimeInWords::MODE_LONG)
:
14:15:16 → two fifteen pm and sixteen seconds
05:13:32 → five thirteen am and thirty-two seconds
19:49:20 → seven fourty-nine pm and twenty seconds
11:53:11 → eleven fifty-three pm and eleven seconds
timeDiff
函数 timeDiff 接受一个 datetime(\DateTime
或可由 \DateTime
解析的字符串)并返回一个解释它多久以前或多久以后的字符串。您可以通过指定第二个参数来更改参考点,从 "now"
到不同的日期。
echo $stringer->timeDiff('2018-03-04 20:56');
将输出类似 "now"、"5分钟前"、"2小时前" 等文本,具体取决于当前时间。
listify
函数 listify 接受一个元素列表和一个数字,表示最多可以显示多少个元素(默认 0
,表示没有限制),其余的将被替换为 "和 X 个其他" 或类似的内容,例如
$stringer->listify(['A','B','C','D','E','F'], 1); // '6 people commented'
$stringer->listify(['A','B','C','D','E','F'], 2); // 'A and 5 others commented'
$stringer->listify(['A','B','C','D','E','F'], 3); // 'A, B and 4 others commented'
$stringer->listify(['A','B','C','D','E','F'], 4); // 'A, B, C and 3 others commented'
$stringer->listify(['A','B','C','D','E','F'], 5); // 'A, B, C, D and 2 others commented'
$stringer->listify(['A','B','C','D','E','F'], 6); // 'A, B, C, D, E and F commented'
$stringer->listify(['A','B','C','D','E','F'], 7); // 'A, B, C, D, E and F commented'
$stringer->listify(['A','B','C','D','E','F'], 8); // 'A, B, C, D, E and F commented'
$stringer->listify(['A','B','C','D','E','F'], 0); // 'A, B, C, D, E and F commented'
$stringer->listify(['A'], 0); // 'A commented'
$stringer->listify(['A', 'B'], 0); // 'A and B commented'
数组元素可以是字符串或定义了 __toString
的对象。
默认使用 "人们评论了" 的翻译,但您可以使用第三个参数更改翻译键
$stringer->listify($loadedProcesses, 3, 'app:answer.listify.');
并在 app.en.yml
answer:
listify:
simple: 'loaded'
addition: '<> one process loaded|%count% processes loaded'
others: '<> one other process loaded|%count% other processes loaded'
and: 'and'
phone
函数 phone 提供了电话号码的优美格式化
- 它确定国家代码(并将
00
标准化为+
),如果未提供,但您提供了第二个参数作为默认区域设置,它将假设该国家的国家代码 - 如果已为该国家代码注册了
PhoneLocaleFormatter
,则电话号码将进一步格式化,例如对于pl_PL
/+48
来自 Avris Polonisator- 它删除开头的可选
0
- 将数字分成正确的块(移动电话和特殊号码 - 三个数字的三个块;固定电话 - 分成城市代码和实际号码)。
- 它删除开头的可选
0048501123456 → +48 501 123 456
48911234567 → +48 91 1234567
0911234567 → 91 1234567
0202224 → 20 2224
001555123456 → +1 555123456
+69012345 → +690 12345
arabicToRoman
函数 arabicToRoman 将1-3999范围内的自然数转换为罗马数字。
框架集成
Micrus
在你的 App\App:registerModules
中注册 Localisator 模块和 Stringer 模块
yield new \Avris\Localisator\LocalisatorModule;
yield new \Avris\Stringer\StringerModule;
并且应该直接可用:Avris\Stringer\Stringer
在容器中可用,并且已注册 Twig 扩展。
Symfony
示例 servies.yaml
配置
Avris\Localisator\LocalisatorBuilder:
calls:
- [registerExtension, ['@Avris\Localisator\LocalisatorExtension']]
- [registerExtension, ['@Avris\Stringer\StringerExtension']]
Avris\Localisator\LocalisatorExtension:
arguments:
$locale: '%locale%'
Avris\Stringer\StringerExtension: ~
Avris\Localisator\LocalisatorInterface:
factory: ['@Avris\Localisator\LocalisatorBuilder', build]
arguments: ['Avris\Localisator\LocalisatorInterface']
Avris\Localisator\LocalisatorTwig: ~
Avris\Stringer\Stringer:
factory: ['@Avris\Localisator\LocalisatorBuilder', build]
arguments: ['Avris\Stringer\Stringer']
Avris\Stringer\StringerTwig:
public: true
版权
- 作者: Andre Prusinowski (Avris.it)
- 许可证: MIT