承接 gustavonecore/php-sanitizer 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gustavonecore/php-sanitizer

Composer 安装命令:

composer require gustavonecore/php-sanitizer

包简介

PHP data sanitizer

README 文档

README

PHP sanitizer and validator

This is a library to sanitize your input from any source, using a predefined template of native types.

Why another sanitizer? Well, this is because I really want to maintain my toolset pretty small, and not depending on big libraries/frameworks.

Requirements

  • PHP >= v7.0

Install it with composer

composer require gustavonecore/php-sanitizer

Example of usage

Template definition First, you must define your template

    $filter = new Gcore\Sanitizer\Template\TemplateSanitizer([
    	'first_name' => 'string',
    	'dob' => 'int',
    	'numbers' => 'int[]',
    	'test' => 'int',
    	'email' => 'email',
    	'email_wrong' => 'email',
    	'double' => 'double',
    	'boolean' => 'bool',
    	'datetime' => 'datetime',
    	"persons[]" => [
    		'name' => 'string',
    		'eyes' => 'int',
    		'address' => 'string[]',
    		'commits[]' => [
    			'hash' => 'string',
    			'n_comments' => 'int',
    			'users' => 'string[]'
    		],
    	],
    ]);

Required fields You can force the sanitization process to require fields, you just need to include a ! at the end of the rule, e.g:

    $filter = new Gcore\Sanitizer\Template\TemplateSanitizer([
    	'email' => 'email!',
    	'phone' => 'string!',
    	'first_name' => 'string',
    ]);

If required fields is invalid If the input have required fileds with invalid data (null) the library will throw a Gcore\Sanitizer\Template\RequiredFieldsException

Sanitize! After that, you are good to sanitize any input

    // This will be your inut body from an user
    $input = [
    	'first_name' => 'Gustavo',
    	'dob' => '10',
    	'numbers' => [1,2,3,'4','5'],
    	'foo' => [1,2,4],
    	'email' => 'gustavo.uach@gmail.com',
    	'email_wrong' => 'gustavo.uachgmail.com',
    	'double' => '7876',
    	'boolean' => true,
    	'datetime' => '2017-11-11 13:50:10',
    	'persons' => [
    		[
    			'name' => 'jhon',
    			'eyes' => 2,
    			'address' => ['foo', 'bar', 'text'],
    			'commits' => [
    				'hash' => '2221321n3kj12n3kj12n32j1',
    				'n_comments' => 900,
    				'users' => ['jhon', 'doe'],
    			],
    		],
    				[
    			'name' => 'albert',
    			'eyes' => 'wrong int here',
    			'address' => ['a', 'b', 1],
    			'commits' => null,
    		],
    	]
    ];

    // All your data is clean now! awesome!
    $cleanOutput = $filter->sanitize($input);

    print_r($cleanOutput);

Clean Output

Output of the previous call

    php examples/index.php
    Array
    (
        [first_name] => Gustavo
        [dob] => 10
        [numbers] => Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
                [3] => 4
                [4] => 5
            )

        [test] =>
        [email] => gustavo.uach@gmail.com
        [email_wrong] =>
        [double] => 7876
        [boolean] => 1
        [datetime] => DateTimeImmutable Object
            (
                [date] => 2017-11-11 13:50:10.000000
                [timezone_type] => 3
                [timezone] => America/Santiago
            )

        [persons] => Array
            (
                [0] => Array
                    (
                        [name] => jhon
                        [eyes] => 2
                        [address] => Array
                            (
                                [0] => foo
                                [1] => bar
                                [2] => text
                            )

                        [commits] => Array
                            (
                                [hash] => 2221321n3kj12n3kj12n32j1
                                [n_comments] => 900
                                [users] => Array
                                    (
                                        [0] => jhon
                                        [1] => doe
                                    )

                            )

                    )

                [1] => Array
                    (
                        [name] => albert
                        [eyes] =>
                        [address] => Array
                            (
                                [0] => a
                                [1] => b
                                [2] => 1
                            )

                        [commits] =>
                    )

            )

    )

Notes

  • Not defined keys in the template will be ignored by the sanitizer.
  • If a value doesn't match the desired type, will be returned as null

TODO

  • Add unit tests. #1 (In progress)
  • Add required fields.
  • Create a new template method to define required fields.
    • Improve this method to allow nested fields
  • Modularize a bit more the Strategy selector to allow extending the library with new types of data.

gustavonecore/php-sanitizer 适用场景与选型建议

gustavonecore/php-sanitizer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 142 次下载、GitHub Stars 达 5, 最近一次更新时间为 2017 年 07 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 gustavonecore/php-sanitizer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gustavonecore/php-sanitizer 我们能提供哪些服务?
定制开发 / 二次开发

基于 gustavonecore/php-sanitizer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 142
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-07-21