Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add IntBoolCast for Entity #6348

Merged
merged 3 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions system/Entity/Cast/IntBoolCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Entity\Cast;

/**
* Int Bool Cast
*
* DB column: int (0/1) <--> Class property: bool
*/
final class IntBoolCast extends BaseCast
{
/**
* @param int $value
*/
public static function get($value, array $params = []): bool
{
return (bool) $value;
}

/**
* @param bool|int|string $value
*/
public static function set($value, array $params = []): int
{
return (int) $value;
}
}
2 changes: 2 additions & 0 deletions system/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use CodeIgniter\Entity\Cast\CSVCast;
use CodeIgniter\Entity\Cast\DatetimeCast;
use CodeIgniter\Entity\Cast\FloatCast;
use CodeIgniter\Entity\Cast\IntBoolCast;
use CodeIgniter\Entity\Cast\IntegerCast;
use CodeIgniter\Entity\Cast\JsonCast;
use CodeIgniter\Entity\Cast\ObjectCast;
Expand Down Expand Up @@ -80,6 +81,7 @@ class Entity implements JsonSerializable
'float' => FloatCast::class,
'int' => IntegerCast::class,
'integer' => IntegerCast::class,
'int-bool' => IntBoolCast::class,
'json' => JsonCast::class,
'object' => ObjectCast::class,
'string' => StringCast::class,
Expand Down
27 changes: 27 additions & 0 deletions tests/system/Entity/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,33 @@ public function testCastInteger()
$this->assertSame(3, $entity->first);
}

public function testCastIntBool()
{
$entity = new class () extends Entity {
protected $casts = [
'active' => 'int-bool',
];
};

$entity->setAttributes(['active' => '1']);

$this->assertTrue($entity->active);

$entity->setAttributes(['active' => '0']);

$this->assertFalse($entity->active);

$entity->active = true;

$this->assertTrue($entity->active);
$this->assertSame(['active' => 1], $entity->toRawArray());

$entity->active = false;

$this->assertFalse($entity->active);
$this->assertSame(['active' => 0], $entity->toRawArray());
}

public function testCastFloat()
{
$entity = $this->getCastEntity();
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Enhancements
- Now **Encryption** can decrypt data encrypted with CI3's Encryption. See :ref:`encryption-compatible-with-ci3`.
- Added method ``Timer::record()`` to measure performance in a callable. Also enhanced common function ``timer()`` to accept optional callable.
- Now ``spark routes`` command shows route names. See :ref:`URI Routing <routing-spark-routes>`.
- Added new :ref:`entities-property-casting` class ``IntBoolCast`` for Entity.

Changes
*******
Expand Down
4 changes: 3 additions & 1 deletion user_guide_src/source/models/entities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,16 @@ current timezone, as set in **app/Config/App.php**:

.. literalinclude:: entities/011.php

.. _entities-property-casting:

Property Casting
----------------

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

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