Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/Parsing/V2/Field/FieldConfidence.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
6 changes: 6 additions & 0 deletions tests/V2/InferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}