-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8230 from kenjis/feat-db-type-converter
feat: add DataConverter to convert types
- Loading branch information
Showing
22 changed files
with
1,349 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\DataCaster\Cast; | ||
|
||
/** | ||
* Class ArrayCast | ||
* | ||
* (PHP) [array --> string] --> (DB driver) --> (DB column) string | ||
* [ <-- string] <-- (DB driver) <-- (DB column) string | ||
*/ | ||
class ArrayCast extends BaseCast implements CastInterface | ||
{ | ||
public static function get(mixed $value, array $params = []): array | ||
{ | ||
if (! is_string($value)) { | ||
self::invalidTypeValueError($value); | ||
} | ||
|
||
if ((strpos($value, 'a:') === 0 || strpos($value, 's:') === 0)) { | ||
$value = unserialize($value, ['allowed_classes' => false]); | ||
} | ||
|
||
return (array) $value; | ||
} | ||
|
||
public static function set(mixed $value, array $params = []): string | ||
{ | ||
return serialize($value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\DataCaster\Cast; | ||
|
||
use TypeError; | ||
|
||
abstract class BaseCast implements CastInterface | ||
{ | ||
public static function get(mixed $value, array $params = []): mixed | ||
{ | ||
return $value; | ||
} | ||
|
||
public static function set(mixed $value, array $params = []): mixed | ||
{ | ||
return $value; | ||
} | ||
|
||
protected static function invalidTypeValueError(mixed $value): never | ||
{ | ||
$message = '[' . static::class . '] Invalid value type: ' . get_debug_type($value); | ||
if (is_scalar($value)) { | ||
$message .= ', and its value: ' . var_export($value, true); | ||
} | ||
|
||
throw new TypeError($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\DataCaster\Cast; | ||
|
||
/** | ||
* Class BooleanCast | ||
* | ||
* (PHP) [bool --> bool ] --> (DB driver) --> (DB column) bool|int(0/1) | ||
* [ <-- string|int] <-- (DB driver) <-- (DB column) bool|int(0/1) | ||
*/ | ||
class BooleanCast extends BaseCast | ||
{ | ||
public static function get(mixed $value, array $params = []): bool | ||
{ | ||
// For PostgreSQL | ||
if ($value === 't') { | ||
return true; | ||
} | ||
if ($value === 'f') { | ||
return false; | ||
} | ||
|
||
return filter_var($value, FILTER_VALIDATE_BOOLEAN); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\DataCaster\Cast; | ||
|
||
/** | ||
* Class CSVCast | ||
* | ||
* (PHP) [array --> string] --> (DB driver) --> (DB column) string | ||
* [ <-- string] <-- (DB driver) <-- (DB column) string | ||
*/ | ||
class CSVCast extends BaseCast | ||
{ | ||
public static function get(mixed $value, array $params = []): array | ||
{ | ||
if (! is_string($value)) { | ||
self::invalidTypeValueError($value); | ||
} | ||
|
||
return explode(',', $value); | ||
} | ||
|
||
public static function set(mixed $value, array $params = []): string | ||
{ | ||
if (! is_array($value)) { | ||
self::invalidTypeValueError($value); | ||
} | ||
|
||
return implode(',', $value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\DataCaster\Cast; | ||
|
||
interface CastInterface | ||
{ | ||
/** | ||
* Takes a value from DataSource, returns its value for PHP. | ||
* | ||
* @param mixed $value Data from database driver | ||
* @param list<string> $params Additional param | ||
* | ||
* @return mixed PHP native value | ||
*/ | ||
public static function get(mixed $value, array $params = []): mixed; | ||
|
||
/** | ||
* Takes a PHP value, returns its value for DataSource. | ||
* | ||
* @param mixed $value PHP native value | ||
* @param list<string> $params Additional param | ||
* | ||
* @return mixed Data to pass to database driver | ||
*/ | ||
public static function set(mixed $value, array $params = []): mixed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\DataCaster\Cast; | ||
|
||
use CodeIgniter\I18n\Time; | ||
|
||
/** | ||
* Class DatetimeCast | ||
* | ||
* (PHP) [Time --> string] --> (DB driver) --> (DB column) datetime | ||
* [ <-- string] <-- (DB driver) <-- (DB column) datetime | ||
*/ | ||
class DatetimeCast extends BaseCast | ||
{ | ||
public static function get(mixed $value, array $params = []): Time | ||
{ | ||
if (! is_string($value)) { | ||
self::invalidTypeValueError($value); | ||
} | ||
|
||
/** | ||
* @see https://www.php.net/manual/en/datetimeimmutable.createfromformat.php#datetimeimmutable.createfromformat.parameters | ||
*/ | ||
$format = $params[0] ?? 'Y-m-d H:i:s'; | ||
|
||
return Time::createFromFormat($format, $value); | ||
} | ||
|
||
public static function set(mixed $value, array $params = []): string | ||
{ | ||
if (! $value instanceof Time) { | ||
self::invalidTypeValueError($value); | ||
} | ||
|
||
return (string) $value; | ||
} | ||
} |
Oops, something went wrong.