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


How to validate a parameter for a class within a method and prevent use if invalid?

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

这就是我目前所拥有的。。。

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

$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有更好的方法吗?我有许多方法需要令牌。

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

  • 您不需要将$user参数传递给retreive_token函数。你在课堂范围内得到了它。只要在函数中使用$this->user就可以访问它。而且你在那个函数中没有使用它,那么你为什么要传递它呢
  • 您没有在任何函数中发送true
  • for-each没有什么问题,但你也想检查array_map。至少您正在将一个函数应用于数组中的每个项。它可能很有用ps:似乎for-eacharray_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
}