The library has been reorganized to provide an immutable Odds class that contains all supported odds formats (Decimal, Fractional, Moneyline) and calculated probability. The OddsFactory class is used to create Odds objects with configurable odds ladder support via dependency injection.
- Immutable Odds Class: Once created, odds values cannot be changed
- All Formats Included: Each
Oddsobject contains decimal, fractional, moneyline, and probability - Dependency Injection: Inject custom odds ladder implementations
- Extensible Design: Base odds ladder class can be extended with custom lookup tables
- Type Safety: Full type hints and strict validation
- Simplified API: Only two classes needed -
OddsandOddsFactory
use Praetorian\Formatter\Odds\OddsFactory;
// Create factory with default mathematical conversion
$factory = new OddsFactory();
// Create odds from decimal
$odds = $factory->fromDecimal(2.5);
echo $odds->getDecimal(); // 2.5
echo $odds->getFractional(); // 3/2 (mathematical conversion)
echo $odds->getMoneyline(); // +150
echo $odds->getProbability(); // 0.4
// Create odds from fractional
$odds = $factory->fromFractional(3, 2); // 3/2
echo $odds->getDecimal(); // 2.5
echo $odds->getFractional(); // 3/2
echo $odds->getMoneyline(); // +150
echo $odds->getProbability(); // 0.4
// Create odds from moneyline
$odds = $factory->fromMoneyline(-150);
echo $odds->getDecimal(); // 1.67
echo $odds->getFractional(); // 67/100 (mathematical conversion)
echo $odds->getMoneyline(); // -150
echo $odds->getProbability(); // 0.6use Praetorian\Formatter\Odds\OddsFactory;
use Praetorian\Formatter\Odds\OddsLadder;
// Create factory with standard odds ladder
$factory = new OddsFactory(new OddsLadder());
$odds = $factory->fromDecimal(1.91);
echo $odds->getFractional(); // 20/21 (from odds ladder)use Praetorian\Formatter\Odds\OddsLadder;
class MyCustomOddsLadder extends OddsLadder
{
protected function getLadder(): array
{
return [
'1.25' => '1/4',
'1.50' => '1/2',
'2.00' => 'evens',
'3.00' => '2/1',
'4.00' => '3/1',
];
}
// Optionally override fallback behavior
protected function fallbackConversion(float $decimal): string
{
return round($decimal - 1) . '/1';
}
}
// Use your custom ladder
$customFactory = new OddsFactory(new MyCustomOddsLadder());
$odds = $customFactory->fromDecimal(1.9);
echo $odds->getFractional(); // 'evens' (from custom ladder)$defaultFactory = new OddsFactory();
$ladderFactory = new OddsFactory(new OddsLadder());
$customFactory = new OddsFactory(new MyCustomOddsLadder());
$decimal = 2.0;
echo $defaultFactory->fromDecimal($decimal)->getFractional(); // 1/1 (mathematical)
echo $ladderFactory->fromDecimal($decimal)->getFractional(); // 19/10 (standard ladder)
echo $customFactory->fromDecimal($decimal)->getFractional(); // 2/1 (custom ladder fallback)The Odds class is immutable and provides the following getters:
getDecimal(): float- Returns the decimal odds valuegetFractional(): string- Returns the fractional odds as a string (e.g., "3/2")getMoneyline(): string- Returns the moneyline odds with appropriate sign (e.g., "+150", "-200")getProbability(): float- Returns the calculated probability (0.0 to 1.0)
The OddsFactory class provides factory methods to create Odds objects:
fromDecimal(float $decimal): Odds- Create odds from decimal valuefromFractional(int $numerator, int $denominator): Odds- Create odds from fractional valuesfromMoneyline(float $moneyline): Odds- Create odds from moneyline value
new OddsFactory(?OddsLadderInterface $oddsLadder = null)$oddsLadder- Optional odds ladder implementation. Ifnull, uses mathematical conversion.
- Implements
OddsLadderInterface - Provides standard betting industry odds ladder
- Can be extended for custom behavior
Extend the base class and override:
getLadder(): array- Return your custom lookup tablefallbackConversion(float $decimal): string- Handle values not in the ladder
$decimalOdd = new DecimalOdd(2.5);
$fractional = $decimalOdd->toFractional();
$moneyline = $decimalOdd->toMoneyline();$factory = new OddsFactory();
$odds = $factory->fromDecimal(2.5);
$decimal = $odds->getDecimal();
$fractional = $odds->getFractional();
$moneyline = $odds->getMoneyline();
$probability = $odds->getProbability(); // New!When no odds ladder is injected (default):
- Uses precise mathematical conversion with continued fractions algorithm
- More accurate for uncommon odds values
- Results in mathematically correct fractions
When new OddsLadder() is injected:
- Uses predefined lookup table matching betting industry standards
- Provides "rounded" fractions that bookmakers typically use
- Better for user-facing applications where standard betting fractions are expected
When custom implementation is injected:
- Complete control over conversion behavior
- Define your own lookup table and fallback logic
- Perfect for specific business requirements or regional standards
- Dependency Injection: Clean, testable, flexible architecture
- Extensibility: Easy to create custom odds ladder implementations
- Immutability: No risk of accidental modification
- Complete Information: All formats calculated once at creation
- Probability Included: No need for separate calculation
- Type Safe: Full PHP 8+ type hints with interfaces
- Simplified: Clean API with dependency injection pattern