Skip to content

Instantly share code, notes, and snippets.

@lucablackwell
Last active November 29, 2022 09:57
Show Gist options
  • Save lucablackwell/b80aa782822268df93cf6ad52b0aeb14 to your computer and use it in GitHub Desktop.
Save lucablackwell/b80aa782822268df93cf6ad52b0aeb14 to your computer and use it in GitHub Desktop.
<?php
function sanitise_num($limit, $default) {
$sanitised = false;
$input = readline('> ');
while (!$sanitised) {
# If there are letters
if (preg_match('/[a-z]/', $input)) {
echo "No letters.\n";
$input = readline('> ');
# If there are symbols
} elseif (preg_match('/[^\p{L}\d\s@#]/u', $input)) {
echo "No symbols.\n";
$input = readline('> ');
# If there is no input
} elseif ($input == null) {
if ($default) {
$input = $default;
$sanitised = true;
} else {
echo "Enter a number.\n";
$input = readline('> ');
}
} elseif (preg_match('/[0-9]/', $input)) {
if ($limit) {
if ($input < $limit) {
$sanitised = true;
} else {
echo "Lower than $limit.\n";
$input = readline('> ');
}
} else {
$sanitised = true;
}
}
}
return $input;
}
function sanitise_str() {
$sanitised = false;
$input = readline('> ');
while (!$sanitised) {
# If there are numbers
if (preg_match('/[0-9]/', $input)) {
echo "No numbers.\n";
$input = readline('> ');
# If there are symbols
} elseif (preg_match('/[^\p{L}\d\s@#]/u', $input)) {
echo "No symbols.\n";
$input = readline('> ');
# If there is no input
} elseif ($input == null) {
echo "Enter a phrase.\n";
$input = readline('> ');
} elseif (preg_match('/[a-z]/', $input)) {
$sanitised = true;
}
}
return $input;
}
function sanitise_float() {
$sanitised = false;
$input = readline('> ');
while (!$sanitised) {
# If there are letters
if (preg_match('/[a-z]/', $input)) {
echo "No letters.\n";
$input = readline('> ');
# If there is no input
} elseif ($input == null) {
echo "Enter a phrase.\n";
$input = readline('> ');
} else {
$sanitised = true;
}
}
return $input;
}
function price($tarrif, $tarrifs, $distance, $distance_m) {
# Additional charge is always 20p
# Additional distance is always 86.9m
$tarrif = $tarrifs[$tarrif];
$price = $tarrif[0] * $distance;
if ($distance_m > $tarrif[1]) {
$price += $tarrif[2];
}
# Amount of 20ps to add
$price += .2 * (($tarrif[3] - $tarrif[1]) / $tarrif[4]);
# If longer than limit
if ($distance_m > $tarrif[3]) {
$price += .2 * (($tarrif[3] - $distance_m) / 86.9);
}
return $price;
}
$vehicles = [
['Small Car', 4],
['Large Car', 7],
['Minibus', 15]
];
$tarrifs = [
/*
per mile,
initial distance,
initial price,
distance limit,
pre-limit distance
*/
[
2.84,
227.0,
3.20,
9647.5,
113.5,
],
[
3.48,
184.8,
3.20,
9609.6,
92.4,
],
[
3.96,
162.4,
3.00,
9581.6,
81.2
]
];
# Get the number of passengers
echo "Enter the number of passengers.\n(if more than 15, please book separate taxis on subsequent use.)\n";
$passengers = sanitise_num(16, false);
# Assign vehicle based on size
# Loop through vehicles
for ($i = 0; $i < count($vehicles); $i++) {
if ($i > 0) {
# If passengers are less than or equal to the current but more than the last
if ($passengers <= $vehicles[$i][1] && $passengers > $vehicles[$i-1][1]) {
$chosen = $vehicles[$i][0];
}
# If first in array
} else {
# If passengers are less than or equal to the current
if ($passengers <= $vehicles[$i][1]) {
$chosen = $vehicles[$i][0];
}
}
}
echo "Is it a public holiday?\n";
$holiday = sanitise_str();
$holiday_bool = false;
while ($holiday_bool == false ) {
if ($holiday == 'yes' || $holiday == 'no') {
$holiday_bool = true;
} else {
echo "(Enter 'yes' or 'no')\n";
$holiday = sanitise_str();
}
}
if ($holiday == 'yes') {
$holiday = true;
} else {
$holiday = false;
}
echo "What day is it?\n";
$day = sanitise_str();
$day_bool = false;
$days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'];
while ($day_bool == false) {
if (in_array($day, $days)) {
$day_bool = true;
} else {
echo "Enter a day in the format:\nMonday / monday / Mon / mon\n";
$day = sanitise_str();
}
}
$day = ' ' . $day;
if (strpos(strtolower($day), 'mon') != 0) {
$day = 1;
} elseif (strpos(strtolower($day), 'tue') != 0) {
$day = 2;
} elseif (strpos(strtolower($day), 'wed') != 0) {
$day = 3;
} elseif (strpos(strtolower($day), 'thu') != 0) {
$day = 4;
} elseif (strpos(strtolower($day), 'fri') != 0) {
$day = 5;
} elseif (strpos(strtolower($day), 'sat') != 0) {
$day = 6;
} elseif (strpos(strtolower($day), 'sun') != 0) {
$day = 7;
}
echo "What hour is it (24 hour clock?)\n";
$time = readline('> ');
$time_bool = false;
while ($time_bool == false) {
if ($time >= 0 && $time <= 24) {
$time_bool = true;
} else {
echo "Enter the current hour in the 24 hour format (0-24)\n";
$time = readline('> ');
}
}
echo "Enter distance in miles.\n";
$distance = sanitise_float();
$distance_m = $distance * 1609.34;
# Distance over 6 miles
if ($distance > 6) {
$price = $distance * 3.70;
# Monday-Friday 05:00-20:00, non holidays
} elseif (($day >= 1 && $day <= 5 && ($time > 5 && $time <= 20)) && !$holiday) {
$tarrif = 0;
$price = price(0, $tarrifs, $distance, $distance_m);
# Monday-Friday 20:00-22:00 or Saturday/Sunday 05:00-22:00
} elseif ((($day >= 1 && $day <= 5 && ($time > 20 && $time <= 22)) || ($day == 6 || $day == 7 && ($time > 5 && $time <= 20))) && !$holiday) {
$price = price(1, $tarrifs, $distance, $distance_m);
# Any day 22:00-05:00 or on holidays
} elseif (($time > 22 || $time <= 5) || $holiday) {
$price = price(2, $tarrifs, $distance, $distance_m);
}
echo "Number of passengers: $passengers\nVehicle used: $chosen\nDistance traveled: $distance miles / $distance_m metres\nPrice: £$price\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment