Skip to content

Commit bd22968

Browse files
authored
🐛 int values should be cast to float (#145)
1 parent e607a51 commit bd22968

3 files changed

Lines changed: 18 additions & 8 deletions

File tree

src/Parsing/V2/Field/SimpleField.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class SimpleField extends BaseField
99
{
1010
/**
11-
* @var string | integer | float | boolean | null Value contained in the field.
11+
* @var string|float|boolean|null Value contained in the field.
1212
*/
1313
public $value;
1414

@@ -20,6 +20,9 @@ public function __construct(array $serverResponse, int $indentLevel = 0)
2020
{
2121
parent::__construct($serverResponse, $indentLevel);
2222
$this->value = array_key_exists('value', $serverResponse) ? $serverResponse['value'] : null;
23+
if (is_int($this->value)) {
24+
$this->value = (float) $this->value;
25+
}
2326
}
2427

2528
/**
@@ -31,12 +34,8 @@ public function __toString(): string
3134
return $this->value ? 'True' : 'False';
3235
}
3336
if (is_numeric($this->value)) {
34-
$value = (float)$this->value;
35-
return $value == (int)$value ?
36-
number_format($value, 1, '.', '') :
37-
rtrim(rtrim(number_format($value, 5, '.', ''), '0'), '.');
37+
return number_format($this->value, 1, '.', '');
3838
}
39-
4039
return $this->value !== null ? (string)$this->value : '';
4140
}
4241
}

tests/Input/URLInputSourceTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,32 @@ protected function setUp(): void
2323
getenv('GITHUB_WORKSPACE') ?: "."
2424
) . "/tests/resources/file_types/";
2525
}
26+
2627
protected function tearDown(): void
2728
{
2829
putenv('MINDEE_API_KEY=' . $this->oldKey);
2930
}
31+
3032
public function testInputFromHTTPShouldNotThrow()
3133
{
3234
$inputDoc = $this->dummyClient->sourceFromUrl("https://example.com/invoice.pdf");
3335
$this->assertInstanceOf(URLInputSource::class, $inputDoc);
3436
}
37+
3538
public function testInputFromHTTPShouldThrow()
3639
{
3740
$this->expectException(MindeeSourceException::class);
38-
$this->dummyClient->sourceFromUrl("http://example.com/invoice.pdf");
41+
new URLInputSource(url: "http://example.com/invoice.pdf");
3942
}
43+
4044
public function testDownloadFileFails(){
4145
$dummyAddress = "addressthatdoesntworkforcipurposes";
4246
$urlSource = $this->dummyClient->sourceFromUrl("https://$dummyAddress");
4347
$this->expectException(MindeeSourceException::class);
4448
$this->expectExceptionMessage("Failed to download file: Could not resolve host: $dummyAddress");
4549
$urlSource->asLocalInputSource("test.pdf");
4650
}
51+
4752
public function testInvalidFileName(){
4853
$urlSource = $this->dummyClient->sourceFromUrl("https://addressthatdoesntworkforcipurposes");
4954
$this->expectException(MindeeSourceException::class);

tests/Parsing/V2/InferenceTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ public function testRawTextsMustBeAccessible(): void
282282

283283
$first = $rawText->pages[0];
284284
$this->assertEquals('This is the raw text of the first page...', $first->content);
285+
286+
foreach ($rawText->pages as $page) {
287+
$this->assertEquals('string', gettype($page->content));
288+
}
285289
}
286290

287291
/**
@@ -305,7 +309,9 @@ public function testCoordinatesAndLocationDataMustBeAccessible(): void
305309
$inference = $response->inference;
306310
$this->assertNotNull($inference);
307311

308-
$dateField = $inference->result->fields->getSimpleField('date');
312+
$fields = $response->inference->result->fields;
313+
314+
$dateField = $fields->getSimpleField('date');
309315
$this->assertCount(1, $dateField->locations);
310316

311317
$location = $dateField->locations[0];

0 commit comments

Comments
 (0)