Skip to content

Commit

Permalink
Typecast refactoring (#724)
Browse files Browse the repository at this point in the history
* Fix bit type

* Typecast refactoring

* Start from master

* Fix test issues

* Fix test issues

* Update

* Restart tests

* Update

* Update

* Update
  • Loading branch information
Tigrov committed Jul 13, 2023
1 parent dc9669c commit 13ddab5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 49 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Expand Up @@ -31,11 +31,11 @@ composer.phar

# phpunit itself is not needed
phpunit.phar
.phpunit.result.cache
tests/Support/runtime/

# local phpunit config
# local phpunit config and cache
/phpunit.xml
/.phpunit.result.cache
/tests/Support/runtime/

# NPM packages
/node_modules
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## 1.0.1 under development

- Chg #722: Remove legacy array syntax for typecast. Use `Param` instead (@terabytesoftw)
- Chg #724: Typecast refactoring. (@Tigrov)

## 1.0.0 April 12, 2023

Expand Down
70 changes: 24 additions & 46 deletions src/Schema/AbstractColumnSchema.php
Expand Up @@ -242,59 +242,37 @@ public function unsigned(bool $value): void
protected function typecast(mixed $value): mixed
{
if (
$value === ''
&& !in_array(
$this->type,
[
SchemaInterface::TYPE_TEXT,
SchemaInterface::TYPE_STRING,
SchemaInterface::TYPE_BINARY,
SchemaInterface::TYPE_CHAR,
],
true
)
$value === null
|| $value === '' && !in_array($this->type, [
SchemaInterface::TYPE_TEXT,
SchemaInterface::TYPE_STRING,
SchemaInterface::TYPE_BINARY,
SchemaInterface::TYPE_CHAR,
], true)
) {
return null;
}

if (
$value === null
|| $value instanceof ExpressionInterface
|| gettype($value) === $this->phpType
) {
if ($value instanceof ExpressionInterface) {
return $value;
}

switch ($this->phpType) {
case SchemaInterface::PHP_TYPE_RESOURCE:
case SchemaInterface::PHP_TYPE_STRING:
if (is_resource($value)) {
return $value;
}

if (is_float($value)) {
return match ($this->phpType) {
gettype($value) => $value,
SchemaInterface::PHP_TYPE_RESOURCE,
SchemaInterface::PHP_TYPE_STRING
=> match (true) {
is_resource($value) => $value,
/** ensure type cast always has . as decimal separator in all locales */
return DbStringHelper::normalizeFloat($value);
}

if (is_bool($value)) {
return $value ? '1' : '0';
}

return (string) $value;
case SchemaInterface::PHP_TYPE_INTEGER:
return (int) $value;
case SchemaInterface::PHP_TYPE_BOOLEAN:
/**
* Treating a 0-bit value as false too.
*
* @link https://github.com/yiisoft/yii2/issues/9006
*/
return (bool) $value && $value !== "\0";
case SchemaInterface::PHP_TYPE_DOUBLE:
return (float) $value;
}

return $value;
is_float($value) => DbStringHelper::normalizeFloat($value),
is_bool($value) => $value ? '1' : '0',
default => (string) $value,
},
SchemaInterface::PHP_TYPE_INTEGER => (int) $value,
/** Treating a 0-bit value as false too (@link https://github.com/yiisoft/yii2/issues/9006) */
SchemaInterface::PHP_TYPE_BOOLEAN => $value && $value !== "\0",
SchemaInterface::PHP_TYPE_DOUBLE => (float) $value,
default => $value,
};
}
}

0 comments on commit 13ddab5

Please sign in to comment.