簡體   English   中英

具有基於方法類型的必需參數的Restler類

[英]Restler class with required params based on method type

假設我有這個要與我的API類一起使用

class EventInfo {
    /// @var int $start The Start time
    public $start

    /// @var string $url The URL for the event {@required false}
    public $url = null;
}

現在,我想將EventInfo用於POST和PATCH方法。 當我執行POST時, $start是要設置的必需屬性。 $url將作為可選參數進入。

但是,當我執行PATCH操作時,就不再需要$start 我可能正在經過一個新的開始時間,但可能沒有。

我該如何指定?

首先,使用DocBlock語法

class EventInfo {
    /**
     * @var int $start The Start time
     */
    public $start;

    /**
     * @var string $url The URL for the event {@required false}
     */
    public $url = null;
}

這將設置默認的必需和可選屬性。 然后,您可以使用{@required }注釋在api方法級別控制所需的屬性。 參見下面的例子

class Home
{
    public function index()
    {
        return array(
            'success' => array(
                'code' => 200,
                'message' => 'Restler is up and running!',
            ),
        );
    }

    public function patch(EventInfo $info)
    {
        return func_get_args();
    }

    /**
     * @param EventInfo $info {@required start,url}
     * @return array
     */
    public function post(EventInfo $info)
    {
        return func_get_args();
    }
}

您也可以類似地添加{@properties start}注釋,以限制API方法需要哪些屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM