Skip to content

Commit

Permalink
Optimized LongCast and DoubleCast (#648)
Browse files Browse the repository at this point in the history
* Optimized LongCast and DoubleCast

* feat: Optimize LongCast and DoubleCast

---------

Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia authored May 28, 2024
1 parent 6ea1c69 commit 401f73b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/validated-dto/src/Casting/DoubleCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace FriendsOfHyperf\ValidatedDTO\Casting;

use FriendsOfHyperf\ValidatedDTO\Exception\CastException;
use Throwable;

class DoubleCast implements Castable
{
Expand All @@ -20,6 +21,10 @@ class DoubleCast implements Castable
*/
public function cast(string $property, mixed $value): float
{
return (float) $value;
try {
return (float) $value;
} catch (Throwable $e) { // @phpstan-ignore-line
throw new CastException($property);
}
}
}
7 changes: 6 additions & 1 deletion src/validated-dto/src/Casting/LongCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace FriendsOfHyperf\ValidatedDTO\Casting;

use FriendsOfHyperf\ValidatedDTO\Exception\CastException;
use Throwable;

class LongCast implements Castable
{
Expand All @@ -20,6 +21,10 @@ class LongCast implements Castable
*/
public function cast(string $property, mixed $value): int
{
return (int) $value;
try {
return (int) $value;
} catch (Throwable $e) { // @phpstan-ignore-line
throw new CastException($property);
}
}
}
12 changes: 11 additions & 1 deletion tests/ValidatedDTO/Unit/DoubleCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@
->cast(test_property(), '10.5')
->toBe(10.5);

it('throws exception when it is unable to cast property')
it('throws exception when it is string')
->expect(fn () => new DoubleCast())
->cast(test_property(), 'TEST')
->toBe(0.0);

it('throws exception when it is empty string')
->expect(fn () => new DoubleCast())
->cast(test_property(), '')
->toBe(0.0);

it('throws exception when it is unable to cast property')
->expect(fn () => new DoubleCast())
->cast(test_property(), new stdClass())
->throws(CastException::class, 'Unable to cast property: test_property - invalid value');
12 changes: 11 additions & 1 deletion tests/ValidatedDTO/Unit/LongCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@
->cast(test_property(), '5')
->toBe(5);

it('throws exception when it is unable to cast property')
it('throws exception when it is string')
->expect(fn () => new LongCast())
->cast(test_property(), 'TEST')
->toBe(0);

it('throws exception when it is empty string')
->expect(fn () => new LongCast())
->cast(test_property(), '')
->toBe(0);

it('throws exception when it is unable to cast property')
->expect(fn () => new LongCast())
->cast(test_property(), new stdClass())
->throws(CastException::class, 'Unable to cast property: test_property - invalid value');

0 comments on commit 401f73b

Please sign in to comment.