Skip to content
This repository was archived by the owner on Apr 7, 2019. It is now read-only.

Commit 9b2e76c

Browse files
Merge pull request #11 from AltThree/fix-types
Fix types
2 parents a1f48d3 + e8a290c commit 9b2e76c

File tree

3 files changed

+141
-2
lines changed

3 files changed

+141
-2
lines changed

src/ThrottlingMiddleware.php

+28-2
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,42 @@ public function __construct(RateLimiter $limiter)
5555
*
5656
* @param \Illuminate\Http\Request $request
5757
* @param \Closure $next
58+
* @param int|string $limit
59+
* @param int|float|string $decay
60+
* @param bool|string $global
61+
* @param bool|string $headers
62+
*
63+
* @throws \AltThree\Throttle\ThrottlingException
64+
*
65+
* @return mixed
66+
*/
67+
public function handle(Request $request, Closure $next, $limit = 60, $decay = 1, $global = false, $headers = true)
68+
{
69+
return $this->safeHandle(
70+
$request,
71+
$next,
72+
TypeUtil::convertNumeric($limit),
73+
TypeUtil::convertNumeric($decay),
74+
TypeUtil::convertBoolean($global),
75+
TypeUtil::convertBoolean($headers)
76+
);
77+
}
78+
79+
/**
80+
* Handle an incoming request, with correct types.
81+
*
82+
* @param \Illuminate\Http\Request $request
83+
* @param \Closure $next
5884
* @param int $limit
59-
* @param float|int $decay
85+
* @param int|float $decay
6086
* @param bool $global
6187
* @param bool $headers
6288
*
6389
* @throws \AltThree\Throttle\ThrottlingException
6490
*
6591
* @return mixed
6692
*/
67-
public function handle(Request $request, Closure $next, $limit = 60, $decay = 1, $global = false, $headers = true)
93+
protected function safeHandle(Request $request, Closure $next, int $limit, $decay, bool $global, bool $headers)
6894
{
6995
if ($this->shouldPassThrough($request)) {
7096
return $next($request);

src/TypeUtil.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Alt Three Throttle.
7+
*
8+
* (c) Alt Three Services Limited
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace AltThree\Throttle;
15+
16+
/**
17+
* This is the type util class.
18+
*
19+
* @author Graham Campbell <[email protected]>
20+
*/
21+
class TypeUtil
22+
{
23+
/**
24+
* Convert the given value to numeric.
25+
*
26+
* @param int|float|string $v
27+
*
28+
* @return int|float
29+
*/
30+
public static function convertNumeric($v)
31+
{
32+
return $v === '' ? 0 : $v + 0;
33+
}
34+
35+
/**
36+
* Convert the given value to boolean.
37+
*
38+
* @param bool|string $v
39+
*
40+
* @return bool
41+
*/
42+
public static function convertBoolean($v)
43+
{
44+
return $v === 'false' ? false : (bool) $v;
45+
}
46+
}

tests/TypeUtilTest.php

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Alt Three Throttle.
7+
*
8+
* (c) Alt Three Services Limited
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace AltThree\Tests\Throttle;
15+
16+
use AltThree\Throttle\TypeUtil;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* This is the analysis test class.
21+
*
22+
* @author Graham Campbell <[email protected]>
23+
*/
24+
class TypeUtilTest extends TestCase
25+
{
26+
/**
27+
* @dataProvider providesNumericCases
28+
*/
29+
public function testConvertNumeric($input, $output)
30+
{
31+
$this->assertSame($output, TypeUtil::convertNumeric($input));
32+
}
33+
34+
public function providesNumericCases()
35+
{
36+
return [
37+
['', 0],
38+
['0', 0],
39+
['0.0', 0.0],
40+
['1', 1],
41+
['123', 123],
42+
[2, 2],
43+
[3.3, 3.3],
44+
];
45+
}
46+
47+
/**
48+
* @dataProvider providesBooleanCases
49+
*/
50+
public function testConvertBoolean($input, $output)
51+
{
52+
$this->assertSame($output, TypeUtil::convertBoolean($input));
53+
}
54+
55+
public function providesBooleanCases()
56+
{
57+
return [
58+
['', false],
59+
['false', false],
60+
['true', true],
61+
['0', false],
62+
['1', true],
63+
[false, false],
64+
[true, true],
65+
];
66+
}
67+
}

0 commit comments

Comments
 (0)