Skip to content

Commit e4077c4

Browse files
committed
make point an array
1 parent e025221 commit e4077c4

9 files changed

Lines changed: 196 additions & 51 deletions

File tree

.github/workflows/_test-code-samples.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
matrix:
1414
php-version:
1515
- "7.4"
16-
- "8.2"
16+
- "8.3"
1717
runs-on: ubuntu-latest
1818
steps:
1919
- uses: actions/checkout@v4

.github/workflows/_test-units.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
uses: shivammathur/setup-php@v2
2525
with:
2626
php-version: ${{ matrix.php-version }}
27-
- uses: ramsey/composer-install@v2
27+
- uses: ramsey/composer-install@v3
2828
- name: Unit testing with phpunit
2929
env:
3030
MINDEE_API_KEY: ${{ secrets.MINDEE_API_KEY_SE_TESTS }}
@@ -55,7 +55,7 @@ jobs:
5555
uses: shivammathur/setup-php@v2
5656
with:
5757
php-version: ${{ matrix.php-version }}
58-
- uses: ramsey/composer-install@v2
58+
- uses: ramsey/composer-install@v3
5959
- name: Change ImageMagick security policy on Ubuntu
6060
run: |
6161
DQT='"'
@@ -79,7 +79,7 @@ jobs:
7979
- 8.1
8080
- 8.2
8181
- 8.3
82-
runs-on: "macos-latest"
82+
runs-on: "macos-14"
8383
steps:
8484
- uses: actions/checkout@v4
8585
with:

src/Geometry/Point.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Mindee\Geometry;
44

5+
use ArrayAccess;
6+
use InvalidArgumentException;
7+
58
/**
69
* Representation of the coordinates of a point.
710
*/
8-
class Point
11+
class Point implements ArrayAccess
912
{
1013
/**
1114
* @var float X coordinate.
@@ -45,4 +48,68 @@ public function getY(): float
4548
{
4649
return $this->y;
4750
}
51+
52+
/**
53+
* Whether an offset exists.
54+
* @param integer|string $offset Use 0 or 1.
55+
* @return boolean
56+
*/
57+
public function offsetExists($offset): bool
58+
{
59+
if ($offset === 0 || $offset === 1) {
60+
return true;
61+
}
62+
return false;
63+
}
64+
65+
/**
66+
* Get an offset value.
67+
* @param integer|string $offset Use 0 or 1.
68+
* @return float
69+
* @throws InvalidArgumentException If the offset is not 0 or 1.
70+
*/
71+
public function offsetGet($offset): float
72+
{
73+
if ($offset === 0) {
74+
return $this->x;
75+
} elseif ($offset === 1) {
76+
return $this->y;
77+
}
78+
throw new InvalidArgumentException("Use 0 for X or 1 for Y");
79+
}
80+
81+
/**
82+
* Set an offset value.
83+
* @param integer|string $offset Use 0 or 1.
84+
* @param float|integer|string $value Coordinate value to set.
85+
* @return void
86+
* @throws InvalidArgumentException If the offset is not 0 or 1.
87+
*/
88+
public function offsetSet($offset, $value): void
89+
{
90+
if ($offset === 0) {
91+
$this->x = $value;
92+
} elseif ($offset === 1) {
93+
$this->y = $value;
94+
} else {
95+
throw new InvalidArgumentException("Use 0 for X or 1 for Y");
96+
}
97+
}
98+
99+
/**
100+
* Get an offset value.
101+
* @param integer|string $offset Use 0 or 1.
102+
* @return void
103+
* @throws InvalidArgumentException If the offset is not 0 or 1.
104+
*/
105+
public function offsetUnset($offset): void
106+
{
107+
if ($offset === 0) {
108+
unset($this->x);
109+
} elseif ($offset === 1) {
110+
unset($this->y);
111+
} else {
112+
throw new InvalidArgumentException("Use 0 for X or 1 for Y");
113+
}
114+
}
48115
}

