Skip to content

Commit 40143c8

Browse files
committed
use enum for world peace
1 parent 6a4408e commit 40143c8

6 files changed

Lines changed: 14 additions & 87 deletions

File tree

src/Geometry/BBox.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ public function getMaxY(): float
8585
/**
8686
* Extends the BBox with the provided points.
8787
*
88-
* @param Polygon|array $points Series of points to add to the BBox.
88+
* @param array|Polygon $points Series of points to add to the BBox.
8989
* @return void
9090
*/
91-
public function extendWith($points)
91+
public function extendWith(Polygon|array $points): void
9292
{
9393
if ($points instanceof Polygon) {
9494
$sequence = $points->getCoordinates();

src/Parsing/Standard/BaseField.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
namespace Mindee\Parsing\Standard;
44

5-
use Mindee\Geometry\BBox;
6-
use Mindee\Geometry\Point;
7-
use Mindee\Geometry\Polygon;
8-
9-
use function Mindee\Geometry\createBoundingBoxFrom;
10-
115
/**
126
* Base class for most fields.
137
*/
@@ -57,7 +51,7 @@ public function __construct(
5751
/**
5852
* Compares with the value of another field.
5953
*
60-
* @param \Mindee\Parsing\Standard\BaseField $obj Field to compare.
54+
* @param BaseField $obj Field to compare.
6155
* @return boolean
6256
*/
6357
public function __compare(BaseField $obj): bool

src/Parsing/Standard/StringField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class StringField extends BaseField
1717
/**
1818
* @var string|null The value as it appears on the document.
1919
*/
20-
public $rawValue;
20+
public string|null $rawValue;
2121

2222

2323
/**

src/Parsing/V2/Field/BaseField.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,27 @@ public function __construct(array $rawPrediction, int $indentLevel = 0)
3737
}
3838
}
3939
if (array_key_exists("confidence", $rawPrediction) && $rawPrediction["confidence"]) {
40-
$this->confidence = new FieldConfidence($rawPrediction["confidence"]);
40+
$this->confidence = FieldConfidence::from($rawPrediction["confidence"]);
4141
}
4242
}
4343

4444
/**
4545
* @param array $rawPrediction Raw prediction array.
4646
* @param integer $indentLevel Level of indentation for rst display.
47-
* @return BaseField
47+
* @return ListField|ObjectField|SimpleField
4848
* @throws MindeeApiException Throws if the field type isn't recognized.
4949
*/
50-
public static function createField(array $rawPrediction, int $indentLevel = 0)
50+
public static function createField(array $rawPrediction, int $indentLevel = 0): ListField|ObjectField|SimpleField
5151
{
5252
if (array_key_exists('items', $rawPrediction)) {
5353
return new ListField($rawPrediction, $indentLevel);
5454
}
55-
5655
if (array_key_exists('fields', $rawPrediction)) {
5756
return new ObjectField($rawPrediction, $indentLevel);
5857
}
59-
6058
if (array_key_exists('value', $rawPrediction)) {
6159
return new SimpleField($rawPrediction, $indentLevel);
6260
}
63-
6461
throw new MindeeApiException(
6562
sprintf('Unrecognized field format in %s.', json_encode($rawPrediction))
6663
);

src/Parsing/V2/Field/FieldConfidence.php

Lines changed: 5 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,10 @@
22

33
namespace Mindee\Parsing\V2\Field;
44

5-
use InvalidArgumentException;
6-
7-
/**
8-
* Confidence level of a field as returned by the V2 API.
9-
*/
10-
class FieldConfidence
5+
enum FieldConfidence: string
116
{
12-
public const CERTAIN = 'Certain';
13-
public const HIGH = 'High';
14-
public const MEDIUM = 'Medium';
15-
public const LOW = 'Low';
16-
17-
/**
18-
* @var string
19-
*/
20-
private string $value;
21-
22-
/**
23-
* @param string $value Vale provided.
24-
* @throws InvalidArgumentException Throws if an invalid value is provided.
25-
*/
26-
public function __construct(string $value)
27-
{
28-
if (!self::isValid($value)) {
29-
throw new InvalidArgumentException(sprintf(
30-
'Invalid confidence value "%s". Valid values are: %s',
31-
$value,
32-
implode(', ', self::getValidValues())
33-
));
34-
}
35-
$this->value = $value;
36-
}
37-
38-
/**
39-
* @param string $value Value to check.
40-
* @return boolean True if the value is valid.
41-
*/
42-
public static function isValid(string $value): bool
43-
{
44-
return in_array($value, self::getValidValues(), true);
45-
}
46-
47-
/**
48-
* @return array<string>
49-
*/
50-
public static function getValidValues(): array
51-
{
52-
return [
53-
self::CERTAIN,
54-
self::HIGH,
55-
self::MEDIUM,
56-
self::LOW,
57-
];
58-
}
59-
60-
/**
61-
* @return string String representation.
62-
*/
63-
public function getValue(): string
64-
{
65-
return $this->value;
66-
}
67-
68-
/**
69-
* @return string
70-
*/
71-
public function __toString(): string
72-
{
73-
return $this->value;
74-
}
7+
case Certain = 'Certain';
8+
case High = 'High';
9+
case Medium = 'Medium';
10+
case Low = 'Low';
7511
}

tests/Parsing/V2/InferenceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ public function testCoordinatesAndLocationDataMustBeAccessible(): void
335335
new Point(0.9015345, 0.23731850000000002),
336336
$location->polygon->getCentroid()
337337
);
338-
$this->assertEquals(FieldConfidence::MEDIUM, $dateField->confidence);
339-
$this->assertEquals('Medium', $dateField->confidence->getValue());
338+
$this->assertEquals(FieldConfidence::Medium, $dateField->confidence);
339+
$this->assertEquals('Medium', $dateField->confidence->value);
340340
}
341341
}

0 commit comments

Comments
 (0)