Skip to content
This repository has been archived by the owner on Jan 12, 2022. It is now read-only.

Commit

Permalink
ability to use external yii2-httpclient object
Browse files Browse the repository at this point in the history
  • Loading branch information
himiklab committed May 23, 2018
1 parent 2f7e609 commit 6fdb334
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions ReCaptchaValidator.php
Expand Up @@ -10,7 +10,8 @@
use Yii;
use yii\base\Exception;
use yii\base\InvalidConfigException;
use yii\httpclient\Client;
use yii\httpclient\Client as HttpClient;
use yii\httpclient\Request as HttpClientRequest;
use yii\validators\Validator;

/**
Expand All @@ -32,6 +33,9 @@ class ReCaptchaValidator extends Validator
/** @var string */
public $uncheckedMessage;

/** @var \yii\httpclient\Request */
public $httpClientRequest;

/** @var boolean */
protected $isValid = false;

Expand All @@ -49,6 +53,10 @@ public function init()
}
}

if (empty($this->httpClientRequest) || !($this->httpClientRequest instanceof HttpClientRequest)) {
$this->httpClientRequest = (new HttpClient())->createRequest();
}

if ($this->message === null) {
$this->message = Yii::t('yii', 'The verification code is incorrect.');
}
Expand Down Expand Up @@ -107,9 +115,7 @@ protected function validateValue($value)
*/
protected function getResponse($value)
{
$client = new Client();
/** @var yii\httpclient\Response $response */
$response = $client->createRequest()
$response = $this->httpClientRequest
->setMethod('GET')
->setUrl(self::SITE_VERIFY_URL)
->setData(['secret' => $this->secret, 'response' => $value, 'remoteip' => Yii::$app->request->userIP])
Expand Down

0 comments on commit 6fdb334

Please sign in to comment.