src/Geometry/Polygon.php

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class Polygon
99
{
1010
/**
11-
* @var array|null Vertices of the polygon.
11+
* @var Point[]|null Vertices of the polygon.
1212
*/
1313
public ?array $coordinates;
1414

@@ -17,7 +17,14 @@ class Polygon
1717
*/
1818
public function __construct(?array $coordinates = null)
1919
{
20-
$this->coordinates = $coordinates;
20+
if (!is_null($coordinates)) {
21+
$this->coordinates = [];
22+
foreach ($coordinates as $point) {
23+
$this->coordinates[] = new Point($point[0], $point[1]);
24+
}
25+
} else {
26+
$this->coordinates = null;
27+
}
2128
}
2229

2330
/**
@@ -30,6 +37,50 @@ public function getCentroid(): Point
3037
return PolygonUtils::getCentroid($this->coordinates);
3138
}
3239

40+
/**
41+
* Retrieves the upper and lower bounds of the y-axis.
42+
*
43+
* @return MinMax
44+
*/
45+
public function getMinMaxY(): MinMax
46+
{
47+
return MinMaxUtils::getMinMaxY($this->coordinates);
48+
}
49+
50+
/**
51+
* Retrieves the upper and lower bounds of the x-axis.
52+
*
53+
* @return MinMax
54+
*/
55+
public function getMinMaxX(): MinMax
56+
{
57+
return MinMaxUtils::getMinMaxX($this->coordinates);
58+
}
59+
60+
/**
61+
* Checks whether a point is located within the polygon's y-axis.
62+
*
63+
* @param Point $point Point to check.
64+
* @return boolean
65+
*/
66+
public function isPointInY(Point $point): bool
67+
{
68+
$minMax = $this->getMinMaxY();
69+
return PolygonUtils::isPointInY($point, $minMax->getMin(), $minMax->getMax());
70+
}
71+
72+
/**
73+
* Checks whether a point is located within the polygon's x-axis.
74+
*
75+
* @param Point $point Point to check.
76+
* @return boolean
77+
*/
78+
public function isPointInX(Point $point): bool
79+
{
80+
$minMax = $this->getMinMaxX();
81+
return PolygonUtils::isPointInX($point, $minMax->getMin(), $minMax->getMax());
82+
}
83+
3384
/**
3485
* Checks whether the Polygon has coordinates.
3586
*
@@ -58,7 +109,6 @@ public function __toString()
58109
if (!$this->isEmpty()) {
59110
return 'Polygon with ' . count($this->getCoordinates()) . ' points.';
60111
}
61-
62112
return '';
63113
}
64114
}

src/Geometry/PolygonUtils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ public static function quadrilateralFromPrediction(array $prediction): Polygon
219219
/**
220220
* Generates a Polygon from a given prediction.
221221
*
222+
* @deprecated construct a new Polygon() instead.
222223
* @param array $prediction Raw prediction array.
223224
* @return Polygon
224225
*/
@@ -228,7 +229,6 @@ public static function polygonFromPrediction(array $prediction): Polygon
228229
foreach ($prediction as $point) {
229230
$points[] = new Point($point[0], $point[1]);
230231
}
231-
232232
return new Polygon($points);
233233
}
234234

