Skip to content

Commit 6f2887f

Browse files
committed
feat: add IntBoolCast
1 parent effcabe commit 6f2887f

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/Entities/Cast/IntBoolCast.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace CodeIgniter\Shield\Entities\Cast;
4+
5+
use CodeIgniter\Entity\Cast\BaseCast;
6+
7+
/**
8+
* Int Bool Cast
9+
*
10+
* DB column: int (0/1) <--> Class property: bool
11+
*/
12+
final class IntBoolCast extends BaseCast
13+
{
14+
/**
15+
* @param int $value
16+
*/
17+
public static function get($value, array $params = []): bool
18+
{
19+
return (bool) $value;
20+
}
21+
22+
/**
23+
* @param bool|int|string $value
24+
*/
25+
public static function set($value, array $params = []): int
26+
{
27+
return (int) $value;
28+
}
29+
}

src/Entities/Entity.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace CodeIgniter\Shield\Entities;
4+
5+
use CodeIgniter\Entity\Entity as FrameworkEntity;
6+
use CodeIgniter\Shield\Entities\Cast\IntBoolCast;
7+
8+
/**
9+
* Base Entity
10+
*/
11+
abstract class Entity extends FrameworkEntity
12+
{
13+
/**
14+
* Custom convert handlers
15+
*
16+
* @var array<string, string>
17+
* @phpstan-var array<string, class-string>
18+
*/
19+
protected $castHandlers = [
20+
'int_bool' => IntBoolCast::class,
21+
];
22+
}

0 commit comments

Comments
 (0)