Skip to content

Commit 54aeb0a

Browse files
Entity - Move default cast handlers to config
The main reason for moving this to config is to be able to use Registars to extend the defaultCastHandles withouth having to modify every entiy object.
1 parent 93cd7c6 commit 54aeb0a

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

app/Config/Entity.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Config;
4+
5+
use CodeIgniter\Config\BaseConfig;
6+
use CodeIgniter\Entity\Cast\ArrayCast;
7+
use CodeIgniter\Entity\Cast\BooleanCast;
8+
use CodeIgniter\Entity\Cast\CSVCast;
9+
use CodeIgniter\Entity\Cast\DatetimeCast;
10+
use CodeIgniter\Entity\Cast\FloatCast;
11+
use CodeIgniter\Entity\Cast\IntegerCast;
12+
use CodeIgniter\Entity\Cast\JsonCast;
13+
use CodeIgniter\Entity\Cast\ObjectCast;
14+
use CodeIgniter\Entity\Cast\StringCast;
15+
use CodeIgniter\Entity\Cast\TimestampCast;
16+
use CodeIgniter\Entity\Cast\URICast;
17+
18+
class Entity extends BaseConfig
19+
{
20+
/**
21+
* Convert handlers
22+
*
23+
* @var array
24+
*/
25+
public array $castHandlers = [
26+
'array' => ArrayCast::class,
27+
'bool' => BooleanCast::class,
28+
'boolean' => BooleanCast::class,
29+
'csv' => CSVCast::class,
30+
'datetime' => DatetimeCast::class,
31+
'double' => FloatCast::class,
32+
'float' => FloatCast::class,
33+
'int' => IntegerCast::class,
34+
'integer' => IntegerCast::class,
35+
'json' => JsonCast::class,
36+
'object' => ObjectCast::class,
37+
'string' => StringCast::class,
38+
'timestamp' => TimestampCast::class,
39+
'uri' => URICast::class,
40+
];
41+
}

system/Entity/Entity.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,6 @@ class Entity implements JsonSerializable
6565
*/
6666
protected $castHandlers = [];
6767

68-
/**
69-
* Default convert handlers
70-
*
71-
* @var array<string, string>
72-
*/
73-
private array $defaultCastHandlers = [
74-
'array' => ArrayCast::class,
75-
'bool' => BooleanCast::class,
76-
'boolean' => BooleanCast::class,
77-
'csv' => CSVCast::class,
78-
'datetime' => DatetimeCast::class,
79-
'double' => FloatCast::class,
80-
'float' => FloatCast::class,
81-
'int' => IntegerCast::class,
82-
'integer' => IntegerCast::class,
83-
'json' => JsonCast::class,
84-
'object' => ObjectCast::class,
85-
'string' => StringCast::class,
86-
'timestamp' => TimestampCast::class,
87-
'uri' => URICast::class,
88-
];
89-
9068
/**
9169
* Holds the current values of all class vars.
9270
*
@@ -373,7 +351,7 @@ protected function castAs($value, string $attribute, string $method = 'get')
373351

374352
$type = trim($type, '[]');
375353

376-
$handlers = array_merge($this->defaultCastHandlers, $this->castHandlers);
354+
$handlers = array_merge(config('Entity')->castHandlers, $this->castHandlers);
377355

378356
if (empty($handlers[$type])) {
379357
return $value;

0 commit comments

Comments
 (0)