src/Parsing/Common/Extras/Extras.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,30 @@
1212
class Extras
1313
{
1414
/**
15-
* @var \Mindee\Parsing\Common\Extras\CropperExtra|null Cropper extra.
15+
* @var CropperExtra|null Cropper extra.
1616
*/
1717
public ?CropperExtra $cropper;
1818
/**
19-
* @var \Mindee\Parsing\Common\Extras\CropperExtra|null Full text OCR extra.
19+
* @var FullTextOcrExtra|null Full text OCR extra.
2020
*/
2121
public ?FullTextOcrExtra $fullTextOcr;
2222
/**
23-
* @var \Mindee\Parsing\Common\Extras\RagExtra|null Rag Extra.
23+
* @var RagExtra|null Rag Extra.
2424
*/
2525
public ?RagExtra $rag;
2626
/**
2727
* @var array Other extras.
2828
*/
2929
private array $data;
3030

31-
3231
/**
3332
* Sets a field.
3433
*
35-
* @param mixed $varName Name of the field to set.
36-
* @param mixed $value Value to set the field with.
34+
* @param string $varName Name of the field to set.
35+
* @param mixed $value Value to set the field with.
3736
* @return void
3837
*/
39-
public function __set($varName, $value)
38+
public function __set(string $varName, $value)
4039
{
4140
$this->data[$varName] = $value;
4241
}

src/Parsing/Standard/PositionField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private static function getQuadrilateral(array $rawPrediction, string $key): ?Po
5757
private static function getPolygon(array $rawPrediction, string $key): ?Polygon
5858
{
5959
if (array_key_exists($key, $rawPrediction)) {
60-
return PolygonUtils::polygonFromPrediction($rawPrediction[$key]);
60+
return new Polygon($rawPrediction[$key]);
6161
}
6262

6363
return null;

tests/ClientV2TestFunctional.php

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,33 @@ protected function setUp(): void
2424
public function testParseFileEmptyMultiPageMustSucceed(): void
2525
{
2626
$source = new PathInput(__DIR__ . '/resources/file_types/pdf/multipage_cut-2.pdf');
27-
28-
$options = new InferenceParameters($this->modelId);
27+
$options = new InferenceParameters($this->modelId, false, true);
2928

3029
$response = $this->mindeeClient->enqueueAndGetInference($source, $options);
31-
3230
$this->assertNotNull($response);
33-
$this->assertNotNull($response->inference);
31+
$inference = $response->inference;
32+
$this->assertNotNull($inference);
33+
34+
$file = $inference->file;
35+
$this->assertNotNull($file);
36+
$this->assertEquals('multipage_cut-2.pdf', $file->name);
37+
$this->assertEquals(2, $file->pageCount);
38+
39+
$this->assertNotNull($inference->model);
40+
$this->assertEquals($this->modelId, $inference->model->id);
3441

35-
$this->assertNotNull($response->inference->file);
36-
$this->assertEquals('multipage_cut-2.pdf', $response->inference->file->name);
42+
$activeOptions = $inference->activeOptions;
43+
$this->assertTrue($activeOptions->rawText, "Raw text must be enabled");
44+
$this->assertFalse($activeOptions->polygon, "Polygon must be disabled by default");
45+
$this->assertFalse($activeOptions->confidence, "Confidence must be disabled by default");
46+
$this->assertFalse($activeOptions->rag, "RAG must be disabled by default");
3747

38-
$this->assertNotNull($response->inference->model);
39-
$this->assertEquals($this->modelId, $response->inference->model->id);
48+
$result = $inference->result;
49+
$this->assertNotNull($result);
4050

41-
$this->assertNotNull($response->inference->result);
42-
$this->assertNull($response->inference->result->options ?? null);
51+
$rawText = $result->rawText;
52+
$this->assertNotNull($rawText);
53+
$this->assertCount(2, $rawText->pages);
4354
}
4455

4556
/**
@@ -52,21 +63,25 @@ public function testParseFileFilledSinglePageMustSucceed(): void
5263
$options = new InferenceParameters($this->modelId, false);
5364

5465
$response = $this->mindeeClient->enqueueAndGetInference($source, $options);
55-
5666
$this->assertNotNull($response);
57-
$this->assertNotNull($response->inference);
67+
$inference = $response->inference;
68+
$this->assertNotNull($inference);
69+
70+
$file = $inference->file;
71+
$this->assertNotNull($file);
72+
$this->assertEquals('default_sample.jpg', $file->name);
73+
$this->assertEquals(1, $file->pageCount);
5874

59-
$this->assertNotNull($response->inference->file);
60-
$this->assertEquals('default_sample.jpg', $response->inference->file->name);
75+
$this->assertNotNull($inference->model);
76+
$this->assertEquals($this->modelId, $inference->model->id);
6177

62-
$this->assertNotNull($response->inference->model);
63-
$this->assertEquals($this->modelId, $response->inference->model->id);
78+
$result = $inference->result;
79+
$this->assertNotNull($result);
6480

65-
$this->assertNotNull($response->inference->result);
66-
$this->assertNotNull($response->inference->result->fields);
67-
$this->assertNotNull($response->inference->result->fields['supplier_name'] ?? null);
81+
$this->assertNotNull($result->fields);
82+
$this->assertNotNull($result->fields['supplier_name'] ?? null);
6883

69-
$supplierName = $response->inference->result->fields['supplier_name']->value ?? null;
84+
$supplierName = $result->fields['supplier_name']->value ?? null;
7085
$this->assertEquals(
7186
'John Smith',
7287
$supplierName
@@ -100,8 +115,14 @@ public function testUrlInputSourceMustNotRaiseErrors(): void
100115
$options = new InferenceParameters($this->modelId);
101116

102117
$response = $this->mindeeClient->enqueueAndGetInference($urlSource, $options);
103-
104118
$this->assertNotNull($response);
105-
$this->assertNotNull($response->inference);
119+
$inference = $response->inference;
120+
$this->assertNotNull($inference);
121+
122+
$file = $inference->file;
123+
$this->assertNotNull($file);
124+
125+
$result = $inference->result;
126+
$this->assertNotNull($result);
106127
}
107128
}

0 commit comments

Comments
 (0)