Skip to content

Commit

Permalink
Merge pull request #4691 from MGatner/cast-uri
Browse files Browse the repository at this point in the history
Add URI cast
  • Loading branch information
MGatner authored May 16, 2021
2 parents 461b965 + 4f02255 commit 1d9d1b5
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 42 deletions.
28 changes: 28 additions & 0 deletions system/Entity/Cast/URICast.php
Original file line number Diff line number Diff line change
@@ -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 = []): URI
{
return $value instanceof URI ? $value : new URI($value);
}
}
11 changes: 6 additions & 5 deletions system/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -82,6 +83,7 @@ class Entity implements JsonSerializable
'object' => ObjectCast::class,
'string' => StringCast::class,
'timestamp' => TimestampCast::class,
'uri' => URICast::class,
];

/**
Expand Down Expand Up @@ -226,7 +228,6 @@ public function toRawArray(bool $onlyChanged = false, bool $recursive = false):
}

return $value;

}, $this->attributes);
}

Expand Down Expand Up @@ -305,7 +306,7 @@ public function hasChanged(string $key = null): bool
/**
* Set raw data array without any mutations
*
* @param array $data
* @param array $data
*
* @return $this
*/
Expand Down Expand Up @@ -461,7 +462,7 @@ public function jsonSerialize()
/**
* Change the value of the private $_cast property
*
* @param boolean|null $cast
* @param boolean|null $cast
*
* @return boolean|Entity
*/
Expand Down Expand Up @@ -509,7 +510,7 @@ public function __set(string $key, $value = null)
// 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);
Expand Down Expand Up @@ -603,7 +604,7 @@ public function __isset(string $key): bool
* Unsets an attribute property.
*
* @param string $key
*
*
* @return void
*/
public function __unset(string $key): void
Expand Down
102 changes: 66 additions & 36 deletions tests/system/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CodeIgniter\Entity\Exceptions\CastException;
use CodeIgniter\I18n\Time;
use CodeIgniter\HTTP\URI;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\ReflectionHelper;
use DateTime;
Expand Down Expand Up @@ -497,6 +498,32 @@ public function testCastNullable()

//--------------------------------------------------------------------

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();
Expand Down Expand Up @@ -1050,49 +1077,52 @@ protected function getCastEntity($data = null) : Entity
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)
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/models/entities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down

0 comments on commit 1d9d1b5

Please sign in to comment.