-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
898480c
commit 99f0dd5
Showing
15 changed files
with
367 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model\Casts; | ||
|
||
use BackedEnum; | ||
use Mongolid\Model\Casts\Exceptions\InvalidTypeException; | ||
|
||
class BackedEnumCast implements CastInterface | ||
{ | ||
/** | ||
* @param class-string<BackedEnum> $backedEnum | ||
*/ | ||
public function __construct( | ||
private string $backedEnum | ||
) { | ||
} | ||
|
||
|
||
/** | ||
* @param int|string|null $value | ||
*/ | ||
public function get(mixed $value): ?BackedEnum | ||
{ | ||
if (is_null($value)) { | ||
return null; | ||
} | ||
|
||
return ($this->backedEnum)::from($value); | ||
} | ||
|
||
/** | ||
* @param BackedEnum|string|int|null $value | ||
*/ | ||
public function set(mixed $value): string|int|null | ||
{ | ||
if (is_null($value)) { | ||
return null; | ||
} | ||
|
||
if (!$value instanceof $this->backedEnum) { | ||
throw new InvalidTypeException( | ||
"$this->backedEnum|null", | ||
$value | ||
); | ||
} | ||
|
||
return $value->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model\Casts\Exceptions; | ||
|
||
use InvalidArgumentException; | ||
use Mongolid\Model\Casts\CastResolver; | ||
|
||
class InvalidTypeException extends InvalidArgumentException | ||
{ | ||
public function __construct(string $expectedType, mixed $value) | ||
{ | ||
$invalidType = is_object($value) | ||
? $value::class | ||
: gettype($value); | ||
|
||
parent::__construct("Value expected type $expectedType, given $invalidType"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Mongolid\Tests\Integration; | ||
|
||
use Mongolid\Tests\Stubs\Box; | ||
use Mongolid\Tests\Stubs\Legacy\Box as LegacyBox; | ||
use Mongolid\Tests\Stubs\Size; | ||
|
||
/** | ||
* @requires PHP >= 8.1 | ||
*/ | ||
class BackedEnumCastTest extends IntegrationTestCase | ||
{ | ||
public function testShouldCreateAndSaveBoxWithCastedAttributes(): void | ||
{ | ||
// Set | ||
$box = new Box(); | ||
$box->box_size = Size::Big; | ||
|
||
// Actions | ||
$box->save(); | ||
|
||
// Assertions | ||
$this->assertEquals(Size::Big, $box->box_size); | ||
$this->assertEquals( | ||
'big', | ||
$box->getOriginalDocumentAttributes()['box_size'] | ||
); | ||
} | ||
|
||
public function testShouldUpdateBoxWithCastedAttributes(): void | ||
{ | ||
// Set | ||
$box = new Box(); | ||
$box->box_size = Size::Small; | ||
|
||
// Actions | ||
$box->update(); | ||
|
||
// Assertions | ||
$this->assertEquals(Size::Small, $box->box_size); | ||
$this->assertEquals( | ||
'small', | ||
$box->getOriginalDocumentAttributes()['box_size'] | ||
); | ||
} | ||
|
||
public function testShouldCreateAndSaveLegacyBoxWithCastedAttributes(): void | ||
{ | ||
// Set | ||
$legacyBox = new LegacyBox(); | ||
$legacyBox->box_size = Size::Big; | ||
|
||
// Actions | ||
$legacyBox->save(); | ||
|
||
// Assertions | ||
$this->assertEquals(Size::Big, $legacyBox->box_size); | ||
$this->assertEquals( | ||
'big', | ||
$legacyBox->getOriginalDocumentAttributes()['box_size'] | ||
); | ||
} | ||
|
||
public function testShouldUpdateLegacyBoxWithCastedAttributes(): void | ||
{ | ||
// Set | ||
$legacyBox = new LegacyBox(); | ||
$legacyBox->box_size = Size::Small; | ||
|
||
// Actions | ||
$legacyBox->save(); | ||
|
||
// Assertions | ||
$this->assertEquals(Size::Small, $legacyBox->box_size); | ||
$this->assertEquals( | ||
'small', | ||
$legacyBox->getOriginalDocumentAttributes()['box_size'] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Mongolid\Tests\Stubs; | ||
|
||
use Mongolid\Model\AbstractModel; | ||
|
||
class Box extends AbstractModel | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $collection = 'boxes'; | ||
|
||
protected array $casts = [ | ||
'box_size' => Size::class, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Mongolid\Tests\Stubs\Legacy; | ||
|
||
use Mongolid\LegacyRecord; | ||
use Mongolid\Tests\Stubs\Size; | ||
|
||
class Box extends LegacyRecord | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $collection = 'boxes'; | ||
|
||
protected array $casts = [ | ||
'box_size' => Size::class, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Mongolid\Tests\Stubs; | ||
|
||
enum Size: string | ||
{ | ||
case Small = 'small'; | ||
case Big = 'big'; | ||
} |
Oops, something went wrong.