简体   繁体   English

具有基于方法类型的必需参数的Restler类

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

Let's say I have this to use with my API classes 假设我有这个要与我的API类一起使用

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

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

And now I want to use EventInfo for both my POST and my PATCH methods. 现在,我想将EventInfo用于POST和PATCH方法。 When I'm doing a POST, $start is a required property to be set. 当我执行POST时, $start是要设置的必需属性。 The $url will come in as an optional parameter. $url将作为可选参数进入。

However, when I'm doing a PATCH operation, then $start should no longer be required. 但是,当我执行PATCH操作时,就不再需要$start I might be passing a new start time, but I might not. 我可能正在经过一个新的开始时间,但可能没有。

How do I specify this? 我该如何指定?

First, use the DocBlock syntax 首先,使用DocBlock语法

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

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

This sets the default required and optional properties. 这将设置默认的必需和可选属性。 Then you can control the required properties at api method level using {@required } comment. 然后,您可以使用{@required }注释在api方法级别控制所需的属性。 See the example below 参见下面的例子

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();
    }
}

You may also add {@properties start} comment similarly to restrict which properties are needed by the API method. 您也可以类似地添加{@properties start}注释,以限制API方法需要哪些属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM