mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Add URI cast
This commit is contained in:
parent
a4c88d3a3f
commit
038a1578bb
28
system/Entity/Cast/URICast.php
Normal file
28
system/Entity/Cast/URICast.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Entity\Cast;
|
||||
|
||||
use CodeIgniter\HTTP\URI;
|
||||
|
||||
/**
|
||||
* Class URICast
|
||||
*/
|
||||
class URICast extends BaseCast
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function get($value, array $params = []): object
|
||||
{
|
||||
return $value instanceof URI ? $value : new URI($value);
|
||||
}
|
||||
}
|
@ -22,6 +22,7 @@ use CodeIgniter\Entity\Cast\JsonCast;
|
||||
use CodeIgniter\Entity\Cast\ObjectCast;
|
||||
use CodeIgniter\Entity\Cast\StringCast;
|
||||
use CodeIgniter\Entity\Cast\TimestampCast;
|
||||
use CodeIgniter\Entity\Cast\URICast;
|
||||
use CodeIgniter\Entity\Exceptions\CastException;
|
||||
use CodeIgniter\I18n\Time;
|
||||
use Exception;
|
||||
@ -82,6 +83,7 @@ class Entity implements JsonSerializable
|
||||
'object' => ObjectCast::class,
|
||||
'string' => StringCast::class,
|
||||
'timestamp' => TimestampCast::class,
|
||||
'uri' => URICast::class,
|
||||
];
|
||||
|
||||
/**
|
||||
@ -226,7 +228,6 @@ class Entity implements JsonSerializable
|
||||
}
|
||||
|
||||
return $value;
|
||||
|
||||
}, $this->attributes);
|
||||
}
|
||||
|
||||
@ -305,7 +306,7 @@ class Entity implements JsonSerializable
|
||||
/**
|
||||
* Set raw data array without any mutations
|
||||
*
|
||||
* @param array $data
|
||||
* @param array $data
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
@ -461,7 +462,7 @@ class Entity implements JsonSerializable
|
||||
/**
|
||||
* Change the value of the private $_cast property
|
||||
*
|
||||
* @param boolean|null $cast
|
||||
* @param boolean|null $cast
|
||||
*
|
||||
* @return boolean|Entity
|
||||
*/
|
||||
@ -509,7 +510,7 @@ class Entity implements JsonSerializable
|
||||
// insert this value. should be outside $isNullable check,
|
||||
// so maybe wants to do sth with null value automatically
|
||||
$method = 'set' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key)));
|
||||
|
||||
|
||||
if (method_exists($this, $method))
|
||||
{
|
||||
$this->$method($value);
|
||||
@ -603,7 +604,7 @@ class Entity implements JsonSerializable
|
||||
* Unsets an attribute property.
|
||||
*
|
||||
* @param string $key
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __unset(string $key): void
|
||||
|
@ -4,6 +4,7 @@ namespace CodeIgniter;
|
||||
|
||||
use CodeIgniter\Entity\Exceptions\CastException;
|
||||
use CodeIgniter\I18n\Time;
|
||||
use CodeIgniter\HTTP\URI;
|
||||
use CodeIgniter\Test\CIUnitTestCase;
|
||||
use CodeIgniter\Test\ReflectionHelper;
|
||||
use DateTime;
|
||||
@ -497,6 +498,32 @@ class EntityTest extends CIUnitTestCase
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
public function testCastURI()
|
||||
{
|
||||
$entity = $this->getCastEntity();
|
||||
|
||||
$data = 'https://codeigniter.com/banana';
|
||||
|
||||
$entity->thirteenth = $data;
|
||||
$this->assertInstanceOf(URI::class, $entity->thirteenth);
|
||||
$this->assertSame($data, (string) $entity->thirteenth);
|
||||
$this->assertSame('/banana', $entity->thirteenth->getPath());
|
||||
}
|
||||
|
||||
public function testURICastURI()
|
||||
{
|
||||
$entity = $this->getCastEntity();
|
||||
|
||||
$data = 'https://codeigniter.com/banana';
|
||||
|
||||
$entity->thirteenth = new URI($data);
|
||||
$this->assertInstanceOf(URI::class, $entity->thirteenth);
|
||||
$this->assertSame($data, (string) $entity->thirteenth);
|
||||
$this->assertSame('/banana', $entity->thirteenth->getPath());
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
public function testCastAsJSON()
|
||||
{
|
||||
$entity = $this->getCastEntity();
|
||||
@ -1050,49 +1077,52 @@ class EntityTest extends CIUnitTestCase
|
||||
return new class($data) extends Entity
|
||||
{
|
||||
protected $attributes = [
|
||||
'first' => null,
|
||||
'second' => null,
|
||||
'third' => null,
|
||||
'fourth' => null,
|
||||
'fifth' => null,
|
||||
'sixth' => null,
|
||||
'seventh' => null,
|
||||
'eighth' => null,
|
||||
'ninth' => null,
|
||||
'tenth' => null,
|
||||
'eleventh' => null,
|
||||
'twelfth' => null,
|
||||
'first' => null,
|
||||
'second' => null,
|
||||
'third' => null,
|
||||
'fourth' => null,
|
||||
'fifth' => null,
|
||||
'sixth' => null,
|
||||
'seventh' => null,
|
||||
'eighth' => null,
|
||||
'ninth' => null,
|
||||
'tenth' => null,
|
||||
'eleventh' => null,
|
||||
'twelfth' => null,
|
||||
'thirteenth' => null,
|
||||
];
|
||||
|
||||
protected $_original = [
|
||||
'first' => null,
|
||||
'second' => null,
|
||||
'third' => null,
|
||||
'fourth' => null,
|
||||
'fifth' => null,
|
||||
'sixth' => null,
|
||||
'seventh' => null,
|
||||
'eighth' => null,
|
||||
'ninth' => null,
|
||||
'tenth' => null,
|
||||
'eleventh' => null,
|
||||
'twelfth' => null,
|
||||
'first' => null,
|
||||
'second' => null,
|
||||
'third' => null,
|
||||
'fourth' => null,
|
||||
'fifth' => null,
|
||||
'sixth' => null,
|
||||
'seventh' => null,
|
||||
'eighth' => null,
|
||||
'ninth' => null,
|
||||
'tenth' => null,
|
||||
'eleventh' => null,
|
||||
'twelfth' => null,
|
||||
'thirteenth' => null,
|
||||
];
|
||||
|
||||
// 'bar' is db column, 'foo' is internal representation
|
||||
protected $casts = [
|
||||
'first' => 'integer',
|
||||
'second' => 'float',
|
||||
'third' => 'double',
|
||||
'fourth' => 'string',
|
||||
'fifth' => 'boolean',
|
||||
'sixth' => 'object',
|
||||
'seventh' => 'array',
|
||||
'eighth' => 'datetime',
|
||||
'ninth' => 'timestamp',
|
||||
'tenth' => 'json',
|
||||
'eleventh' => 'json-array',
|
||||
'twelfth' => 'csv',
|
||||
'first' => 'integer',
|
||||
'second' => 'float',
|
||||
'third' => 'double',
|
||||
'fourth' => 'string',
|
||||
'fifth' => 'boolean',
|
||||
'sixth' => 'object',
|
||||
'seventh' => 'array',
|
||||
'eighth' => 'datetime',
|
||||
'ninth' => 'timestamp',
|
||||
'tenth' => 'json',
|
||||
'eleventh' => 'json-array',
|
||||
'twelfth' => 'csv',
|
||||
'thirteenth' => 'uri',
|
||||
];
|
||||
|
||||
public function setSeventh($seventh)
|
||||
|
@ -330,7 +330,7 @@ You can specify that properties in your Entity should be converted to common dat
|
||||
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**, and **timestamp**.
|
||||
**integer**, **float**, **double**, **string**, **boolean**, **object**, **array**, **datetime**, **timestamp**, and **URI**.
|
||||
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::
|
||||
|
Loading…
x
Reference in New Issue
Block a user