Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

Commit

Permalink
Add support for float fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Apr 17, 2019
1 parent 0d59eeb commit 5e75973
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Field/FloatField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

namespace Dynamap\Field;

class FloatField extends Field
{
public function dynamoDbType(): string
{
return 'N';
}

/**
* {@inheritdoc}
*/
protected function castValueForDynamoDbFormat($value): string
{
// Numbers should be sent as strings to DynamoDB
return (string) $value;
}

/**
* {@inheritdoc}
*/
protected function castValueFromDynamoDbFormat($value): float
{
return (float) $value;
}
}
4 changes: 4 additions & 0 deletions src/TableMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Dynamap\Field\BooleanField;
use Dynamap\Field\DateTimeField;
use Dynamap\Field\Field;
use Dynamap\Field\FloatField;
use Dynamap\Field\IntegerField;
use Dynamap\Field\StringField;
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
Expand Down Expand Up @@ -98,6 +99,9 @@ private function readProperties(string $className, array $keys): void
case 'int':
$fieldMapping = new IntegerField($property);
break;
case 'float':
$fieldMapping = new FloatField($property);
break;
case 'bool':
$fieldMapping = new BooleanField($property);
break;
Expand Down
10 changes: 10 additions & 0 deletions tests/DynamapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ public function test string attribute(): void
$this->assertEquals('Hello world!', $article->getName());
}

public function test float attribute(): void
{
$this->dynamap->save(new Article(123));

/** @var Article $article */
$article = $this->dynamap->get('articles', 123);

$this->assertEquals(5., $article->getRating());
}

public function test update existing object(): void
{
$article = new Article(123);
Expand Down
8 changes: 8 additions & 0 deletions tests/Fixture/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class Article
/** @var string|null */
private $name;

/** @var float */
private $rating = 5.;

/** @var bool */
private $published = false;

Expand Down Expand Up @@ -40,6 +43,11 @@ public function getName(): ?string
return $this->name;
}

public function getRating(): float
{
return $this->rating;
}

public function publish(): void
{
$this->published = true;
Expand Down

0 comments on commit 5e75973

Please sign in to comment.