Skip to content

Commit 5280d9f

Browse files
committed
Moved toFloat() to Helper class.
1 parent 62aaed9 commit 5280d9f

File tree

3 files changed

+123
-11
lines changed

3 files changed

+123
-11
lines changed

src/Omnipay/Common/Helper.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
namespace Omnipay\Common;
77

8+
use InvalidArgumentException;
9+
810
/**
911
* Helper class
1012
*
@@ -115,4 +117,30 @@ public static function getGatewayClassName($shortName)
115117

116118
return '\\Omnipay\\'.$shortName.'Gateway';
117119
}
120+
121+
/**
122+
* Convert an amount into a float.
123+
* The float datatype can then be converted into the string
124+
* format that the remote gateway requies.
125+
*
126+
* @var string|int|float $value The value to convert.
127+
* @throws InvalidArgumentException on a validation failure.
128+
* @return float The amount converted to a float.
129+
*/
130+
131+
public static function toFloat($value)
132+
{
133+
if (!is_string($value) && !is_int($value) && !is_float($value)) {
134+
throw new InvalidArgumentException('Data type is not a valid decimal number.');
135+
}
136+
137+
if (is_string($value)) {
138+
// Validate generic number, with optional sign and decimals.
139+
if (!preg_match('/^[-]?[0-9]+(\.[0-9]*)?$/', $value)) {
140+
throw new InvalidArgumentException('String is not a valid decimal number.');
141+
}
142+
}
143+
144+
return (float)$value;
145+
}
118146
}

src/Omnipay/Common/Message/AbstractRequest.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Omnipay\Common\ItemBag;
1515
use Symfony\Component\HttpFoundation\ParameterBag;
1616
use Symfony\Component\HttpFoundation\Request as HttpRequest;
17+
use InvalidArgumentException;
1718

1819
/**
1920
* Abstract Request
@@ -290,18 +291,12 @@ public function setCardReference($value)
290291

291292
public function toFloat($value)
292293
{
293-
if (!is_string($value) && ! is_int($value) && ! is_float($value)) {
294-
throw new InvalidRequestException('Data type is not a valid decimal number.');
294+
try {
295+
return Helper::toFloat($value);
296+
} catch (InvalidArgumentException $e) {
297+
// Throw old exception for legacy implementations.
298+
throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e);
295299
}
296-
297-
if (is_string($value)) {
298-
// Validate generic number, with optional sign and decimals.
299-
if (!preg_match('/^[-]?[0-9]+(\.[0-9]*)?$/', $value)) {
300-
throw new InvalidRequestException('String is not a valid decimal number.');
301-
}
302-
}
303-
304-
return (float)$value;
305300
}
306301

307302
/**

tests/Omnipay/Common/HelperTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,93 @@ public function testGetGatewayClassNameUnderscoreNamespace()
134134
$class = Helper::getGatewayClassName('PayPal_Express');
135135
$this->assertEquals('\\Omnipay\\PayPal\\ExpressGateway', $class);
136136
}
137+
138+
/**
139+
* Some valid toFloat() inputs.
140+
*/
141+
public function testToFloatFromFloat()
142+
{
143+
$shortName = Helper::toFloat(1.99);
144+
$this->assertSame(1.99, $shortName);
145+
}
146+
147+
public function testToFloatFromInt()
148+
{
149+
$shortName = Helper::toFloat(199);
150+
$this->assertSame(199.0, $shortName);
151+
}
152+
153+
public function testToFloatFromStringDecimal()
154+
{
155+
$shortName = Helper::toFloat("1.99");
156+
$this->assertSame(1.99, $shortName);
157+
}
158+
159+
public function testToFloatFromStringRedunantZeroes()
160+
{
161+
$shortName = Helper::toFloat("000009.99900000000");
162+
$this->assertSame(9.999, $shortName);
163+
}
164+
165+
public function testToFloatFromStringEmptyDecimal()
166+
{
167+
$shortName = Helper::toFloat("1.");
168+
$this->assertSame(1.0, $shortName);
169+
}
170+
171+
public function testToFloatFromStringInt()
172+
{
173+
$shortName = Helper::toFloat("199");
174+
$this->assertSame(199.0, $shortName);
175+
}
176+
177+
public function testToFloatFromStringIntNegative()
178+
{
179+
$shortName = Helper::toFloat("-199");
180+
$this->assertSame(-199.0, $shortName);
181+
}
182+
183+
/**
184+
* Some invalid toFloat() inputs.
185+
*/
186+
187+
/**
188+
* The number MUST always start with a digit.
189+
* This is arguably an arbitrary rule that perhaps does not need
190+
* to be enforced.
191+
*
192+
* @expectedException \InvalidArgumentException
193+
* @expectedExceptionMessage String is not a valid decimal number
194+
*/
195+
public function testToFloatFromStringEmptyIntegerPart()
196+
{
197+
$shortName = Helper::toFloat(".99");
198+
}
199+
200+
/**
201+
* @expectedException \InvalidArgumentException
202+
* @expectedExceptionMessage String is not a valid decimal number
203+
*/
204+
public function testToFloatFromStringTwoDecimalPoints()
205+
{
206+
$shortName = Helper::toFloat("1.99.");
207+
}
208+
209+
/**
210+
* @expectedException \InvalidArgumentException
211+
* @expectedExceptionMessage String is not a valid decimal number
212+
*/
213+
public function testToFloatFromStringWrongDecimalPoints()
214+
{
215+
$shortName = Helper::toFloat("1,99");
216+
}
217+
218+
/**
219+
* @expectedException \InvalidArgumentException
220+
* @expectedExceptionMessage Data type is not a valid decimal number
221+
*/
222+
public function testToFloatFromBoolean()
223+
{
224+
$shortName = Helper::toFloat(false);
225+
}
137226
}

0 commit comments

Comments
 (0)