2013-08-26 6 views
5

私は、クエリ文字列を介して多くのパラメータを持つ残りのAPIを持っています。誰かがデザインパターンを知っているのか、すべてのパラメータ(オブジェクト、関数、配列、json)を整理する良い方法があるのだろうかと思います。今私は、同じ関数、非常に醜いコード内のすべてのパラメータを解析し、検証しています。REST APIパラメータの解析と検証の構成方法

理想的には、データベースORMや設定ファイル/配列/ jsonに似たパラメータを処理する方法があります。しかし、私はこの問題に対する解決策を思いつきませんでした。

洞察力があれば幸いです!私の考えの

例:

<?php 
... 

$parameters = [ 
    // ?fields=id,name 
    'fields' => [ 
     'default' => ['id', 'name'], 
     'valid' => ['id', 'name', 'date], 
     'type'  => 'csv', // list of values (id & name) 
     'required' => ['id'], 
     'replace' => ['title' => 'name'], // if the database & api names don't match 
     'relation' => null, // related database table 
    ], 
    // ?list=true 
    'list' => [ 
     'default' => ['false'], 
     'valid'  => ['true', 'false'], 
     'type'  => 'boolean' // single value (true or false) 
     'required' => [], 
     'replace' => [], // if the database & api names don't match 
     'relation' => 'category', // related database table 
    ], 
    .... 

]; 

答えて

2

あなたが検証ライブラリを探しているように私には思えます。私のお気に入りはSymfony's:https://github.com/symfony/validatorです。 Zend Framework 2には検証コンポーネントもあります。私は個人的にそれを使用していないが、私はそれも非常に良いことを期待しています。 symfonyの/バリのreadmeから

例:

<?php 

use Symfony\Component\Validator\Validation; 
use Symfony\Component\Validator\Constraints as Assert; 

$validator = Validation::createValidator(); 

$constraint = new Assert\Collection(array(
    'name' => new Assert\Collection(array(
     'first_name' => new Assert\Length(array('min' => 101)), 
     'last_name' => new Assert\Length(array('min' => 1)), 
    )), 
    'email' => new Assert\Email(), 
    'simple' => new Assert\Length(array('min' => 102)), 
    'gender' => new Assert\Choice(array(3, 4)), 
    'file'  => new Assert\File(), 
    'password' => new Assert\Length(array('min' => 60)), 
)); 

$inputは、そのようなYAMLのようないくつかの他の形式で検証ルールを定義することも可能である$_GET又はparse_str等を用いて得られたものであろう。

関連する問題