Skip to content

Commit 038a157

Browse files
committed
Add URI cast
1 parent a4c88d3 commit 038a157

4 files changed

Lines changed: 101 additions & 42 deletions

File tree

system/Entity/Cast/URICast.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Entity\Cast;
13+
14+
use CodeIgniter\HTTP\URI;
15+
16+
/**
17+
* Class URICast
18+
*/
19+
class URICast extends BaseCast
20+
{
21+
/**
22+
* @inheritDoc
23+
*/
24+
public static function get($value, array $params = []): object
25+
{
26+
return $value instanceof URI ? $value : new URI($value);
27+
}
28+
}

system/Entity/Entity.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use CodeIgniter\Entity\Cast\ObjectCast;
2323
use CodeIgniter\Entity\Cast\StringCast;
2424
use CodeIgniter\Entity\Cast\TimestampCast;
25+
use CodeIgniter\Entity\Cast\URICast;
2526
use CodeIgniter\Entity\Exceptions\CastException;
2627
use CodeIgniter\I18n\Time;
2728
use Exception;
@@ -82,6 +83,7 @@ class Entity implements JsonSerializable
8283
'object' => ObjectCast::class,
8384
'string' => StringCast::class,
8485
'timestamp' => TimestampCast::class,
86+
'uri' => URICast::class,
8587
];
8688

