Skip to content

Commit 4649b0a

Browse files
authored
Merge pull request #6348 from kenjis/feat-int-bool-cast
feat: add IntBoolCast for Entity
2 parents 0a23584 + dd077aa commit 4649b0a

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

system/Entity/Cast/IntBoolCast.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Entity\Cast;
13+
14+
/**
15+
* Int Bool Cast
16+
*
17+
* DB column: int (0/1) <--> Class property: bool
18+
*/
19+
final class IntBoolCast extends BaseCast
20+
{
21+
/**
22+
* @param int $value
23+
*/
24+
public static function get($value, array $params = []): bool
25+
{
26+
return (bool) $value;
27+
}
28+
29+
/**
30+
* @param bool|int|string $value
31+
*/
32+
public static function set($value, array $params = []): int
33+
{
34+
return (int) $value;
35+
}
36+
}

system/Entity/Entity.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Entity\Cast\CSVCast;
1818
use CodeIgniter\Entity\Cast\DatetimeCast;
1919
use CodeIgniter\Entity\Cast\FloatCast;
20+
use CodeIgniter\Entity\Cast\IntBoolCast;
2021
use CodeIgniter\Entity\Cast\IntegerCast;
2122
use CodeIgniter\Entity\Cast\JsonCast;
2223
use CodeIgniter\Entity\Cast\ObjectCast;
@@ -80,6 +81,7 @@ class Entity implements JsonSerializable
8081
'float' => FloatCast::class,
8182
'int' => IntegerCast::class,
8283
'integer' => IntegerCast::class,
84+
'int-bool' => IntBoolCast::class,
8385
'json' => JsonCast::class,
8486
'object' => ObjectCast::class,
8587
'string' => StringCast::class,

tests/system/Entity/EntityTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,33 @@ public function testCastInteger()
293293
$this->assertSame(3, $entity->first);
294294
}
295295

296+
public function testCastIntBool()
297+
{
298+
$entity = new class () extends Entity {
299+
protected $casts = [
300+
'active' => 'int-bool',
301+
];
302+
};
303+
304+
$entity->setAttributes(['active' => '1']);
305+
306+
$this->assertTrue($entity->active);
307+
308+
$entity->setAttributes(['active' => '0']);
309+
310+
$this->assertFalse($entity->active);
311+
312+
$entity->active = true;
313+
314+
$this->assertTrue($entity->active);
315+
$this->assertSame(['active' => 1], $entity->toRawArray());
316+
317+
$entity->active = false;
318+
319+
$this->assertFalse($entity->active);
320+
$this->assertSame(['active' => 0], $entity->toRawArray());
321+
}
322+
296323
public function testCastFloat()
297324
{
298325
$entity = $this->getCastEntity();

user_guide_src/source/changelogs/v4.3.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Enhancements
5959
- Now **Encryption** can decrypt data encrypted with CI3's Encryption. See :ref:`encryption-compatible-with-ci3`.
6060
- Added method ``Timer::record()`` to measure performance in a callable. Also enhanced common function ``timer()`` to accept optional callable.
6161
- Now ``spark routes`` command shows route names. See :ref:`URI Routing <routing-spark-routes>`.
62+
- Added new :ref:`entities-property-casting` class ``IntBoolCast`` for Entity.
6263

6364
Changes
6465
*******

user_guide_src/source/models/entities.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,16 @@ current timezone, as set in **app/Config/App.php**:
187187

188188
.. literalinclude:: entities/011.php
189189

190+
.. _entities-property-casting:
191+
190192
Property Casting
191193
----------------
192194

193195
You can specify that properties in your Entity should be converted to common data types with the ``$casts`` property.
194196
This option should be an array where the key is the name of the class property, and the value is the data type it
195197
should be cast to. Casting only affects when values are read. No conversions happen that affect the permanent value in
196198
either the entity or the database. Properties can be cast to any of the following data types:
197-
**integer**, **float**, **double**, **string**, **boolean**, **object**, **array**, **datetime**, **timestamp**, and **uri**.
199+
**integer**, **float**, **double**, **string**, **boolean**, **object**, **array**, **datetime**, **timestamp**, **uri** and **int-bool**.
198200
Add a question mark at the beginning of type to mark property as nullable, i.e., **?string**, **?integer**.
199201

200202
For example, if you had a User entity with an ``is_banned`` property, you can cast it as a boolean:

0 commit comments

Comments
 (0)