Skip to content

Commit

Permalink
Make Decimal128 Incrementable
Browse files Browse the repository at this point in the history
  • Loading branch information
malarzm committed May 1, 2020
1 parent e2c9bc1 commit 498d069
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
7 changes: 4 additions & 3 deletions docs/en/reference/storage-strategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ increment
---------

The ``increment`` strategy does not apply to collections but can be used for
``int``, ``float``, and any custom type implementing the ``\Doctrine\ODM\MongoDB\Types\Incrementable``
interface. When using the ``increment`` strategy, the field value will be
updated using the `$inc`_ operator.
``int``, ``float``, ``decimal128``, and any custom type implementing the
``\Doctrine\ODM\MongoDB\Types\Incrementable`` interface. When using the
``increment`` strategy, the field value will be updated using the `$inc`_
operator.

addToSet
--------
Expand Down
8 changes: 7 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Types/Decimal128Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
namespace Doctrine\ODM\MongoDB\Types;

use MongoDB\BSON\Decimal128;
use function bcsub;

class Decimal128Type extends Type
class Decimal128Type extends Type implements Incrementable
{
use ClosureToPHP;

Expand All @@ -26,4 +27,9 @@ public function convertToPHPValue($value)
{
return $value !== null ? (string) $value : null;
}

public function diff($old, $new)
{
return bcsub($new, $old);
}
}
39 changes: 39 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,37 @@
use Documents\UserUpsertIdStrategyNone;
use InvalidArgumentException;
use MongoDB\BSON\ObjectId;
use const PHP_VERSION;
use function bcscale;
use function bcsqrt;
use function min;
use function strlen;
use function version_compare;

class FunctionalTest extends BaseTest
{
/** @var int */
private $initialScale;

public function setUp() : void
{
parent::setUp();

if (version_compare('7.3.0', PHP_VERSION, '<=')) {
$this->initialScale = bcscale(2);
} else {
$this->initialScale = min(0, strlen(bcsqrt('2')) - 2);
bcscale(2);
}
}

public function tearDown() : void
{
parent::tearDown();

bcscale($this->initialScale);
}

public function provideUpsertObjects()
{
return [
Expand Down Expand Up @@ -291,6 +319,7 @@ public function testIncrement()
$user->setUsername('jon');
$user->setCount(100);
$user->setFloatCount(100);
$user->setDecimal128Count('0.50');

$this->dm->persist($user);
$this->dm->flush();
Expand All @@ -300,21 +329,25 @@ public function testIncrement()

$user->incrementCount(5);
$user->incrementFloatCount(5);
$user->incrementDecimal128Count('0.20');
$this->dm->flush();
$this->dm->clear();

$user = $this->dm->getRepository(User::class)->findOneBy(['username' => 'jon']);
$this->assertSame(105, $user->getCount());
$this->assertSame(105.0, $user->getFloatCount());
$this->assertSame('0.70', $user->getDecimal128Count());

$user->setCount(50);
$user->setFloatCount(50);
$user->setDecimal128Count('9.99');

$this->dm->flush();
$this->dm->clear();
$user = $this->dm->getRepository(User::class)->findOneBy(['username' => 'jon']);
$this->assertSame(50, $user->getCount());
$this->assertSame(50.0, $user->getFloatCount());
$this->assertSame('9.99', $user->getDecimal128Count());
}

public function testIncrementWithFloat()
Expand Down Expand Up @@ -355,6 +388,7 @@ public function testIncrementSetsNull()
$user->setUsername('jon');
$user->setCount(10);
$user->setFloatCount(10);
$user->setDecimal128Count('10.00');

$this->dm->persist($user);
$this->dm->flush();
Expand All @@ -363,24 +397,29 @@ public function testIncrementSetsNull()
$user = $this->dm->getRepository(User::class)->findOneBy(['username' => 'jon']);
$this->assertSame(10, $user->getCount());
$this->assertSame(10.0, $user->getFloatCount());
$this->assertSame('10.00', $user->getDecimal128Count());

$user->incrementCount(1);
$user->incrementFloatCount(1);
$user->incrementDecimal128Count('1');
$this->dm->flush();
$this->dm->clear();

$user = $this->dm->getRepository(User::class)->findOneBy(['username' => 'jon']);
$this->assertSame(11, $user->getCount());
$this->assertSame(11.0, $user->getFloatCount());
$this->assertSame('11.00', $user->getDecimal128Count());

$user->setCount(null);
$user->setFloatCount(null);
$user->setDecimal128Count(null);
$this->dm->flush();
$this->dm->clear();

$user = $this->dm->getRepository(User::class)->findOneBy(['username' => 'jon']);
$this->assertNull($user->getCount());
$this->assertNull($user->getFloatCount());
$this->assertNull($user->getDecimal128Count());
}

public function testTest()
Expand Down
19 changes: 19 additions & 0 deletions tests/Documents/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use function bcadd;

/**
* @ODM\Document(collection="users")
Expand Down Expand Up @@ -74,6 +75,9 @@ class User extends BaseDocument
/** @ODM\Field(type="float", strategy="increment") */
protected $floatCount;

/** @ODM\Field(type="decimal128", strategy="increment") */
protected $decimal128Count;

/** @ODM\ReferenceMany(targetDocument=BlogPost::class, mappedBy="user", nullable=true) */
protected $posts;

Expand Down Expand Up @@ -320,6 +324,16 @@ public function setFloatCount($floatCount)
$this->floatCount = $floatCount;
}

public function getDecimal128Count()
{
return $this->decimal128Count;
}

public function setDecimal128Count($decimal128Count)
{
$this->decimal128Count = $decimal128Count;
}

public function getSimpleReferenceOneInverse()
{
return $this->simpleReferenceOneInverse;
Expand Down Expand Up @@ -348,6 +362,11 @@ public function incrementFloatCount($num = null)
}
}

public function incrementDecimal128Count($num = null)
{
$this->decimal128Count = bcadd($this->decimal128Count, $num ?? '1');
}

public function setPosts($posts)
{
$this->posts = $posts;
Expand Down

0 comments on commit 498d069

Please sign in to comment.