hallindavid/manny
Composer 安装命令:
composer require hallindavid/manny
包简介
a package of manipulators that hopefully come in useful for those of us who always forget regex when we need it (manny is short for manipulation)
README 文档
README
Manny (Short for Manipulators)
a light-weight PHP package of useful common manipulators/formatters.
Installation with Composer
composer require hallindavid/manny
if using Laravel, the Manny alias should be autodiscovered and usable easily like this.
use Manny; Manny::phone("8008008000"); // Returns: 800-800-8000
for other frameworks, you will likely need to do
require_once "vendor/autoload.php" use Manny; Manny::phone("8008008000"); // Returns: 800-800-8000
Manny::phone
Manny::phone - a Canada/US phone formatter - rebuilt better than before from hallindavid/phonehelper
Definition
/** * @param string $number * @param array $options * * * Default Options * * $default_options = [ * 'showCountryCode' => false, * 'showAreaCode' => true, * 'showExchange' => true, * 'showLine' => true, * 'showExtension' => false, * 'prefix' => false, * 'country_area_delimiter' => false, * 'area_exchange_delimiter' => '-', * 'exchange_line_delimiter' => '-', * 'line_extension_delimiter'=> ' ext. ', * ]; * @return string */ function phone($number, $options)
Example
Manny::phone("8008008000"); //outputs 800-800-8000
Extending Manny::phone
It's pretty easy to extend the phone class - here is an example
class Brack10 extends Manny\Phone { public function __construct($text) { parent::__construct($text); $this->showCountryCode = false; $this->showAreaCode = true; $this->showExchange = true; $this->showLine = true; $this->showExtension = false; $this->prefix = false; $this->country_area_delimiter = '('; $this->area_exchange_delimiter = ') '; $this->exchange_line_delimiter = '-'; $this->line_extension_delimiter = ' ext. '; } } $phone = new Brack10("123456789123456"); $phone->format(); //Returns: (234) 567-8912
Manny::mask
A mask function for formatting fixed-length data. (great for real-time-masking with livewire/livewire)
Definition
/** * @param string $target * @param string $pattern * @return string */ function mask($target, $pattern)
Pattern creation
A should be a placeholder for an alphabetical character
1 should be a placeholder for a numeric character
all other characters are treated as formatting characters
Example
//US Social Security Number Manny::mask("987654321", "111-11-1111"); //returns "987-65-4321" //US Zip-code Manny::mask("The whitehouse zip code is: 20500", "11111"); //returns "20500" //Canada Postal Code Manny::mask("K1M1M4", "A1A 1A1"); // //outputs 987-65-4321
Manny::yoink
use yoink to pull specific key-values from an associative array, and (optionally) pass in defaults.
Definition
/** * @param array $target - should be key-val associative array * @param array $elements - should be flat array with desired key names from target array * @param array $defaults (optional) - key-val associative array which will be appended to extracted key-value pairs before returning * @return array */ function yoink($target, $elements, $defaults = null)
Example
$array = ['id' => '17', 'name'=> 'John Doe']; $elements = ['name', 'role']; $default_values = ['role'=> 'member']; Manny::yoink($array, $elements, $default_values); //Returns: ['name'=>'John Doe','role'=>'member'] ;
Manny::stripper
a preg_replace abstraction easy-to-remember parameters to reduce frequent googling
Definition
/** * @param string $text - the subject of our stripping * @param array|null $options - an array with the return types you'd like * * keys can include the following types: * alpha - keep the alphabetical characters (case-insensitive) * num - keep the digits (0-9) * comma - keep commas * colon - keep the : character * dot - keep periods * dash - keep dashes/hyphens * space - keep spaces * underscore - keep underscores * pipe - keep pipe characters * bracket - keep square brackets [] * parenthesis - keep parenthesis () * curly - keep curley braces (useful for handlebar syntax ex. {{ thing }} * * @return string */ function stripper($text, $options = null)
Example
$string = 'With only 5-10 hours of development, Dave built Manny, saving him atleast 10 seconds per day!'; $config = ['num', 'alpha', 'space']; Manny::stripper($string,$config); //Returns: 'With only 510 hours of development Dave built Manny saving him atleast 10 seconds per day'; $alt_config = ['num']; Manny::stripper($string,$alt_config); //Returns: '51010';
Manny::keep
an alias for the Manny::stripper with the same functionality (since you are really "keeping" all the characters you define in the options, it makes for better code readability)
Example
$string = 'I only want to "keep" the alpha, num, and spaces for this string!'; $options = ['alpha', 'num', 'space'] Manny::keep() //Returns: 'I only want to keep the alpha num and spaces for this string'
Manny::crumble
a preg_replace abstraction easy-to-remember parameters to reduce frequent googling
Definition
/** * @param string $text - the subject of our crumbling * @param array $crumbs - an array of positive integers * @param bool $appendExtra - keys can include the following types * * @return array */ function crumble($string, $crumbs, $appendExtra = false)
Example
Manny::crumble("18008008000888", [1,3,3,4]) //Output: ["1","800","800","8000"]; //with append extra Manny::crumble("18008008000888", [1,3,3,4],true) //Output: ["1","800","800","8000", "888"];
Manny::percent
This is a quick-use tool for generating percents. It cleans up bad data before processing, and has an opinionated workflow (eg. 0/0 = 100%)
Definition
/** * @param int|float|string $num - the numerator * @param int|float|string $denom - the denominator * @param int $precision - keys can include the following types * * @return float */ function percent($num, $denom, $precision = 0)
Example
Manny::percent(1,8); //Output: 12.5;
Testing
There are a tonne of tests for the packaged formats - to run them, pull the package then
composer install
composer test
Support
To say thanks, you can share the project on social media or
Issues
Please report all issues in the GitHub Issue tracker
Contributing
Shoot me an email, or DM me on twitter and I am happy to allow other contributors.
hallindavid/manny 适用场景与选型建议
hallindavid/manny 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 109.65k 次下载、GitHub Stars 达 37, 最近一次更新时间为 2020 年 03 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「formatter」 「number」 「format」 「trim」 「string」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hallindavid/manny 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hallindavid/manny 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hallindavid/manny 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides a simple decimal/roman number converter
Softonic OpenApi validation formats extension
Tools to convert between .NET and PHP date formats
Yii2 phone formatter and validator.
Adds the EDTF data type to Wikibase
A package for working with arbitrary precision numbers in Laravel.
统计信息
- 总下载量: 109.65k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 38
- 点击次数: 33
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-29