Skip to content

Commit

Permalink
Merge pull request #8230 from kenjis/feat-db-type-converter
Browse files Browse the repository at this point in the history
feat: add DataConverter to convert types
  • Loading branch information
kenjis authored Feb 4, 2024
2 parents d4362c4 + 7199ac4 commit e5c5ced
Show file tree
Hide file tree
Showing 22 changed files with 1,349 additions and 95 deletions.
19 changes: 19 additions & 0 deletions deptrac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ parameters:
collectors:
- type: className
regex: ^Codeigniter\\Database\\.*
- name: DataCaster
collectors:
- type: className
regex: ^Codeigniter\\DataCaster\\.*
- name: DataConverter
collectors:
- type: className
regex: ^Codeigniter\\DataConverter\\.*
- name: Email
collectors:
- type: className
Expand Down Expand Up @@ -166,10 +174,16 @@ parameters:
- Entity
- Events
- I18n
DataCaster:
- I18n
- URI
DataConverter:
- DataCaster
Email:
- I18n
- Events
Entity:
- DataCaster
- I18n
Files:
- I18n
Expand Down Expand Up @@ -228,6 +242,11 @@ parameters:
- CodeIgniter\HTTP\Header
- CodeIgniter\HTTP\IncomingRequest
- CodeIgniter\HTTP\ResponseInterface
CodeIgniter\DataCaster\DataCaster:
- CodeIgniter\Entity\Cast\CastInterface
- CodeIgniter\Entity\Exceptions\CastException
CodeIgniter\DataCaster\Exceptions\CastException:
- CodeIgniter\Entity\Exceptions\CastException
CodeIgniter\Entity\Cast\URICast:
- CodeIgniter\HTTP\URI
CodeIgniter\Log\Handlers\ChromeLoggerHandler:
Expand Down
2 changes: 1 addition & 1 deletion phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@
];
$ignoreErrors[] = [
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
'count' => 3,
'count' => 1,
'path' => __DIR__ . '/system/Entity/Entity.php',
];
$ignoreErrors[] = [
Expand Down
40 changes: 1 addition & 39 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<files psalm-version="5.20.0@3f284e96c9d9be6fe6b15c79416e1d1903dcfef4">
<file src="app/Config/View.php">
<UndefinedDocblockClass>
<code><![CDATA[array<string, ParserCallableString>]]></code>
Expand All @@ -26,12 +26,6 @@
<code>Memcache|Memcached</code>
</UndefinedDocblockClass>
</file>
<file src="system/Commands/Utilities/Routes/ControllerMethodReader.php">
<DuplicateArrayKey>
<code>$routeWithoutController</code>
<code>$routeWithoutController</code>
</DuplicateArrayKey>
</file>
<file src="system/Config/View.php">
<UndefinedDocblockClass>
<code><![CDATA[array<string, array<parser_callable_string>|parser_callable_string|parser_callable>]]></code>
Expand Down Expand Up @@ -91,15 +85,6 @@
<code><![CDATA[$this->db->transStatus]]></code>
</InaccessibleProperty>
</file>
<file src="system/Database/OCI8/Connection.php">
<UndefinedConstant>
<code>OCI_COMMIT_ON_SUCCESS</code>
<code>OCI_COMMIT_ON_SUCCESS</code>
<code>OCI_COMMIT_ON_SUCCESS</code>
<code>OCI_NO_AUTO_COMMIT</code>
<code>SQLT_CHR</code>
</UndefinedConstant>
</file>
<file src="system/Debug/Toolbar/Views/toolbar.tpl.php">
<InaccessibleMethod>
<code>renderTimeline</code>
Expand Down Expand Up @@ -132,11 +117,6 @@
<code>#[ReturnTypeWillChange]</code>
</MissingImmutableAnnotation>
</file>
<file src="system/Test/ControllerResponse.php">
<UnsupportedPropertyReferenceUsage>
<code><![CDATA[$this->dom = &$this->domParser]]></code>
</UnsupportedPropertyReferenceUsage>
</file>
<file src="system/View/Parser.php">
<UndefinedDocblockClass>
<code><![CDATA[array<string, array<parser_callable_string>|parser_callable_string|parser_callable>]]></code>
Expand Down Expand Up @@ -164,11 +144,6 @@
<code>$this</code>
</InvalidScope>
</file>
<file src="tests/system/CLI/ConsoleTest.php">
<DuplicateArrayKey>
<code>$command</code>
</DuplicateArrayKey>
</file>
<file src="tests/system/CommonFunctionsTest.php">
<UndefinedClass>
<code>UnexsistenceClass</code>
Expand All @@ -181,19 +156,6 @@
<code><![CDATA['Config\TestRegistrar']]></code>
</UndefinedClass>
</file>
<file src="tests/system/Database/BaseConnectionTest.php">
<InaccessibleProperty>
<code><![CDATA[$db->username]]></code>
<code><![CDATA[$db->username]]></code>
</InaccessibleProperty>
</file>
<file src="tests/system/Database/Live/OCI8/CallStoredProcedureTest.php">
<UndefinedConstant>
<code>OCI_ASSOC</code>
<code>OCI_B_CURSOR</code>
<code>OCI_RETURN_NULLS</code>
</UndefinedConstant>
</file>
<file src="tests/system/Entity/EntityTest.php">
<EmptyArrayAccess>
<code>$current[$key]</code>
Expand Down
41 changes: 41 additions & 0 deletions system/DataCaster/Cast/ArrayCast.php
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);
}
}
39 changes: 39 additions & 0 deletions system/DataCaster/Cast/BaseCast.php
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);
}
}
36 changes: 36 additions & 0 deletions system/DataCaster/Cast/BooleanCast.php
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);
}
}
41 changes: 41 additions & 0 deletions system/DataCaster/Cast/CSVCast.php
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);
}
}
37 changes: 37 additions & 0 deletions system/DataCaster/Cast/CastInterface.php
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;
}
48 changes: 48 additions & 0 deletions system/DataCaster/Cast/DatetimeCast.php
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;
}
}
Loading

0 comments on commit e5c5ced

Please sign in to comment.