diff --git a/src/Parsing/V2/Field/FieldConfidence.php b/src/Parsing/V2/Field/FieldConfidence.php index 9caeff94..76ce76bf 100644 --- a/src/Parsing/V2/Field/FieldConfidence.php +++ b/src/Parsing/V2/Field/FieldConfidence.php @@ -8,4 +8,69 @@ enum FieldConfidence: string case High = 'High'; case Medium = 'Medium'; case Low = 'Low'; + + /** + * @return integer Rank of the value. + */ + public function rank(): int + { + return match ($this) { + self::Low => 1, + self::Medium => 2, + self::High => 3, + self::Certain => 4, + }; + } + + /** + * Shorthand for the '<=' operator. + * @param FieldConfidence $other Other confidence value. + * @return boolean True if this confidence is lower than or equal to the other. + */ + public function lte(FieldConfidence $other): bool + { + return $this->rank() <= $other->rank(); + } + + /** + * Shorthand for the '>=' operator. + * @param FieldConfidence $other Other confidence value. + * @return boolean True if this confidence is greater than or equal to the other. + */ + public function gte(FieldConfidence $other): bool + { + return $this->rank() >= $other->rank(); + } + + /** + * Shorthand for the '<' operator. + * @param FieldConfidence $other Other confidence value. + * @return boolean True if this confidence is lower than the other. + */ + public function lt(FieldConfidence $other): bool + { + return $this->rank() < $other->rank(); + } + + + /** + * Shorthand for the '>' operator. + * @param FieldConfidence $other Other confidence value. + * @return boolean True if this confidence is greater than the other. + */ + public function gt(FieldConfidence $other): bool + { + return $this->rank() > $other->rank(); + } + + + /** + * Shorthand for the '==' operator. + * @param FieldConfidence $other Other confidence value. + * @return boolean True if this confidence is equal to the other. + */ + public function eq(FieldConfidence $other): bool + { + return $this === $other; + } } diff --git a/tests/V2/InferenceTest.php b/tests/V2/InferenceTest.php index ce1ad465..bf04c37f 100644 --- a/tests/V2/InferenceTest.php +++ b/tests/V2/InferenceTest.php @@ -350,6 +350,12 @@ public function testCoordinatesAndLocationDataMustBeAccessible(): void $location->polygon->getCentroid() ); $this->assertEquals(FieldConfidence::Medium, $dateField->confidence); + $this->assertEquals(FieldConfidence::Medium->rank(), $dateField->confidence->rank()); + $this->assertTrue(FieldConfidence::Medium->eq($dateField->confidence)); + $this->assertLessThan(FieldConfidence::High->rank(), $dateField->confidence->rank()); + $this->assertTrue(FieldConfidence::High->gt($dateField->confidence)); + $this->assertGreaterThan(FieldConfidence::Low->rank(), $dateField->confidence->rank()); + $this->assertTrue(FieldConfidence::Low->lt($dateField->confidence)); $this->assertEquals('Medium', $dateField->confidence->value); } }