-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add a NumberType
that maps to the BcMath\Number
value object
#6686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Types; | ||
|
||
use BcMath\Number; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\Exception\InvalidType; | ||
use Doctrine\DBAL\Types\Exception\ValueNotConvertible; | ||
use TypeError; | ||
use ValueError; | ||
|
||
use function is_float; | ||
|
||
final class NumberType extends Type | ||
{ | ||
/** {@inheritDoc} */ | ||
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string | ||
{ | ||
return $platform->getDecimalTypeDeclarationSQL($column); | ||
} | ||
|
||
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
if (! $value instanceof Number) { | ||
throw InvalidType::new($value, static::class, ['null', Number::class]); | ||
} | ||
|
||
return (string) $value; | ||
} | ||
|
||
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Number | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
// SQLite might return a decimal as float. | ||
if (is_float($value)) { | ||
$value = (string) $value; | ||
} | ||
|
||
try { | ||
return new Number($value); | ||
} catch (TypeError | ValueError $e) { | ||
throw ValueNotConvertible::new($value, static::class, previous: $e); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Functional\Types; | ||
|
||
use BcMath\Number; | ||
use Doctrine\DBAL\Schema\Table; | ||
use Doctrine\DBAL\Tests\FunctionalTestCase; | ||
use Doctrine\DBAL\Types\Types; | ||
use PHPUnit\Framework\Attributes\RequiresPhp; | ||
use PHPUnit\Framework\Attributes\RequiresPhpExtension; | ||
use PHPUnit\Framework\Attributes\TestWith; | ||
|
||
#[RequiresPhp('8.4')] | ||
#[RequiresPhpExtension('bcmath')] | ||
final class NumberTest extends FunctionalTestCase | ||
{ | ||
#[TestWith(['13.37'])] | ||
#[TestWith(['13.0'])] | ||
public function testInsertAndRetrieveNumber(string $numberAsString): void | ||
{ | ||
$expected = new Number($numberAsString); | ||
|
||
$table = new Table('number_table'); | ||
$table->addColumn('val', Types::NUMBER, ['precision' => 4, 'scale' => 2]); | ||
|
||
$this->dropAndCreateTable($table); | ||
|
||
$this->connection->insert( | ||
'number_table', | ||
['val' => $expected], | ||
['val' => Types::NUMBER], | ||
); | ||
|
||
$value = $this->connection->convertToPHPValue( | ||
$this->connection->fetchOne('SELECT val FROM number_table'), | ||
Types::NUMBER, | ||
); | ||
|
||
self::assertInstanceOf(Number::class, $value); | ||
self::assertSame(0, $expected <=> $value); | ||
} | ||
|
||
public function testCompareNumberTable(): void | ||
{ | ||
$table = new Table('number_table'); | ||
$table->addColumn('val', Types::NUMBER, ['precision' => 4, 'scale' => 2]); | ||
|
||
$this->dropAndCreateTable($table); | ||
|
||
$schemaManager = $this->connection->createSchemaManager(); | ||
|
||
self::assertTrue( | ||
$schemaManager->createComparator() | ||
->compareTables($schemaManager->introspectTable('number_table'), $table) | ||
->isEmpty(), | ||
); | ||
} | ||
} |
This file contains hidden or 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,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Types; | ||
|
||
use BcMath\Number; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\Exception\InvalidType; | ||
use Doctrine\DBAL\Types\Exception\ValueNotConvertible; | ||
use Doctrine\DBAL\Types\NumberType; | ||
use PHPUnit\Framework\Attributes\RequiresPhp; | ||
use PHPUnit\Framework\Attributes\RequiresPhpExtension; | ||
use PHPUnit\Framework\Attributes\TestWith; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
|
||
#[RequiresPhp('8.4')] | ||
#[RequiresPhpExtension('bcmath')] | ||
final class NumberTest extends TestCase | ||
{ | ||
private AbstractPlatform&MockObject $platform; | ||
private NumberType $type; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->platform = $this->createMock(AbstractPlatform::class); | ||
$this->type = new NumberType(); | ||
} | ||
|
||
#[TestWith(['5.5'])] | ||
#[TestWith(['5.5000'])] | ||
#[TestWith([5.5])] | ||
public function testDecimalConvertsToPHPValue(mixed $dbValue): void | ||
{ | ||
$phpValue = $this->type->convertToPHPValue($dbValue, $this->platform); | ||
|
||
self::assertInstanceOf(Number::class, $phpValue); | ||
self::assertSame(0, $phpValue <=> new Number('5.5')); | ||
} | ||
|
||
public function testDecimalNullConvertsToPHPValue(): void | ||
{ | ||
self::assertNull($this->type->convertToPHPValue(null, $this->platform)); | ||
} | ||
|
||
public function testNumberConvertsToDecimalString(): void | ||
{ | ||
self::assertSame('5.5', $this->type->convertToDatabaseValue(new Number('5.5'), $this->platform)); | ||
} | ||
|
||
public function testNumberNullConvertsToNull(): void | ||
{ | ||
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform)); | ||
} | ||
|
||
#[TestWith(['5.5'])] | ||
#[TestWith([new stdClass()])] | ||
public function testInvalidPhpValuesTriggerException(mixed $value): void | ||
{ | ||
self::expectException(InvalidType::class); | ||
|
||
$this->type->convertToDatabaseValue($value, $this->platform); | ||
} | ||
|
||
#[TestWith(['foo'])] | ||
#[TestWith([true])] | ||
public function testUnexpectedValuesReturnedByTheDatabaseTriggerException(mixed $value): void | ||
{ | ||
self::expectException(ValueNotConvertible::class); | ||
|
||
$this->type->convertToPHPValue($value, $this->platform); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is necessary because of Psalm, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I know, we need to kick it.