Skip to content

Commit

Permalink
refact: remove static methods from cast contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielgomes94 committed Oct 18, 2023
1 parent 60406ab commit 55acc80
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/Model/Casts/CastInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

interface CastInterface
{
public static function get(mixed $value): mixed;
public function get(mixed $value): mixed;

public static function set(mixed $value): mixed;
public function set(mixed $value): mixed;
}
4 changes: 2 additions & 2 deletions src/Model/Casts/DateTime/BaseDateTimeCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ abstract class BaseDateTimeCast implements CastInterface
* @param UTCDateTime|null $value
* @return DateTimeInterface|null
*/
abstract public static function get(mixed $value): mixed;
abstract public function get(mixed $value): mixed;

/**
* @param DateTimeInterface|UTCDateTimeInterface|null $value
* @return UTCDateTime|null
*/
public static function set(mixed $value): mixed
public function set(mixed $value): mixed
{
if (is_null($value)) {
return null;
Expand Down
3 changes: 1 addition & 2 deletions src/Model/Casts/DateTime/DateTimeCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ class DateTimeCast extends BaseDateTimeCast
* @param UTCDateTime|null $value
* @return DateTime|null
*/
public static function get(mixed $value): mixed
public function get(mixed $value): mixed
{
if (is_null($value)) {

return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Model/Casts/DateTime/ImmutableDateTimeCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ImmutableDateTimeCast extends BaseDateTimeCast
* @param UTCDateTime|null $value
* @return DateTimeImmutable|null
*/
public static function get(mixed $value): mixed
public function get(mixed $value): mixed
{
if (is_null($value)) {
return null;
Expand Down
7 changes: 4 additions & 3 deletions tests/Unit/Model/Casts/DateTime/BaseDateTimeCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ public function testShouldSet(): void
{
// Set
$dateInDateTime = DateTime::createFromFormat('d/m/Y H:i:s', '08/10/2025 12:30:45');
$dateTimeCast = new DateTimeCast();

// Actions
$expires_at = DateTimeCast::set($dateInDateTime);
$nulled_at = DateTimeCast::set(null);
$restored_at = DateTimeCast::set(
$expires_at = $dateTimeCast->set($dateInDateTime);
$nulled_at = $dateTimeCast->set(null);
$restored_at = $dateTimeCast->set(
new UTCDateTime($dateInDateTime)
);

Expand Down
7 changes: 4 additions & 3 deletions tests/Unit/Model/Casts/DateTime/DateTimeCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ public function testShouldGet(): void
$timestamp = new UTCDateTime(
DateTime::createFromFormat('d/m/Y H:i:s', '08/10/2025 12:30:45')
);
$dateTimeCast = new DateTimeCast();

// Actions
$revoked_at = DateTimeCast::get(null);
$expires_at = DateTimeCast::get($timestamp);
$validated_at = DateTimeCast::get($timestamp);
$revoked_at = $dateTimeCast->get(null);
$expires_at = $dateTimeCast->get($timestamp);
$validated_at = $dateTimeCast->get($timestamp);

// Assertions
$this->assertNull($revoked_at);
Expand Down
7 changes: 4 additions & 3 deletions tests/Unit/Model/Casts/DateTime/ImmutableDateTimeCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ public function testShouldGet(): void
$timestamp = new UTCDateTime(
DateTimeImmutable::createFromFormat('d/m/Y H:i:s', '08/10/2025 12:30:45')
);
$immutableDateTimeCast = new ImmutableDateTimeCast();

// Actions
$revoked_at = ImmutableDateTimeCast::get(null);
$birthdate = ImmutableDateTimeCast::get($timestamp);
$created_at = ImmutableDateTimeCast::get($timestamp);
$revoked_at = $immutableDateTimeCast->get(null);
$birthdate = $immutableDateTimeCast->get($timestamp);
$created_at = $immutableDateTimeCast->get($timestamp);

// Assertions
$this->assertNull($revoked_at);
Expand Down

0 comments on commit 55acc80

Please sign in to comment.