Skip to content

Commit a0c49d3

Browse files
committed
upgraded for Nette 3.0 & PHP7.1+
thanks to @peldax (#21)
1 parent ed5df70 commit a0c49d3

24 files changed

Lines changed: 131 additions & 149 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ You can change the default requester by setting the `requester` key in [configur
175175
176176
You can also implement your own requester. Just make sure it implements the [ReCaptchaControl\Http\Requester\IRequester](src/ReCaptchaControl/Http/Requester/IRequester.php) interface.
177177
178-
It basically requires a single `public function post($url, array $values = [])` method which takes URL, performs a HTTP POST request with given `$values` and returns body of the response as a string. In case of a failure, `ReCaptchaControl\Http\Requester\RequestException` should be thrown.
178+
It basically requires a single `public function post(string $url, array $values = []): string` method which takes URL, performs a HTTP POST request with given `$values` and returns body of the response as a string. In case of a failure, `ReCaptchaControl\Http\Requester\RequestException` should be thrown.
179179
180180
You can then use it the same way as above:
181181

composer.json

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@
99
"issues": "https://github.com/uestla/ReCaptchaControl/issues"
1010
},
1111
"require": {
12-
"php": ">=5.6.0",
13-
"nette/di": "^2.4 || ^3.0",
14-
"nette/http": "^2.4 || ^3.0",
15-
"nette/forms": "^2.4 || ^3.0",
16-
"nette/utils": "^2.4 || ^3.0",
17-
"nette/php-generator": "^2.4 || ^3.0"
12+
"php": ">=7.1",
13+
"nette/di": "^3.0",
14+
"nette/http": "^3.0",
15+
"nette/forms": "^3.0",
16+
"nette/utils": "^3.0",
17+
"nette/php-generator": "^3.0"
1818
},
1919
"require-dev": {
20-
"php": ">= 7.0",
21-
"tracy/tracy": "^2.4",
22-
"latte/latte": "^2.4",
23-
"nette/tester": "^2.0",
24-
"nette/bootstrap": "^2.4",
20+
"tracy/tracy": "^2.6",
21+
"latte/latte": "^2.5",
22+
"nette/bootstrap": "^3.0",
23+
"nette/tester": "^2.2",
2524
"guzzlehttp/guzzle": "^6.3"
2625
},
2726
"suggest": {

src/ReCaptchaControl/Control.php

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @link https://github.com/uestla/ReCaptchaControl
99
*/
1010

11+
declare(strict_types = 1);
12+
1113
namespace ReCaptchaControl;
1214

1315
use Nette\Forms;
@@ -24,13 +26,7 @@ class Control extends Forms\Controls\BaseControl
2426
private $initialized = false;
2527

2628

27-
/**
28-
* @param Validator $validator
29-
* @param Renderer $renderer
30-
* @param string $caption
31-
* @param string $message
32-
*/
33-
public function __construct(Validator $validator, Renderer $renderer, $caption = null, $message = null)
29+
public function __construct(Validator $validator, Renderer $renderer, ?string $caption = null, ?string $message = null)
3430
{
3531
parent::__construct($caption);
3632

@@ -43,7 +39,6 @@ public function __construct(Validator $validator, Renderer $renderer, $caption =
4339
}
4440

4541

46-
/** @inheritdoc */
4742
public function addRule($validator, $message = null, $arg = null)
4843
{
4944
if ($this->initialized
@@ -55,8 +50,7 @@ public function addRule($validator, $message = null, $arg = null)
5550
}
5651

5752

58-
/** @return Html */
59-
public function getControl()
53+
public function getControl(): Html
6054
{
6155
$this->setOption('rendered', true);
6256
$el = clone $this->control;
@@ -66,30 +60,19 @@ public function getControl()
6660
}
6761

6862

69-
/** @return bool */
70-
public function isFilled()
63+
public function isFilled(): bool
7164
{
7265
return true;
7366
}
7467

7568

76-
/**
77-
* @param Control $control
78-
* @return bool
79-
*/
80-
public static function validateValid(Control $control)
69+
public static function validateValid(Control $control): bool
8170
{
8271
return $control->validator->validate();
8372
}
8473

8574

86-
/**
87-
* @param Validator $validator
88-
* @param Renderer $renderer
89-
* @param string $method
90-
* @return void
91-
*/
92-
public static function register(Validator $validator, Renderer $renderer, $method = 'addRecaptcha')
75+
public static function register(Validator $validator, Renderer $renderer, string $method = 'addRecaptcha'): void
9376
{
9477
Forms\Container::extensionMethod($method, function ($container, $name, $label = null, $message = null) use ($validator, $renderer) {
9578
return $container[$name] = new Control($validator, $renderer, $label, $message);

src/ReCaptchaControl/DI/Extension.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @link https://github.com/uestla/ReCaptchaControl
99
*/
1010

11+
declare(strict_types = 1);
12+
1113
namespace ReCaptchaControl\DI;
1214

1315
use Nette\DI\Statement;
@@ -44,8 +46,7 @@ class Extension extends CompilerExtension
4446
protected $prefix;
4547

4648

47-
/** @return void */
48-
public function loadConfiguration()
49+
public function loadConfiguration(): void
4950
{
5051
$config = $this->validateConfig($this->defaults);
5152
$builder = $this->getContainerBuilder();
@@ -76,11 +77,7 @@ public function loadConfiguration()
7677
}
7778

7879

79-
/**
80-
* @param ClassType $class
81-
* @return void
82-
*/
83-
public function afterCompile(ClassType $class)
80+
public function afterCompile(ClassType $class): void
8481
{
8582
$initialize = $class->getMethod('initialize');
8683
$config = $this->validateConfig($this->defaults);

src/ReCaptchaControl/Http/IRequestDataProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @link https://github.com/uestla/ReCaptchaControl
99
*/
1010

11+
declare(strict_types = 1);
12+
1113
namespace ReCaptchaControl\Http;
1214

1315

@@ -16,10 +18,8 @@ interface IRequestDataProvider
1618

1719
const RESPONSE_KEY = 'g-recaptcha-response';
1820

19-
/** @return string|null */
20-
public function getResponseValue();
21+
public function getResponseValue(): ?string;
2122

22-
/** @return string|null */
23-
public function getRemoteIP();
23+
public function getRemoteIP(): ?string;
2424

2525
}

src/ReCaptchaControl/Http/RequestDataProvider.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @link https://github.com/uestla/ReCaptchaControl
99
*/
1010

11+
declare(strict_types = 1);
12+
1113
namespace ReCaptchaControl\Http;
1214

1315
use Nette\Http\Request;
@@ -20,22 +22,19 @@ class RequestDataProvider implements IRequestDataProvider
2022
private $httpRequest;
2123

2224

23-
/** @param Request $httpRequest */
2425
public function __construct(Request $httpRequest)
2526
{
2627
$this->httpRequest = $httpRequest;
2728
}
2829

2930

30-
/** @inheritdoc */
31-
public function getResponseValue()
31+
public function getResponseValue(): ?string
3232
{
33-
return $this->httpRequest->getPost(self::RESPONSE_KEY, null);
33+
return $this->httpRequest->getPost(self::RESPONSE_KEY);
3434
}
3535

3636

37-
/** @inheritdoc */
38-
public function getRemoteIP()
37+
public function getRemoteIP(): ?string
3938
{
4039
return $this->httpRequest->getRemoteAddress();
4140
}

src/ReCaptchaControl/Http/Requester/CurlRequester.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @link https://github.com/uestla/ReCaptchaControl
99
*/
1010

11+
declare(strict_types = 1);
12+
1113
namespace ReCaptchaControl\Http\Requester;
1214

1315
use Nette\Utils\Strings;
@@ -20,7 +22,6 @@ class CurlRequester implements IRequester
2022
private $options;
2123

2224

23-
/** @param array $options */
2425
public function __construct(array $options = [])
2526
{
2627
if (!extension_loaded('curl')) {
@@ -31,8 +32,7 @@ public function __construct(array $options = [])
3132
}
3233

3334

34-
/** @inheritdoc */
35-
public function post($url, array $values = [])
35+
public function post(string $url, array $values = []): string
3636
{
3737
$ch = curl_init();
3838

@@ -49,23 +49,19 @@ public function post($url, array $values = [])
4949
$error = curl_error($ch);
5050
curl_close($ch);
5151

52-
if ($errno === 0) {
52+
if ($errno === 0 && is_string($response)) {
5353
return $response;
5454
}
5555

5656
throw RequestException::create($url, $error);
5757
}
5858

5959

60-
/**
61-
* @param array $options
62-
* @return array
63-
*/
64-
private static function processOptions(array $options)
60+
private static function processOptions(array $options): array
6561
{
6662
// NOTE: intentionally not using array_walk since array keys cannot be changed
6763
foreach ($options as $key => $val) {
68-
if (Strings::startsWith($key, 'CURLOPT_')) {
64+
if (Strings::startsWith((string) $key, 'CURLOPT_')) {
6965
unset($options[$key]);
7066
$options[constant($key)] = $val;
7167
}

src/ReCaptchaControl/Http/Requester/GuzzleRequester.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @link https://github.com/uestla/ReCaptchaControl
99
*/
1010

11+
declare(strict_types = 1);
12+
1113
namespace ReCaptchaControl\Http\Requester;
1214

1315
use GuzzleHttp\ClientInterface;
@@ -26,8 +28,7 @@ public function __construct(ClientInterface $client)
2628
}
2729

2830

29-
/** @inheritdoc */
30-
public function post($url, array $values = [])
31+
public function post(string $url, array $values = []): string
3132
{
3233
try {
3334
$response = $this->client->request('POST', $url, [

src/ReCaptchaControl/Http/Requester/IRequester.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @link https://github.com/uestla/ReCaptchaControl
99
*/
1010

11+
declare(strict_types = 1);
12+
1113
namespace ReCaptchaControl\Http\Requester;
1214

1315

@@ -17,11 +19,8 @@ interface IRequester
1719
/**
1820
* Performs a HTTP POST request with given $values
1921
*
20-
* @param string $url
21-
* @param array $values
22-
* @return string
2322
* @throws RequestException
2423
*/
25-
public function post($url, array $values = []);
24+
public function post(string $url, array $values = []): string;
2625

2726
}

src/ReCaptchaControl/Http/Requester/SimpleRequester.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
* @link https://github.com/uestla/ReCaptchaControl
99
*/
1010

11+
declare(strict_types = 1);
12+
1113
namespace ReCaptchaControl\Http\Requester;
1214

1315

1416
class SimpleRequester implements IRequester
1517
{
1618

17-
/** @inheritdoc */
18-
public function post($url, array $values = [])
19+
public function post(string $url, array $values = []): string
1920
{
2021
$context = stream_context_create([
2122
'http' => [

0 commit comments

Comments
 (0)