Skip to content

Commit 5c44ce1

Browse files
committed
feat: add Database\DataConverter class
1 parent 9dab5f1 commit 5c44ce1

File tree

15 files changed

+1180
-0
lines changed

15 files changed

+1180
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Database\DataConverter\Cast;
13+
14+
/**
15+
* Class ArrayCast
16+
*
17+
* DB column: string <--> PHP: array
18+
*/
19+
class ArrayCast extends BaseCast implements CastInterface
20+
{
21+
/**
22+
* {@inheritDoc}
23+
*/
24+
public static function fromDatabase($value, array $params = []): array
25+
{
26+
if (! is_string($value)) {
27+
self::invalidTypeValueError($value);
28+
}
29+
30+
if ((strpos($value, 'a:') === 0 || strpos($value, 's:') === 0)) {
31+
$value = unserialize($value, ['allowed_classes' => false]);
32+
}
33+
34+
return (array) $value;
35+
}
36+
37+
/**
38+
* {@inheritDoc}
39+
*/
40+
public static function toDatabase($value, array $params = []): string
41+
{
42+
return serialize($value);
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Database\DataConverter\Cast;
13+
14+
use TypeError;
15+
16+
/**
17+
* Class BaseCast
18+
*/
19+
abstract class BaseCast implements CastInterface
20+
{
21+
/**
22+
* {@inheritDoc}
23+
*/
24+
public static function fromDatabase($value, array $params = [])
25+
{
26+
return $value;
27+
}
28+
29+
/**
30+
* {@inheritDoc}
31+
*/
32+
public static function toDatabase($value, array $params = [])
33+
{
34+
return $value;
35+
}
36+
37+
/**
38+
* Throws TypeError
39+
*/
40+
protected static function invalidTypeValueError(mixed $value): never
41+
{
42+
throw new TypeError('Invalid type value: ' . var_export($value, true));
43+
}
44+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Database\DataConverter\Cast;
13+
14+
/**
15+
* Class BooleanCast
16+
*
17+
* DB column: bool|int(0/1) <--> PHP: bool
18+
*/
19+
class BooleanCast extends BaseCast
20+
{
21+
/**
22+
* {@inheritDoc}
23+
*/
24+
public static function fromDatabase($value, array $params = []): bool
25+
{
26+
// For PostgreSQL
27+
if ($value === 't') {
28+
return true;
29+
}
30+
if ($value === 'f') {
31+
return false;
32+
}
33+
34+
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
35+
}
36+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Database\DataConverter\Cast;
13+
14+
/**
15+
* Class CSVCast
16+
*
17+
* DB column: string <--> PHP: array
18+
*/
19+
class CSVCast extends BaseCast
20+
{
21+
/**
22+
* {@inheritDoc}
23+
*/
24+
public static function fromDatabase($value, array $params = []): array
25+
{
26+
if (! is_string($value)) {
27+
self::invalidTypeValueError($value);
28+
}
29+
30+
return explode(',', $value);
31+
}
32+
33+
/**
34+
* {@inheritDoc}
35+
*/
36+
public static function toDatabase($value, array $params = []): string
37+
{
38+
if (! is_array($value)) {
39+
self::invalidTypeValueError($value);
40+
}
41+
42+
return implode(',', $value);
43+
}
44+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Database\DataConverter\Cast;
13+
14+
/**
15+
* Interface CastInterface
16+
*/
17+
interface CastInterface
18+
{
19+
/**
20+
* Takes value from database, returns its value for PHP.
21+
*
22+
* @param bool|float|int|string|null $value Data
23+
* @param array $params Additional param
24+
*
25+
* @return array|bool|float|int|object|string|null
26+
*/
27+
public static function fromDatabase($value, array $params = []);
28+
29+
/**
30+
* Takes the PHP value, returns its value for database.
31+
*
32+
* @param array|bool|float|int|object|string|null $value Data
33+
* @param array $params Additional param
34+
*
35+
* @return bool|float|int|string|null
36+
*/
37+
public static function toDatabase($value, array $params = []);
38+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Database\DataConverter\Cast;
13+
14+
use CodeIgniter\I18n\Time;
15+
use Exception;
16+
17+
/**
18+
* Class DatetimeCast
19+
*
20+
* DB column: datetime <--> PHP: Time
21+
*/
22+
class DatetimeCast extends BaseCast
23+
{
24+
/**
25+
* {@inheritDoc}
26+
*
27+
* @throws Exception
28+
*/
29+
public static function fromDatabase($value, array $params = []): Time
30+
{
31+
if (! is_string($value)) {
32+
self::invalidTypeValueError($value);
33+
}
34+
35+
return Time::parse($value);
36+
}
37+
38+
/**
39+
* {@inheritDoc}
40+
*/
41+
public static function toDatabase($value, array $params = []): string
42+
{
43+
if (! $value instanceof Time) {
44+
self::invalidTypeValueError($value);
45+
}
46+
47+
return (string) $value;
48+
}
49+
}
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 CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Database\DataConverter\Cast;
13+
14+
/**
15+
* Class FloatCast
16+
*
17+
* DB column: float <--> PHP: float
18+
*/
19+
class FloatCast extends BaseCast
20+
{
21+
/**
22+
* {@inheritDoc}
23+
*/
24+
public static function fromDatabase($value, array $params = []): float
25+
{
26+
return (float) $value;
27+
}
28+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Database\DataConverter\Cast;
13+
14+
/**
15+
* Int Bool Cast
16+
*
17+
* DB column: int(0/1) <--> PHP: bool
18+
*/
19+
final class IntBoolCast extends BaseCast
20+
{
21+
/**
22+
* {@inheritDoc}
23+
*/
24+
public static function fromDatabase($value, array $params = []): bool
25+
{
26+
if (! is_int($value) && ! is_string($value)) {
27+
self::invalidTypeValueError($value);
28+
}
29+
30+
return (bool) $value;
31+
}
32+
33+
/**
34+
* {@inheritDoc}
35+
*/
36+
public static function toDatabase($value, array $params = []): int
37+
{
38+
if (! is_bool($value)) {
39+
self::invalidTypeValueError($value);
40+
}
41+
42+
return (int) $value;
43+
}
44+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Database\DataConverter\Cast;
13+
14+
/**
15+
* Class IntegerCast
16+
*
17+
* DB column: int <--> PHP: int
18+
*/
19+
class IntegerCast extends BaseCast
20+
{
21+
/**
22+
* {@inheritDoc}
23+
*/
24+
public static function fromDatabase($value, array $params = []): int
25+
{
26+
if (! is_string($value) && ! is_int($value)) {
27+
self::invalidTypeValueError($value);
28+
}
29+
30+
return (int) $value;
31+
}
32+
}

0 commit comments

Comments
 (0)