Skip to content

Commit

Permalink
feat: add support for float number
Browse files Browse the repository at this point in the history
  • Loading branch information
brd6 committed Nov 29, 2023
1 parent 8367858 commit dda321d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/Resource/Page/PropertyValue/NumberPropertyValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

class NumberPropertyValue extends AbstractPropertyValue
{
protected ?int $number = null;
protected ?float $number = null;

protected function initialize(): void
{
$data = $this->getRawData();
$this->number = isset($data['number']) ? (int) $data['number'] : null;
$this->number = isset($data['number']) ? (float) $data['number'] : null;
}

public function getNumber(): ?int
public function getNumber(): ?float
{
return $this->number;
}

public function setNumber(int $number): self
public function setNumber(float $number): self
{
$this->number = $number;

Expand Down
8 changes: 4 additions & 4 deletions src/Resource/Property/Value/NumberValueProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@

class NumberValueProperty extends AbstractValueProperty
{
protected ?int $number = null;
protected ?float $number = null;

protected function initialize(): void
{
$this->number = isset($this->getRawData()['number']) ?
(int) $this->getRawData()['number'] :
(float) $this->getRawData()['number'] :
null;
}

public function getNumber(): ?int
public function getNumber(): ?float
{
return $this->number;
}

public function setNumber(int $number): self
public function setNumber(float $number): self
{
$this->number = $number;

Expand Down
5 changes: 5 additions & 0 deletions tests/Fixtures/client_pages_retrieve_page_properties_200.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
"type": "number",
"number": 42
},
"My number float": {
"id": "%40aiB",
"type": "number",
"number": 42.42
},
"formula2": {
"id": "H%3BxC",
"type": "formula",
Expand Down
25 changes: 25 additions & 0 deletions tests/Resource/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Brd6\NotionSdkPhp\Resource\File\Emoji;
use Brd6\NotionSdkPhp\Resource\File\External;
use Brd6\NotionSdkPhp\Resource\Page;
use Brd6\NotionSdkPhp\Resource\Page\PropertyValue\NumberPropertyValue;
use PHPUnit\Framework\TestCase;

use function count;
Expand Down Expand Up @@ -88,4 +89,28 @@ public function testPageProperties(): void
$this->assertNotEmpty($property->toArray());
}
}

public function testNumberProperty(): void
{
/** @var Page $page */
$page = Page::fromRawData(
(array) json_decode(
(string) file_get_contents('tests/Fixtures/client_pages_retrieve_page_properties_200.json'),
true,
),
);

$properties = $page->getProperties();

/** @var NumberPropertyValue $myNumber */
$myNumber = $properties['My number'];

$this->assertInstanceOf(NumberPropertyValue::class, $myNumber);
$this->assertEquals(42, $myNumber->getNumber());

$myNumberFloat = $properties['My number float'];

$this->assertInstanceOf(NumberPropertyValue::class, $myNumberFloat);
$this->assertEquals(42.42, $myNumberFloat->getNumber());
}
}

0 comments on commit dda321d

Please sign in to comment.