8789
/**
@@ -226,7 +228,6 @@ public function toRawArray(bool $onlyChanged = false, bool $recursive = false):
226228
}
227229

228230
return $value;
229-
230231
}, $this->attributes);
231232
}
232233

@@ -305,7 +306,7 @@ public function hasChanged(string $key = null): bool
305306
/**
306307
* Set raw data array without any mutations
307308
*
308-
* @param array $data
309+
* @param array $data
309310
*
310311
* @return $this
311312
*/
@@ -461,7 +462,7 @@ public function jsonSerialize()
461462
/**
462463
* Change the value of the private $_cast property
463464
*
464-
* @param boolean|null $cast
465+
* @param boolean|null $cast
465466
*
466467
* @return boolean|Entity
467468
*/
@@ -509,7 +510,7 @@ public function __set(string $key, $value = null)
509510
// insert this value. should be outside $isNullable check,
510511
// so maybe wants to do sth with null value automatically
511512
$method = 'set' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key)));
512-
513+
513514
if (method_exists($this, $method))
514515
{
515516
$this->$method($value);
@@ -603,7 +604,7 @@ public function __isset(string $key): bool
603604
* Unsets an attribute property.
604605
*
605606
* @param string $key
606-
*
607+
*
607608
* @return void
608609
*/
609610
public function __unset(string $key): void

tests/system/EntityTest.php

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use CodeIgniter\Entity\Exceptions\CastException;
66
use CodeIgniter\I18n\Time;
7+
use CodeIgniter\HTTP\URI;
78
use CodeIgniter\Test\CIUnitTestCase;
89
use CodeIgniter\Test\ReflectionHelper;
910
use DateTime;
@@ -497,6 +498,32 @@ public function testCastNullable()
497498

498499
//--------------------------------------------------------------------
499500

501+
public function testCastURI()
502+
{
503+
$entity = $this->getCastEntity();
504+
505+
$data = 'https://codeigniter.com/banana';
506+
507+
$entity->thirteenth = $data;
508+
$this->assertInstanceOf(URI::class, $entity->thirteenth);
509+
$this->assertSame($data, (string) $entity->thirteenth);
510+
$this->assertSame('/banana', $entity->thirteenth->getPath());
511+
}
512+
513+
public function testURICastURI()
514+
{
515+
$entity = $this->getCastEntity();
516+
517+
$data = 'https://codeigniter.com/banana';
518+
519+
$entity->thirteenth = new URI($data);
520+
$this->assertInstanceOf(URI::class, $entity->thirteenth);
521+
$this->assertSame($data, (string) $entity->thirteenth);
522+
$this->assertSame('/banana', $entity->thirteenth->getPath());
523+
}
524+
525+
//--------------------------------------------------------------------
526+
500527
public function testCastAsJSON()
501528
{
502529
$entity = $this->getCastEntity();
@@ -1050,49 +1077,52 @@ protected function getCastEntity($data = null) : Entity
10501077
return new class($data) extends Entity
10511078
{
10521079
protected $attributes = [
1053-
'first' => null,
1054-
'second' => null,
1055-
'third' => null,
1056-
'fourth' => null,
1057-
'fifth' => null,
1058-
'sixth' => null,
1059-
'seventh' => null,
1060-
'eighth' => null,
1061-
'ninth' => null,
1062-
'tenth' => null,
1063-
'eleventh' => null,
1064-
'twelfth' => null,
1080+
'first' => null,
1081+
'second' => null,
1082+
'third' => null,
1083+
'fourth' => null,
1084+
'fifth' => null,
1085+
'sixth' => null,
1086+
'seventh' => null,
1087+
'eighth' => null,
1088+
'ninth' => null,
1089+
'tenth' => null,
1090+
'eleventh' => null,
1091+
'twelfth' => null,
1092+
'thirteenth' => null,
10651093
];
10661094

10671095
protected $_original = [
1068-
'first' => null,
1069-
'second' => null,
1070-
'third' => null,
1071-
'fourth' => null,
1072-
'fifth' => null,
1073-
'sixth' => null,
1074-
'seventh' => null,
1075-
'eighth' => null,
1076-
'ninth' => null,
1077-
'tenth' => null,
1078-
'eleventh' => null,
1079-
'twelfth' => null,
1096+
'first' => null,
1097+
'second' => null,
1098+
'third' => null,
1099+
'fourth' => null,
1100+
'fifth' => null,
1101+
'sixth' => null,
1102+
'seventh' => null,
1103+
'eighth' => null,
1104+
'ninth' => null,
1105+
'tenth' => null,
1106+
'eleventh' => null,
1107+
'twelfth' => null,
1108+
'thirteenth' => null,
10801109
];
10811110

10821111
// 'bar' is db column, 'foo' is internal representation
10831112
protected $casts = [
1084-
'first' => 'integer',
1085-
'second' => 'float',
1086-
'third' => 'double',
1087-
'fourth' => 'string',
1088-
'fifth' => 'boolean',
1089-
'sixth' => 'object',
1090-
'seventh' => 'array',
1091-
'eighth' => 'datetime',
1092-
'ninth' => 'timestamp',
1093-
'tenth' => 'json',
1094-
'eleventh' => 'json-array',
1095-
'twelfth' => 'csv',
1113+
'first' => 'integer',
1114+
'second' => 'float',
1115+
'third' => 'double',
1116+
'fourth' => 'string',
1117+
'fifth' => 'boolean',
1118+
'sixth' => 'object',
1119+
'seventh' => 'array',
1120+
'eighth' => 'datetime',
1121+
'ninth' => 'timestamp',
1122+
'tenth' => 'json',
1123+
'eleventh' => 'json-array',
1124+
'twelfth' => 'csv',
1125+
'thirteenth' => 'uri',
10961126
];
10971127

10981128
public function setSeventh($seventh)

user_guide_src/source/models/entities.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ You can specify that properties in your Entity should be converted to common dat
330330
This option should be an array where the key is the name of the class property, and the value is the data type it
331331
should be cast to. Casting only affects when values are read. No conversions happen that affect the permanent value in
332332
either the entity or the database. Properties can be cast to any of the following data types:
333-
**integer**, **float**, **double**, **string**, **boolean**, **object**, **array**, **datetime**, and **timestamp**.
333+
**integer**, **float**, **double**, **string**, **boolean**, **object**, **array**, **datetime**, **timestamp**, and **URI**.
334334
Add a question mark at the beginning of type to mark property as nullable, i.e., **?string**, **?integer**.
335335

336336
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)