The Trip Cost Calculator provides easy functionality for evaluating the total cost for a pre-defined route that your vehicle will travel. The parameters supported and considered in cost calculation are:
- route distance in km
- route travelling time
- driver salary hourly rate
- vehicle wear and tear costs
The library is using PHP-DI
<?php
use DI\ContainerBuilder;
use Trip\Calculator\TripCalculator;
use Trip\Calculator;
$apiKey = "";
$diBuilder = new ContainerBuilder();
$diBuilder->addDefinitions(
[
'Trip\Calculator\Interfaces\GeoService' => function (ContainerInterface $c) {
return $geoService = new OpenRoute($apiKey);
}
]
);
$diContainer = $diBuilder->build();
$tripCalculator = $diContainer->get("Trip\Calculator\TripCalculator");
In the above code segment, we're using PHP-DI to autowire the dependencies. We're also configuring which Geo Service API to use. Above, we're setting the OpenRoute Service. Adding other alternatives is planned future work for the library.
To properly calculate the trip costs, you need to assign the different values.
$vehicle = $tripCalculator->getVehicle();
$vehicle->name = "Vehicle 1";
$vehicle->fuelLitrePerHundred = "9";
$vehicle->wearTearHourly = "5";
$driver = $tripCalculator->getDriver();
$driver->name = "Chris";
$driver->hourlyRate = "10.50";
$trip = $tripCalculator->getTrip();
$trip->name = "Test trip";
## set the points (coordinates) that need to be included in the trip calculation
$trip->addPoint(new Point(14.4819237, 35.920715));
$trip->addPoint(new Point(14.4526501, 35.9366694));
After setting the configurations required for the objects as defined above, you need to then call the calculation process, which will use third party APIs.
$totalCost = $tripCalculator->calculateCost();
The above will give you the total cost in Euro, but more details are populated in the trip object.
$travelledKilometers = $trip->travelledKilometers;
$travelTimeMinutes = $trip->travelTimeMinutes;
$geometryDirections = $tripCalculator->getGeoService()->getDirections();
All third party libraries are listed in the composer.json, and have been left as is without modification.
- php 7.4 or above
- ext-decimal / php-decimal
- composer
Chris Bartolo - chris@chrisbartolo.com
The Trip Cost Calculator is licenses under the MIT license