Skip to content

Commit a5880c8

Browse files
committed
Renamed methods for floats.
1 parent 3608d86 commit a5880c8

File tree

4 files changed

+186
-183
lines changed

4 files changed

+186
-183
lines changed

README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,18 @@ The class `SetBased\Helper\Cast` has the following methods for testing mixed val
8787
The class `SetBased\Abc\Helper\Cast` has the methods shown in the table below for casting mixed values to a primitive
8888
data type. When a value can not cast an exception will be thrown.
8989

90-
| Method | Null Value | Return Type |
91-
|-------------------|----------------------|--------------|
92-
| toManBool | throws an exception | bool |
93-
| toManFloat | throws an exception | float |
94-
| toManFiniteFloat | throws an exception | float |
95-
| toManInt | throws an exception | int |
96-
| toManString | throws an exception | string |
97-
| toOptBool | returns null | bool\|null |
98-
| toOptFloat | returns null | float\|null |
99-
| toOptFiniteFloat | returns null | float\|null |
100-
| toOptInt | returns null | int\|null |
101-
| toOptString | returns null | string\|null |
90+
| Method | Null Value | Return Type |
91+
|---------------------|----------------------|--------------|
92+
| toManBool | throws an exception | bool |
93+
| toManFloat | throws an exception | float |
94+
| toManFloatInclusive | throws an exception | float |
95+
| toManInt | throws an exception | int |
96+
| toManString | throws an exception | string |
97+
| toOptBool | returns null | bool\|null |
98+
| toOptFloat | returns null | float\|null |
99+
| toOptFloatInclusive | returns null | float\|null |
100+
| toOptInt | returns null | int\|null |
101+
| toOptString | returns null | string\|null |
102102

103103
Remarks:
104104
* 'opt' is short for optional: `null` values are valid. Testing and casting against `null` yields `true` and `null`, respectively.
@@ -107,6 +107,7 @@ Remarks:
107107
## Sample
108108

109109
Code:
110+
110111
```php
111112
<?php
112113
declare(strict_types=1);

src/Cast.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static function isManBool(mixed $value): bool
2929
*
3030
* @return bool
3131
*/
32-
public static function isManFiniteFloat(mixed $value): bool
32+
public static function isManFloat(mixed $value): bool
3333
{
3434
switch (gettype($value))
3535
{
@@ -60,13 +60,13 @@ public static function isManFiniteFloat(mixed $value): bool
6060

6161
//--------------------------------------------------------------------------------------------------------------------
6262
/**
63-
* Returns whether a value is not null and can be cast to a float.
63+
* Returns whether a value is not null and can be cast to a float including NaN, -INF, and INF.
6464
*
6565
* @param mixed $value The value.
6666
*
6767
* @return bool
6868
*/
69-
public static function isManFloat(mixed $value): bool
69+
public static function isManFloatInclusive(mixed $value): bool
7070
{
7171
switch (gettype($value))
7272
{
@@ -169,22 +169,22 @@ public static function isOptBool(mixed $value): bool
169169
*
170170
* @return bool
171171
*/
172-
public static function isOptFiniteFloat(mixed $value): bool
172+
public static function isOptFloat(mixed $value): bool
173173
{
174-
return $value===null || static::isManFiniteFloat($value);
174+
return $value===null || static::isManFloat($value);
175175
}
176176

177177
//--------------------------------------------------------------------------------------------------------------------
178178
/**
179-
* Returns whether a value is null or can be cast to a float.
179+
* Returns whether a value is null or can be cast to a float including NaN, -INF, and INF.
180180
*
181181
* @param mixed $value The value.
182182
*
183183
* @return bool
184184
*/
185-
public static function isOptFloat(mixed $value): bool
185+
public static function isOptFloatInclusive(mixed $value): bool
186186
{
187-
return $value===null || static::isManFloat($value);
187+
return $value===null || static::isManFloatInclusive($value);
188188
}
189189

190190
//--------------------------------------------------------------------------------------------------------------------
@@ -255,7 +255,7 @@ public static function toManBool(mixed $value, ?bool $default = null): bool
255255
*
256256
* @throws InvalidCastException
257257
*/
258-
public static function toManFiniteFloat(mixed $value, ?float $default = null): float
258+
public static function toManFloat(mixed $value, ?float $default = null): float
259259
{
260260
if ($value===null && $default!==null)
261261
{
@@ -267,7 +267,7 @@ public static function toManFiniteFloat(mixed $value, ?float $default = null): f
267267
return $default;
268268
}
269269

270-
if (static::isManFiniteFloat($value)===false)
270+
if (static::isManFloat($value)===false)
271271
{
272272
throw new InvalidCastException('Value can not be converted to finite float');
273273
}
@@ -277,7 +277,8 @@ public static function toManFiniteFloat(mixed $value, ?float $default = null): f
277277

278278
//--------------------------------------------------------------------------------------------------------------------
279279
/**
280-
* Converts a value to a float. If the value can not be safely cast to a float throws an exception.
280+
* Converts a value to a float including NaN, -INF, and INF. If the value can not be safely cast to a float throws an
281+
* exception.
281282
*
282283
* @param mixed $value The value.
283284
* @param float|null $default The default value. If the value is null and the default is not null the default value
@@ -287,14 +288,14 @@ public static function toManFiniteFloat(mixed $value, ?float $default = null): f
287288
*
288289
* @throws InvalidCastException
289290
*/
290-
public static function toManFloat(mixed $value, ?float $default = null): float
291+
public static function toManFloatInclusive(mixed $value, ?float $default = null): float
291292
{
292293
if ($value===null && $default!==null)
293294
{
294295
return $default;
295296
}
296297

297-
if (static::isManFloat($value)===false)
298+
if (static::isManFloatInclusive($value)===false)
298299
{
299300
throw new InvalidCastException('Value can not be converted to float');
300301
}
@@ -393,7 +394,7 @@ public static function toOptBool(mixed $value, ?bool $default = null): ?bool
393394
*
394395
* @return float|null
395396
*/
396-
public static function toOptFiniteFloat(mixed $value, ?float $default = null): ?float
397+
public static function toOptFloat(mixed $value, ?float $default = null): ?float
397398
{
398399
if ($value===null)
399400
{
@@ -405,26 +406,27 @@ public static function toOptFiniteFloat(mixed $value, ?float $default = null): ?
405406
return $default;
406407
}
407408

408-
return static::toManFiniteFloat($value);
409+
return static::toManFloat($value);
409410
}
410411

411412
//--------------------------------------------------------------------------------------------------------------------
412413
/**
413-
* Converts a value to a float. If the value can not be safely cast to a float throws an exception.
414+
* Converts a value to a float including NaN, -INF, and INF. If the value can not be safely cast to a float throws an
415+
* exception.
414416
*
415417
* @param mixed $value The value.
416418
* @param float|null $default The default value. If the value is null the default value will be returned.
417419
*
418420
* @return float|null
419421
*/
420-
public static function toOptFloat(mixed $value, ?float $default = null): ?float
422+
public static function toOptFloatInclusive(mixed $value, ?float $default = null): ?float
421423
{
422424
if ($value===null)
423425
{
424426
return $default;
425427
}
426428

427-
return static::toManFloat($value);
429+
return static::toManFloatInclusive($value);
428430
}
429431

430432
//--------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)