collab-corp / input-formatter
PHP的格式化工具包
Requires
- php: ^7.0
- nesbot/carbon: ^1.22
Requires (Dev)
- phpunit/phpunit: ^6.0
- symfony/var-dumper: ^4.2
README
一个用于格式化/转换值的PHP包。
已弃用:此包已被弃用。请考虑使用我们的formatter包代替。
安装
通过composer
composer require collab-corp/input-formatter
简介
你很可能遇到过一个场景,需要将某些值转换为某种格式或有效值。当然,你可以实现这一点并自己调用内置的PHP函数,可能一个或两个函数调用不算太坏,但想象一下,如果你需要将几个函数应用到你的值或值上呢?以下是一些人可能会遇到的示例
//user input for a phone number $value = '(123)348-3847 '; //however your application doesnt care about formatted numbers such as parenthesis. //so we do some minor cleanup on our input: $value = trim($value); //then strip all non numeric numbers $value= preg_replace('/[^0-9]/', '', $value); //then any other formats/function calls your value might need
这也许不是一个最复杂的示例,但考虑一个示例,你可能需要通过5个其他函数调用来处理你的值,以获得所需的格式。或者更糟糕的是,如果需要在输入数组上执行此操作。
这就是这个包派上用场的地方,可以方便地为你处理多个调用,并帮助你使代码更整洁。
以下是使用三种不同方式创建Formatter的相同示例
//instance $value = new Formatter('(123)348-3847 ')->trim()->onlyNumbers()->get(); //statically $value = Formatter::create('(123)348-3847 ')->trim()->onlyNumbers()->get(); //helper function $value = formatter('(123)348-3847 ')->trim()->onlyNumbers()->get();
利用Formatter类的方法链能力,你可以使事物更简洁,不必担心会使代码变得混乱。
多个/数组值
Formatter类不仅限于单个值,还可以用于数组值。Formatter类将递归地应用格式化方法,这使得在不自己遍历它们的情况下转换多个值变得容易。
$values = formatter(['value123', 'value321', 'foo'])->onlyNumbers()->suffix('Yay!')->get(); //['123Yay!', '321Yay!', 'fooYay!'] $values = $values->get();
嵌套数组值也是如此。如果值是数组,则其每个值都将应用格式化方法!
批量转换
此包最有用的功能之一是能够将输入转换为干净的Laravel验证语法。如果你熟悉Laravel,那么这应该对你来说非常自然。
//using our \CollabCorp\Formatter\Concerns\FormatsInput trait $data = [ 'foo'=>'bar', 'test'=>'something%%%%%%', 'phone_number'=>'This is my phone number (123)134-4444' 'some_input'=>'some value', 'some_number'=>'2' 'some_other_input'=>'foooobar' //etc. ]; //can achieve the same with Formatter::convert($data,$formatterMethods); $convertedData = $this->convert($data, [ 'foo'=>'prefix:foo',//pass args to formatter methods using this format method:commaDelimetedArgs 'test'=>'rtrim:%', 'number*'=>'add:2|multiply:2' 'phone_number'=>'trim|onlyNumbers|phone' //can call multiple formatters using a pipe delimeter ]);
不错吧?等等,还有更多!你还可以利用定义格式化方法时的字符串模式
$this->convert($data,[ '*phone*'=>'....' // only call formatters if its name contains the word "phone" '*phone'=>'....' // only call formatters if its name ends the word "phone" 'phone*'=>'....' // only call formatters if its name starts the word "phone" ])
闭包和对象
需要在批量转换中获取更多控制?没问题。你可以利用使用闭包和实现我们的\CollabCorp\Formatter\Contracts\Formattable
接口的类对象
$this->convert($data,[ 'something'=>'...', 'other_input'=>['trim',function($value){ $value = 'new value'; return $value; }], 'something_else'=>['trim',FormatInput::class] ]) //FormatInput class: class FormatInput implements \CollabCorp\Formatter\Contracts\Formattable{ public function format($value) { $value = 'new value'; return $value; } }
这允许你更好地控制要格式化的值。
空值时退出
有时你可能只想在值非空时处理输入格式化器。例如,你可能只想在给定birth_date
输入非空时将其转换为Carbon实例。你可以通过在应处理但未提供值时应处理的任何格式化器之前指定bailIfEmpty
方法来完成此操作
$this->convert($data,[ 'birt_date'=>'bailIfEmpty|toCarbon' //only process toCarbon if there is a value. ]);
Carbon日期支持
您还可以方便地在这些大量转换过程中使用碳库。
$this->convert($data,[ //every method after toCarbon is passed to the underlying carbon instance //so you can call any available method on the carbon instance as needed. 'some_data'=>'toCarbon|setTimezone:America/Indiana/Indianapolis' ]);
可用的格式化方法
请参考类/文件 src/Formatter/Formatter.php
以查看可用的转换方法。
虽然我们可以在这里列出所有方法,但这会使本说明文件显得无限长,并最终变得臃肿,充斥着不必要的文档。
如果扩展此类或添加方法,重要的是要知道格式化类使用 __call
动态调用受保护的方法。
因此,如果您想向类中添加新的格式化方法并保持方法链支持和大量转换不受影响,请按照以下方式设置您的函数
。
protected function somethingNew($value, $anyOtherArgs)
{
//do stuff to value then return
return $value;
}
致谢/信用
处理大量转换中所有方法的 \CollabCorp\FormatterMethodParser\FormatterMethodParser
类利用了来自 Illuminate\Support
包中的几个方法。这些方法是 Laravel 团队编写的,并归功于他们。我们不声称编写了这些方法,我们只是在自己的类中实现了它们,以便方便地解析方法。有关更多信息,请参阅 Laravel 源代码
请参阅 Laravel 仓库。
贡献
以下方式接受贡献
- 问题跟踪器
- 拉取请求
- Collab Corp Slack(将发送邀请)
许可
本项目采用 MIT 许可证。