PHP code example of mathiasonea / laravel-rulebook
1. Go to this page and download the library: Download mathiasonea/laravel-rulebook library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
if ($invoice->issued_at < new DateTimeImmutable('2026-01-01T00:00:00+01:00')) {
return $this->priceUnder2025Policy($vehicle);
}
if ($invoice->issued_at < new DateTimeImmutable('2027-01-01T00:00:00+01:00')) {
return $this->priceUnder2026Policy($vehicle);
}
return $this->currentPrice($vehicle);
namespace App\Pricing;
use App\Models\Vehicle;
use MathiasOnea\Rulebook\Rulebook;
/**
* @extends Rulebook<Vehicle, VehiclePricingContext, Money>
*/
final class VehiclePricingRulebook extends Rulebook
{
protected function rules(): array
{
return [
DefaultVehiclePrice::class,
AustrianVehiclePrice::class,
AustrianElectricVehiclePrice2025::class,
AustrianElectricVehiclePrice2026::class,
AustrianElectricVehiclePrice2027::class,
];
}
}
namespace App\Pricing;
use App\Models\Vehicle;
use MathiasOnea\Rulebook\Inputs\RuleInput;
use MathiasOnea\Rulebook\Results\RuleResult;
use MathiasOnea\Rulebook\Rule;
/**
* @extends Rule<Vehicle, VehiclePricingContext, Money>
*/
abstract class AustrianElectricVehiclePrice extends Rule
{
public function priority(): int
{
return 100;
}
public function evaluate(RuleInput $input): RuleResult
{
$vehicle = $input->subject(Vehicle::class);
$context = $input->context(VehiclePricingContext::class);
if (! $vehicle->isElectric()) {
return RuleResult::doesNotApply(
reason: 'The vehicle is not electric.',
);
}
if ($context->country !== Country::Austria) {
return RuleResult::doesNotApply(
reason: 'The pricing country is not Austria.',
);
}
return RuleResult::applies(
outcome: Money::EUR(
$this->basePriceInCents()
- $this->incentiveInCents()
+ ($vehicle->batteryCapacityInKwh * $this->batteryFeePerKwhInCents()),
),
reason: "The {$this->policyYear()} Austrian electric-vehicle price applies.",
);
}
abstract protected function policyYear(): int;
abstract protected function basePriceInCents(): int;
abstract protected function incentiveInCents(): int;
abstract protected function batteryFeePerKwhInCents(): int;
}
use DateTimeImmutable;
use MathiasOnea\Rulebook\Periods\ValidityPeriod;
final class AustrianElectricVehiclePrice2025 extends AustrianElectricVehiclePrice
{
public function validity(): ValidityPeriod
{
return ValidityPeriod::between(
from: new DateTimeImmutable('2025-01-01T00:00:00+01:00'),
until: new DateTimeImmutable('2026-01-01T00:00:00+01:00'),
);
}
protected function policyYear(): int { return 2025; }
protected function basePriceInCents(): int { return 35_000_00; }
protected function incentiveInCents(): int { return 4_000_00; }
protected function batteryFeePerKwhInCents(): int { return 0; }
}
final class AustrianElectricVehiclePrice2026 extends AustrianElectricVehiclePrice
{
public function validity(): ValidityPeriod
{
return ValidityPeriod::between(
from: new DateTimeImmutable('2026-01-01T00:00:00+01:00'),
until: new DateTimeImmutable('2027-01-01T00:00:00+01:00'),
);
}
protected function policyYear(): int { return 2026; }
protected function basePriceInCents(): int { return 35_000_00; }
protected function incentiveInCents(): int { return 2_800_00; }
protected function batteryFeePerKwhInCents(): int { return 4_00; }
}
final class AustrianElectricVehiclePrice2027 extends AustrianElectricVehiclePrice
{
public function validity(): ValidityPeriod
{
return ValidityPeriod::between(
from: new DateTimeImmutable('2027-01-01T00:00:00+01:00'),
until: new DateTimeImmutable('2028-01-01T00:00:00+01:00'),
);
}
protected function policyYear(): int { return 2027; }
protected function basePriceInCents(): int { return 35_500_00; }
protected function incentiveInCents(): int { return 1_000_00; }
protected function batteryFeePerKwhInCents(): int { return 5_00; }
}
$decision = $rulebook->resolveAt(
subject: new Vehicle(
electric: true,
batteryCapacityInKwh: 75,
),
at: new DateTimeImmutable('2026-06-15T10:00:00+02:00'),
context: new VehiclePricingContext(country: Country::Austria),
);
$decision->winningRule(); // an AustrianElectricVehiclePrice2026 instance
$decision->outcome(); // EUR 32,500.00
$decision->winningResult()->reason();
// "The 2026 Austrian electric-vehicle price applies."
$rulebook->resolveAt($vehicle, new DateTimeImmutable('2025-07-01T00:00:00+02:00'), $context)
->winningRule(); // AustrianElectricVehiclePrice2025
$rulebook->resolveAt($vehicle, new DateTimeImmutable('2027-07-01T00:00:00+02:00'), $context)
->winningRule(); // AustrianElectricVehiclePrice2027
$decision->outcome(); // Money
$decision->winningRule(); // the selected Rule instance
$decision->winningRuleKey(); // the stable key captured for the winner
$decision->winningResult(); // outcome and mandatory reason
$decision->evaluations(); // every RuleEvaluation
$decision->evaluationFor($key); // one evaluation by stable rule key
$decision->applicableRules(); //