Skip to content

Instantly share code, notes, and snippets.

@nicolaskempf57
Created August 3, 2016 12:54
Show Gist options
  • Save nicolaskempf57/60faf8618689b59172cc34edfb9be60a to your computer and use it in GitHub Desktop.
Save nicolaskempf57/60faf8618689b59172cc34edfb9be60a to your computer and use it in GitHub Desktop.
<?php
public function paths() {
$paths_list = array();
$simple_path = $this->check_same_path($this->depart, $this->arrivee);
if ($simple_path) {
$paths_list = $this->simple_paths($simple_path);
}
if (!$simple_path || !$paths_list) {
$paths_list = $this->complex_paths();
}
$this->log_search();
return $paths_list;
}
private function check_same_path($depart, $arrivee)
{
$chemins_depart = $this->database->query("SELECT DISTINCT chemin_id FROM trips_details WHERE stop_name = '" . $depart . "'")->fetchAll(PDO::FETCH_COLUMN, "chemin_id");
$chemins_arrivee = $this->database->query("SELECT DISTINCT chemin_id FROM trips_details WHERE stop_name = '" . $arrivee . "'")->fetchAll(PDO::FETCH_COLUMN, "chemin_id");
return array_intersect($chemins_depart, $chemins_arrivee);
}
private function simple_paths($results) {
$index = $this->create_id_search();
$all = $this->cache->get($index);
$cached = true;
if(!$all) {
$routes = $this->database->query("SELECT route_id FROM trips_details WHERE chemin_id IN (" . implode(", ", $results) . ")")->fetchall(PDO::FETCH_COLUMN, "route_id");
$all = array();
$lignes = array();
foreach($routes as $route) {
if(!array_key_exists($route, $lignes)) {
$lignes[$route] = true;
$all[] = array(
["route_id" => $route,
"stop_name" => $this->depart],
["route_id" => null,
"stop_name" => $this->arrivee]);
}
}
$cached = false;
$this->cache->set($index, $all, 0);
}
return array("code" => $index, "cached" => $cached, "possibles" => $all);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment