You are here

class Response in reCAPTCHA 6.2

Same name and namespace in other branches
  1. 8.2 recaptcha-php/src/ReCaptcha/Response.php \ReCaptcha\Response
  2. 7.2 recaptcha-php/src/ReCaptcha/Response.php \ReCaptcha\Response

The response returned from the service.

Hierarchy

Expanded class hierarchy of Response

File

recaptcha-php/src/ReCaptcha/Response.php, line 32

Namespace

ReCaptcha
View source
class Response {

  /**
   * Succes or failure.
   * @var boolean
   */
  private $success = false;

  /**
   * Error code strings.
   * @var array
   */
  private $errorCodes = array();

  /**
   * Build the response from the expected JSON returned by the service.
   *
   * @param string $json
   * @return \ReCaptcha\Response
   */
  public static function fromJson($json) {
    $responseData = json_decode($json, true);
    if (!$responseData) {
      return new Response(false, array(
        'invalid-json',
      ));
    }
    if (isset($responseData['success']) && $responseData['success'] == true) {
      return new Response(true);
    }
    if (isset($responseData['error-codes']) && is_array($responseData['error-codes'])) {
      return new Response(false, $responseData['error-codes']);
    }
    return new Response(false);
  }

  /**
   * Constructor.
   *
   * @param boolean $success
   * @param array $errorCodes
   */
  public function __construct($success, array $errorCodes = array()) {
    $this->success = $success;
    $this->errorCodes = $errorCodes;
  }

  /**
   * Is success?
   *
   * @return boolean
   */
  public function isSuccess() {
    return $this->success;
  }

  /**
   * Get error codes.
   *
   * @return array
   */
  public function getErrorCodes() {
    return $this->errorCodes;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Response::$errorCodes private property Error code strings.
Response::$success private property Succes or failure.
Response::fromJson public static function Build the response from the expected JSON returned by the service.
Response::getErrorCodes public function Get error codes.
Response::isSuccess public function Is success?
Response::__construct public function Constructor.