如何在方法中验证类的参数并在无效时阻止使用?

时间:2016-07-27 22:54:03

标签: php api validation oop

我正在构建一个小类来处理api请求,我遇到了一个错误处理问题(我也是OOP的新手所以请跟我一起)我需要限制或抛出我班上任何方法的错误这需要设置用户参数,如果令牌没有被检索,我也需要同样的东西,我似乎无法绕过它。

这就是我到目前为止......

$ user数组在类之外的配置文件中设置,如此(默认为空):

$user = array(
    'user_email' = '',
    'user_pass' = ''
);

处理API的类(简化问题)

class eventAPI {

    private $user
    private $token

    public function __construct($user) {

        $this->user = $user;

        // if possible assign token when instantiated
        $this->retrieve_token($user);

    }

    private function retreive_token($user) {

        // Check if user parameter has been set
        if($this->validate_user_parameter()) {

            // use credentials to make HTTP request for token 
            $token = 'somerandomtoken';

            // assign token property retreived value
            $this->token = $token;

        } else {

            echo 'User parameter has not been set.' // Not real message just for testing
            return FALSE;
        }
    }

    public function show_all_events() {

        // Check if token has been retreived
        if($this->validate_token_retreived()) {

            // Use token to retreive events list via HTTP request

        } else {

            echo 'API not active. No valid token detected'; // for testing purposes
            return FALSE
        }

    }

    // reduntant code... Can't wrap my head around another way for checking for token.
    public function show_single_event() {

        // Check if token has been retreived
        if($this->validate_token_retreived()) {

            // Use token to retreive events list via HTTP request

        } else {

            echo 'API not active. No valid token detected'; // for testing purposes
            return FALSE
        }
    }

    // This is mostly where I am confused how to solve. 
    private function validate_user_parameter() {

        foreach($this->user as $key => $value) {

            // Remove whitespace from value
            $value = trim($value);

            if(empty($value)) {

                echo 'User credentials have not been set'; // for testing purposes

                return FALSE;

            }
        }

    }

    private function validate_token_retreived() {

        $result = FALSE;

        // Bool value not sure if this is the best way to do this
        if(isset($this->$token)) {
            $result = TRUE;
        }

        return $result;
    }


}

第一个问题:我需要验证数组中的用户参数,以便我可以使用私有方法来检索令牌。我选择使用foreach循环来检查每个值,但它看起来有点陈旧。

第二个问题:我对每个公共方法都进行了冗余检查,以检查令牌是否有效。使用OOP有更好的方法吗?我有很多方法需要令牌。

简而言之,我如何确保在实例化类之后,如果任何验证失败,最终用户将使用的公共方法将不会触发。用户信息在实例化时只需要有效一次,然后令牌需要对大多数剩余方法有效。

1 个答案:

答案 0 :(得分:0)

  • 您不需要将$user参数传递给retreive_token函数。你在课堂范围内得到它。只需在函数中使用$this->user即可访问它。你也没有在那个函数中使用它,所以你为什么通过它?
  • 您没有在任何功能中发送true
  • for-each没有错,但您也要检查array_map。至少你将一个函数应用于数组中的每个项目。它可能很有用。 ps:似乎for-each仍然比array_map
  • 此外,您还需要检查empty函数,在哪种情况下返回false
  • 您可以在函数中使用多个返回。你不需要设置一个变量来做到这一点。

示例

private function validate_token_retreived()
{
    if(isset($this->token))
        return true;

    return false;
}
  • 在大多数情况下,您无法使用else

示例

public function show_all_events()
{
   if($this->validate_token_retreived()) {
       // Use token to retreive events list via HTTP request 
       // and return here

   }

   echo 'API not active. No valid token detected'; // for testing purposes
   return FALSE; // do you really return here? seems you are just generating an output
}