diff --git a/composer.json b/composer.json index e09500511f7ce8b5a501feabbbacdb528b7bf9cf..6f141889c4538810117757663bfffbeed3b08d89 100644 --- a/composer.json +++ b/composer.json @@ -53,6 +53,7 @@ "src/exceptions.php" ], "files": [ + "src/constants.php", "src/functions.php", "src/Conditions/functions.php", "src/Types/functions.php" diff --git a/src/DefaultValue.php b/src/DefaultOrExistingValue.php similarity index 53% rename from src/DefaultValue.php rename to src/DefaultOrExistingValue.php index abdf2ba17f540aa69665e5ef72732d794c29a11f..fe4bf0e113b0769c93018305299533a06c2862d8 100644 --- a/src/DefaultValue.php +++ b/src/DefaultOrExistingValue.php @@ -4,6 +4,9 @@ declare(strict_types=1); namespace Grifart\Tables; -final class DefaultValue +/** + * @internal + */ +final class DefaultOrExistingValue { } diff --git a/src/Scaffolding/TableImplementation.php b/src/Scaffolding/TableImplementation.php index 122d16eeb12b9cdacfa4866846432c2fedcd35c3..a4f01d5da970757e42f0aa64e0806f5701dff753 100644 --- a/src/Scaffolding/TableImplementation.php +++ b/src/Scaffolding/TableImplementation.php @@ -14,7 +14,7 @@ use Grifart\Tables\Column; use Grifart\Tables\ColumnMetadata; use Grifart\Tables\ColumnNotFound; use Grifart\Tables\Conditions\Condition; -use Grifart\Tables\DefaultValue; +use Grifart\Tables\DefaultOrExistingValue; use Grifart\Tables\Expression; use Grifart\Tables\RowNotFound; use Grifart\Tables\OrderBy\OrderBy; @@ -209,8 +209,8 @@ final class TableImplementation implements Capability $isNullable = $fieldType->isNullable(); if ($hasDefaultValue) { - $namespace->addUse(DefaultValue::class); - $fieldType = new UnionType($fieldType, resolve(DefaultValue::class)); + $namespace->addUse(DefaultOrExistingValue::class); + $fieldType = new UnionType($fieldType, resolve(DefaultOrExistingValue::class)); } @@ -219,7 +219,14 @@ final class TableImplementation implements Capability ->setNullable($isNullable); if ($hasDefaultValue) { - $parameter->setDefaultValue(new Code\Literal('new ?', [new Code\Literal($namespace->simplifyName(DefaultValue::class))])); + $parameter->setDefaultValue( + new Code\Literal( + $namespace->simplifyName( + $isEditMethod ? 'Grifart\Tables\Unchanged' : 'Grifart\Tables\DefaultValue', + $namespace::NameConstant, + ), + ), + ); } if ($fieldType->requiresDocComment()) { @@ -233,7 +240,7 @@ final class TableImplementation implements Capability if ($hasDefaultValue) { $method->addBody( 'if (!? instanceof ?) {', - [new Code\Literal('$' . $fieldName), new Code\Literal($namespace->simplifyName(DefaultValue::class))], + [new Code\Literal('$' . $fieldName), new Code\Literal($namespace->simplifyName(DefaultOrExistingValue::class))], ); } diff --git a/src/constants.php b/src/constants.php new file mode 100644 index 0000000000000000000000000000000000000000..93fb528fc90e99a29139dc64402d36e7f35be52b --- /dev/null +++ b/src/constants.php @@ -0,0 +1,8 @@ +<?php + +declare(strict_types=1); + +namespace Grifart\Tables; + +const DefaultValue = new DefaultOrExistingValue(); +const Unchanged = new DefaultOrExistingValue();