Rock Sanitize-シンプルで柔軟な消毒剤

ロックサニタイズについて話す みなさんこんにちは!

Webアプリケーションを保護するツールについて引き続き説明します。 現在、これはデータサニタイズライブラリです。

サニテーションは、ユーザー入力から無効または安全でない文字を削除(またはエスケープ)するか、出力を正しく生成します。

そのため、すでにRock Validateに出会った人は、同様の構文に喜んで驚かれることでしょう。

Sanitize::removeTags() ->lowercase() ->sanitize('<b>Hello World!</b>'); // output: hello world! 

コンストラクタを介して:

 (new Sanitize)->removeTags()->lowercase()->sanitize('<b>Hello World!</b>'); 

ルール


ルールのセットはまだ十分な大きさではありませんが、カスタマイズによって簡単に補うことができます。

ルールのグループがあります:

ルールの完全なリスト

それらのいくつかに注意します。

呼び出し()

最初の引数として呼び出し可能な値を取ります。 例:
 $s = Sanitize::call('mb_strtolower', ['UTF-8']); $s->sanitize(' !'); // output:  ! 

シリアライズ解除()

jsonまたはphp文字列が表現であるかどうかを自動的に検出し、それに応じてデシリアライズします。
 $s = Sanitize::unserialize()->removeTags()->trim() $s->sanitize('{"name" : " <b> Tom </b> "}'); /* output: [ 'name' => 'Tom' ] */ 

属性衛生


属性によって配列/オブジェクトをサニタイズするには、attributes()メソッドが使用されます。
 $input = [ 'name' => '<b>Tom</b>', 'age' => -22 ]; $attributes = [ 'name' => Sanitize::removeTags(), 'age' => Sanitize::abs() ]; Sanitize::attributes($attributes)->sanitize($input); /* output: [ 'name' => 'Tom', 'age' => 22 ] */ 

すべての属性に1つのルールを使用する必要がある場合は、次のようにします。
 $input = [ 'name' => '<b>Tom</b>', 'email' => '<b>tom@site.com</b>', ]; Sanitize::attributes(Sanitize::removeTags())->sanitize($input); /* output: [ 'name' => 'Tom', 'email' => 'tom@site.com' ] */ 

デフォルトでは、配列/オブジェクト属性による再帰的走査が有効になっています。 例:
 $input = [ 'name' => '<b>Tom</b>', 'other' => [ 'email' => '<b>tom@site.com</b>', 'note' => [ '<b>text...</b>' ] ] ]; Sanitize::attributes(Sanitize::removeTags())->sanitize($input); /* output: [ 'name' => 'Tom', 'other' => [ 'email' => 'tom@site.com', 'note' => [ 'text...' ] ] ] */ 

この動作を無効にできます:

 Sanitize::recursive(false)->attributes(Sanitize::removeTags()); 

追加機能


残差の原則に従ってルールのセットを指定することができます。
 $input = [ 'name' => '<b> Tom</b>', 'email' => '<b>tom@site.com </b>', 'age' => -22, ]; $s = Sanitize::attributes([ 'age' => Sanitize::abs(), '*' => Sanitize::removeTags()->trim() ]); $s->sanitize($input); /* output: [ 'name' => 'Tom', 'email' => 'tom@site.com', 'age' => 22, ] */ 

ラベル「*」は、他のものに置き換えることができます。

 Sanitize::labelRemainder('_remainder'); 

同様の機能がRock Validateで利用可能になりました。

配列の後ろにある属性のサニタイズが必要な場合は、キーのチェーンを指定できます。
 $input = [ 'name' => '<b>Tom</b>', 'other' => [ 'tel' => '<b>777-777</b>', 'email' => '<b>tom@site.com</b>', 'note' => [ 'first' => '<b> text... </b> ', ] ] ]; $attributes = [ 'other.email' => Sanitize::removeTags(), 'other.note.first' => Sanitize::removeTags()->trim() ]; Sanitize::attributes($attributes)->sanitize($input); /* output: [ 'name' => '<b>Tom</b>', 'other' => [ 'tel' => '<b>777-777</b>', 'email' => 'tom@site.com', 'note' =>[ 'first' => 'text...', ] ] ] */ 

この機能はアレイでのみ使用可能です。

カスタマイズ


ルールを使用してクラスを作成します。
 use rock\sanitize\rules\Rule class Round extends Rule { protected $precision = 0; public function __construct($precision = 0) { $this->precision= $precision; } public function sanitize($input) { return round($input, $this->precision); } } 

利益:
 $config = [ 'rules' => [ 'round' => \namespace\to\Round::className() ] ]; $s = new Sanitize($config); $s->round()->sanitize(7.4); // output: 7.0 

したがって、既存のルールを置き換えるか、ルールに他のエイリアスを指定することができます。

設置


 composer require romeoz/rock-sanitize:* 

Githubプロジェクト

Source: https://habr.com/ru/post/J254045/


All Articles