PHPackages                             clazz/typed-sanitizer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. clazz/typed-sanitizer

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

clazz/typed-sanitizer
=====================

A Typed Sanitizer For PHP

1.0.6(9y ago)666↓84.6%MITPHPPHP &gt;=5.4

Since Jun 15Pushed 9y ago1 watchersCompare

[ Source](https://github.com/clazz/php-typed-sanitizer)[ Packagist](https://packagist.org/packages/clazz/typed-sanitizer)[ RSS](/packages/clazz-typed-sanitizer/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (2)Versions (8)Used By (0)

PHP Typed Sanitizer (PHP类型净化器)
==============================

[](#php-typed-sanitizer-php类型净化器)

[![](https://camo.githubusercontent.com/e550e257041c36faadfe437c71654744574253e896dc76c39b98da9c0f458ed8/68747470733a2f2f7472617669732d63692e6f72672f636c617a7a2f7068702d74797065642d73616e6974697a65722e737667)](https://travis-ci.org/clazz/php-typed-sanitizer)

如何安装？
-----

[](#如何安装)

推荐使用composer进行安装：

```
composer require clazz/typed-sanitizer
```

使用说明
----

[](#使用说明)

这个净化器可以用于净化外部输入（用户输入、接口输入）。 啥也不说了，上段代码试试：

```
use Clazz\Typed\Types\Type;

// 假设有个接口想要一个用户的数据，而外部输入是这样：
$input = [
    'id' => '123',
    'name' => ' James William ',
    'age' => '12',
    'isMale' => '1',
];

// 可以定义我们想要的是这样的：
$userDefinition = Type::arr([
    'id'     => 'int',
    'name'   => Type::string()->trim()->length('< 30'),
    'age'    => Type::int()->isRequired(),
    'isMale' => Type::boolean(),
]);

// 然后就可以执行净化了：
$sanitizedUserData = $userDefinition->sanitize($input);

// 看看得到的数据，果然是预期的：
var_export($sanitizedUserData);
//输出:
//    array (
//      'id' => 123,
//      'name' => 'James William',
//      'age' => 12,
//      'isMale' => true,
//    )
```

为了方便使用，本库还提供了两个工具类： `Clazz\Typed\Sanitizer` 和 `Clazz\Typed\Illuminate\Support\Facades\Input`.

使用`Clazz\Typed\Sanitizer`可以省去定义`Type`，修改后的示例：

```
use Clazz\Typed\Types\Type;
use Clazz\Typed\Sanitizer;

// 假设有个接口想要一个用户的数据，而外部输入是这样：
$input = [
    'id' => '123',
    'name' => ' James William ',
    'age' => '12',
    'isMale' => '1',
];

// 类型定义 + 执行净化了：
$sanitizedUserData = Sanitizer::getSanitized([
         'id'     => 'int',
         'name'   => Type::string()->trim()->length('< 30'),
         'age'    => Type::int()->isRequired(),
         'isMale' => Type::boolean(),
     ], $input);
```

在`Laravel`/`Lumen`中使用`Clazz\Typed\Illuminate\Support\Facades\Input`就更方便了 —— 它会默认从HTTP请求中获取输入：

```
use Clazz\Typed\Types\Type;
use Clazz\Typed\Illuminate\Support\Facades\Input;

// 假设有个接口想要一个用户的数据，而HTTP的输入参数是这样：
// id=123&name=%20%20James%20William%20%20&age=12&isMale=1

// 类型定义 + 执行净化了：
$sanitizedUserData = Input::getSanitized([
         'id'     => 'int',
         'name'   => Type::string()->trim()->length('< 30'),
         'age'    => Type::int()->isRequired(),
         'isMale' => Type::boolean(),
     ]);
```

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~59 days

Recently: every ~89 days

Total

7

Last Release

3305d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8750132?v=4)[牧毅](/maintainers/Clarence-pan)[@Clarence-pan](https://github.com/Clarence-pan)

---

Top Contributors

[![Clarence-pan](https://avatars.githubusercontent.com/u/8750132?v=4)](https://github.com/Clarence-pan "Clarence-pan (12 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/clazz-typed-sanitizer/health.svg)

```
[![Health](https://phpackages.com/badges/clazz-typed-sanitizer/health.svg)](https://phpackages.com/packages/clazz-typed-sanitizer)
```

###  Alternatives

[chaoswey/taiwan-id-validator

台灣身分證、統一編號驗證

319.9k](/packages/chaoswey-taiwan-id-validator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
