actengage / sanitize
常用清洗函数集合。
v2.0.1
2024-07-12 02:11 UTC
Requires
- php: ^8.2
- laravel/framework: ^11.0
Requires (Dev)
- laravel/pint: ^1.8
- orchestra/testbench: ^9.0
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2024-09-12 02:31:28 UTC
README
一组用于清洗常见数据(如电子邮件、电话和邮编)的类。
安装
composer require actengage/sanitize
发布配置
您可以选择发布配置文件。
php artisan vendor:publish --tag=sanitize-config
基本示例
use Actengage\Sanitize\Facades\Sanitize; Sanitize::email(' JOHN.doe @gmail '); // johndoe@gmail.com Sanitize::phone('(888) 555-1234'); // 8885551234 Sanitize::zip('12345'); // 12345
属性类型转换
您可以将 Eloquent 属性轻松转换为清洗后的值。
use Actengage\Sanitize\Casts\Email; use Actengage\Sanitize\Casts\Phone; use Actengage\Sanitize\Casts\Zip; class User extends Model { protected $guarded = []; protected $casts = [ 'email' => Email::class, 'phone' => Phone::class, 'zip' => Zip::class, ]; } $user = User::create([ 'email' => 'john.doe@gmail.com', 'phone' => '1-800-555-1234', 'zip' => '1234', ]) dd($user->email); // johndoe@gmail.com dd($user->phone); // 8005001234 dd($user->zip); // 01234
默认清洗器
您可以在 config/sanitize.php
中添加默认清洗器。
<?php use Actengage\Sanitize\Sanitizers\Email; use Actengage\Sanitize\Sanitizers\Phone; use Actengage\Sanitize\Sanitizers\Zip; return [ /* |-------------------------------------------------------------------------- | Sanitizers |-------------------------------------------------------------------------- | | This value is an array of invokable classes that implement the Sanitizer | interface. These are the default macros associated with the Sanitizer | instance. | | use Actengage\Sanitize\Facades\Sanitize; | | Sanitize::email('test @test.com'); // test@test.com | Sanitize::phone('(888) 555-1234'); // 8885551234 | Sanitize::zip('12345-1234'); // 123451234 | */ 'sanitizers' => [ 'email' => Email::class, 'phone' => Phone::class, 'zip' => Zip::class, ] ];
清洗器宏
您还可以使用宏添加额外的清洗器函数。
Sanitize::macro('test', function($value) { return round($value) * 100; }); Sanitize::test(100.1234); // 10000