Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small fixes, modifications and additions #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
307 changes: 162 additions & 145 deletions NS.php

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions NSException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@
*/
class NSException extends Exception
{
}
?>
}
47 changes: 27 additions & 20 deletions Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,28 @@
* You should have received a copy of the GNU General Public License along with
* phpNS. If not, see <http://www.gnu.org/licenses/>.
*/
abstract class Utils
{
public static function ISO8601Date2UnixTimestamp($iso8601)
{
abstract class Utils {
public static function ISO8601Date2UnixTimestamp($iso8601) {
return strtotime($iso8601);
}

public static function UnixTimestamp2ISO8601Date($unixTimestamp)
{
public static function UnixTimestamp2ISO8601Date($unixTimestamp) {
return date('c', $unixTimestamp);
}

/**
* This function will convert an ISO8601 period to the number of seconds.
* The number of seconds a period represents cannot be accurately calculated if you don't know the start of the period, so you need to give this.
* @param string $iso8601period The ISO8601 period to be converted
* @param long $fromDateTime The start of the period, given as a Unix timestamp
*/
public static function ISO8601Period2UnixTimestamp($iso8601period, $fromDateTime)
{
if (class_exists('DateInterval'))
{
* @param float $fromDateTime The start of the period, given as a Unix timestamp
* @return int
*/
public static function ISO8601Period2UnixTimestamp($iso8601period, $fromDateTime) {
if (class_exists('DateInterval')) {
$dateInterval = new DateInterval($iso8601period);
$toDateTime = strtotime("+".$dateInterval->format("%y")." year +".$dateInterval->format("%m")." month +".$dateInterval->format("%d")." day +".$dateInterval->format("%h")." hour +".$dateInterval->format("%i")." minute +".$dateInterval->format("%s")." second", $fromDateTime);
}
else
{
else {
// Remove the ambiguity of month and minute: make month = x
$arr = explode('T', $iso8601period);
$arr[0] = str_replace('M', 'X', $arr[0]);
Expand All @@ -63,14 +58,26 @@ public static function ISO8601Period2UnixTimestamp($iso8601period, $fromDateTime
return $toDateTime - $fromDateTime;
}

public static function boolean2String($boolean)
{
public static function boolean2String($boolean) {
return ($boolean ? "true" : "false");
}

public static function string2Boolean($string)
{
public static function string2Boolean($string) {
return strtolower($string) === "true";
}
}
?>

public static function distanceBetweenPoints($latitude1, $longitude1, $latitude2, $longitude2, $unit = 'Km') {
$theta = $longitude1 - $longitude2;
$distance = (sin(deg2rad($latitude1)) *
sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) *
cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
$distance = acos($distance);
$distance = rad2deg($distance);
$distance = $distance * 60 * 1.1515;
switch($unit) {
case 'Km': break;
case 'Mi' : $distance = $distance * 1.609344;
}
return (round($distance,2));
}
}
53 changes: 26 additions & 27 deletions cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License along with
* phpNS. If not, see <http://www.gnu.org/licenses/>.
*/
require_once(dirname(__file__).'/../retriever/Retriever.php');
require_once(dirname(__FILE__).'/../retriever/Retriever.php');

/**
* A cache is an object that keeps track of requests made, and responses retrieved.
Expand All @@ -29,10 +29,10 @@ abstract class Cache

// Seconds to cache a previous result
private $timeToCacheStations = 86400; // 60 * 60 * 24
private $timeToCachePrijzen = 86400; // 60 * 60 * 24
private $timeToCacheActuelevertrektijden = 30;
private $timeToCacheTreinplanner = 60;
private $timeToCacheStoringen = 120; // 60 * 2
private $timeToCachePrices = 86400; // 60 * 60 * 24
private $timeToCacheActualDepartureTimes = 30;
private $timeToCacheTrainscheduler = 60;
private $timeToCacheOutages = 120; // 60 * 2

public function __construct($retriever)
{
Expand All @@ -53,50 +53,49 @@ public function setTimeToCacheStations($timeToCacheStations)
{
$this->timeToCacheStations = $timeToCacheStations;
}
public function getTimeToCachePrijzen()
public function getTimeToCachePrices()
{
return $this->timeToCachePrijzen;
return $this->timeToCachePrices;
}

public function setTimeToCachePrijzen($timeToCachePrijzen)
public function setTimeToCachePrices($timeToCachePrices)
{
$this->timeToCachePrijzen = $timeToCachePrijzen;
$this->timeToCachePrices = $timeToCachePrices;
}

public function getTimeToCacheActuelevertrektijden()
public function getTimeToCacheActualDepartureTimes()
{
return $this->timeToCacheActuelevertrektijden;
return $this->timeToCacheActualDepartureTimes;
}

public function setTimeToCacheActuelevertrektijden($timeToCacheActuelevertrektijden)
public function setTimeToCacheActualDepartureTimes($timeToCacheActualDepartureTimes)
{
$this->timeToCacheActuelevertrektijden = $timeToCacheActuelevertrektijden;
$this->timeToCacheActualDepartureTimes = $timeToCacheActualDepartureTimes;
}

public function getTimeToCacheTreinplanner()
public function getTimeToCacheTrainscheduler()
{
return $this->timeToCacheTreinplanner;
return $this->timeToCacheTrainscheduler;
}

public function setTimeToCacheTreinplanner($timeToCacheTreinplanner)
public function setTimeToCacheTrainscheduler($timeToCacheTrainscheduler)
{
$this->timeToCacheTreinplanner = $timeToCacheTreinplanner;
$this->timeToCacheTrainscheduler = $timeToCacheTrainscheduler;
}

public function getTimeToCacheStoringen()
public function getTimeToCacheOutages()
{
return $this->timeToCacheStoringen;
return $this->timeToCacheOutages;
}

public function setTimeToCacheStoringen($timeToCacheStoringen)
public function setTimeToCacheOutages($timeToCacheOutages)
{
$this->timeToCacheStoringen = $timeToCacheStoringen;
$this->timeToCacheOutages = $timeToCacheOutages;
}

public abstract function getStations();
public abstract function getPrijzen($fromStation, $toStation, $viaStation = null, $dateTime = null);
public abstract function getActueleVertrektijden($station);
public abstract function getTreinplanner($fromStation, $toStation, $viaStation = null, $previousAdvices = null, $nextAdvices = null, $dateTime = null, $departure = null, $hslAllowed = null, $yearCard = null);
public abstract function getStoringen($station, $actual = null, $unplanned = null);
}
?>
public abstract function getRates($fromStation, $toStation, $viaStation = null, $dateTime = null);
public abstract function getActualDepartureTimes($station);
public abstract function getTrainscheduler($fromStation, $toStation, $viaStation = null, $previousAdvices = null, $nextAdvices = null, $dateTime = null, $departure = null, $hslAllowed = null, $yearCard = null);
public abstract function getOutages($station, $actual = null, $unplanned = null);
}
31 changes: 15 additions & 16 deletions cache/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,61 +44,61 @@ public function getStations()
}
}

public function getPrijzen($fromStation, $toStation, $viaStation = null, $dateTime = null)
public function getRates($fromStation, $toStation, $viaStation = null, $dateTime = null)
{
$tmpFile = $this->initTmpDir("prijzen", $fromStation, $toStation, $viaStation, $dateTime)."result.xml";
if (file_exists($tmpFile) && filemtime($tmpFile) + $this->getTimeToCachePrijzen() > time())
$tmpFile = $this->initTmpDir("rates", $fromStation, $toStation, $viaStation, $dateTime)."result.xml";
if (file_exists($tmpFile) && filemtime($tmpFile) + $this->getTimeToCachePrices() > time())
{
return file_get_contents($tmpFile);
}
else
{
$xml = $this->getRetriever()->getPrijzen($fromStation, $toStation, $viaStation, $dateTime);
$xml = $this->getRetriever()->getRates($fromStation, $toStation, $viaStation, $dateTime);
file_put_contents($tmpFile, $xml);
return $xml;
}
}

public function getActueleVertrektijden($station)
public function getActualDepartureTimes($station)
{
$tmpFile = $this->initTmpDir("avt", $station)."result.xml";
if (file_exists($tmpFile) && filemtime($tmpFile) + $this->getTimeToCacheActuelevertrektijden() > time())
if (file_exists($tmpFile) && filemtime($tmpFile) + $this->getTimeToCacheActualDepartureTimes() > time())
{
return file_get_contents($tmpFile);
}
else
{
$xml = $this->getRetriever()->getActueleVertrektijden($station);
$xml = $this->getRetriever()->getActualDepartureTimes($station);
file_put_contents($tmpFile, $xml);
return $xml;
}
}

public function getTreinplanner($fromStation, $toStation, $viaStation = null, $previousAdvices = null, $nextAdvices = null, $dateTime = null, $departure = null, $hslAllowed = null, $yearCard = null)
public function getTrainscheduler($fromStation, $toStation, $viaStation = null, $previousAdvices = null, $nextAdvices = null, $dateTime = null, $departure = null, $hslAllowed = null, $yearCard = null)
{
$tmpFile = $this->initTmpDir("treinplanner", $fromStation, $toStation, $viaStation, $previousAdvices, $nextAdvices, $dateTime, $departure, $hslAllowed, $yearCard)."result.xml";
if (file_exists($tmpFile) && filemtime($tmpFile) + $this->getTimeToCacheTreinplanner() > time())
$tmpFile = $this->initTmpDir("trainscheduler", $fromStation, $toStation, $viaStation, $previousAdvices, $nextAdvices, $dateTime, $departure, $hslAllowed, $yearCard)."result.xml";
if (file_exists($tmpFile) && filemtime($tmpFile) + $this->getTimeToCacheTrainscheduler() > time())
{
return file_get_contents($tmpFile);
}
else
{
$xml = $this->getRetriever()->getTreinplanner($fromStation, $toStation, $viaStation, $previousAdvices, $nextAdvices, $dateTime, $departure, $hslAllowed, $yearCard);
$xml = $this->getRetriever()->getTrainscheduler($fromStation, $toStation, $viaStation, $previousAdvices, $nextAdvices, $dateTime, $departure, $hslAllowed, $yearCard);
file_put_contents($tmpFile, $xml);
return $xml;
}
}

public function getStoringen($station, $actual = null, $unplanned = null)
public function getOutages($station, $actual = null, $unplanned = null)
{
$tmpFile = $this->initTmpDir("storingen", $station, $actual, $unplanned)."result.xml";
if (file_exists($tmpFile) && filemtime($tmpFile) + $this->getTimeToCacheStoringen() > time())
$tmpFile = $this->initTmpDir("outages", $station, $actual, $unplanned)."result.xml";
if (file_exists($tmpFile) && filemtime($tmpFile) + $this->getTimeToCacheOutages() > time())
{
return file_get_contents($tmpFile);
}
else
{
$xml = $this->getRetriever()->getStoringen($station, $actual, $unplanned);
$xml = $this->getRetriever()->getOutages($station, $actual, $unplanned);
file_put_contents($tmpFile, $xml);
return $xml;
}
Expand Down Expand Up @@ -143,4 +143,3 @@ private function initTmpDir($functionName)
return $strTmpDir;
}
}
?>
43 changes: 21 additions & 22 deletions cache/MySQLCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class MySQLCache extends Cache

// ID's that are used in the cache table
const ID_STATIONS = 0;
const ID_PRIJZEN = 1;
const ID_ACTUELEVERTREKTIJDEN = 2;
const ID_TREINPLANNER = 3;
const ID_STORINGEN = 4;
const ID_PRICES = 1;
const ID_ACTUALDEPARTURETIMES = 2;
const ID_TRAINSCHEDULER = 3;
const ID_OUTAGES = 4;

public function __construct($retriever, $server, $username, $password, $database, $table)
{
Expand Down Expand Up @@ -143,48 +143,47 @@ public function getStations()
return $xml;
}

public function getPrijzen($fromStation, $toStation, $viaStation = null, $dateTime = null)
public function getRates($fromStation, $toStation, $viaStation = null, $dateTime = null)
{
$xml = $this->tryFromCache(self::ID_PRIJZEN, $this->getTimeToCachePrijzen(), $fromStation, $toStation, $viaStation, $dateTime);
$xml = $this->tryFromCache(self::ID_PRICES, $this->getTimeToCacheRates(), $fromStation, $toStation, $viaStation, $dateTime);
if ($xml === NULL)
{
$xml = $this->getRetriever()->getPrijzen($fromStation, $toStation, $viaStation, $dateTime);
$this->putInCache(self::ID_PRIJZEN, $xml, $fromStation, $toStation, $viaStation, $dateTime);
$xml = $this->getRetriever()->getRates($fromStation, $toStation, $viaStation, $dateTime);
$this->putInCache(self::ID_PRICES, $xml, $fromStation, $toStation, $viaStation, $dateTime);
}
return $xml;
}

public function getActueleVertrektijden($station)
public function getActualDepartureTimes($station)
{
$xml = $this->tryFromCache(self::ID_ACTUELEVERTREKTIJDEN, $this->getTimeToCacheActuelevertrektijden(), $station);
$xml = $this->tryFromCache(self::ID_ACTUALDEPARTURETIMES, $this->getTimeToCacheActualDepartureTimes(), $station);
if ($xml === NULL)
{
$xml = $this->getRetriever()->getActueleVertrektijden($station);
$this->putInCache(self::ID_ACTUELEVERTREKTIJDEN, $xml, $station);
$xml = $this->getRetriever()->getActualDepartureTimes($station);
$this->putInCache(self::ID_ACTUALDEPARTURETIMES, $xml, $station);
}
return $xml;
}

public function getTreinplanner($fromStation, $toStation, $viaStation = null, $previousAdvices = null, $nextAdvices = null, $dateTime = null, $departure = null, $hslAllowed = null, $yearCard = null)
public function getTrainscheduler($fromStation, $toStation, $viaStation = null, $previousAdvices = null, $nextAdvices = null, $dateTime = null, $departure = null, $hslAllowed = null, $yearCard = null)
{
$xml = $this->tryFromCache(self::ID_TREINPLANNER, $this->getTimeToCacheTreinplanner(), $fromStation, $toStation, $viaStation, $previousAdvices, $nextAdvices, $dateTime, $departure, $hslAllowed, $yearCard);
$xml = $this->tryFromCache(self::ID_TRAINSCHEDULER, $this->getTimeToCacheTrainscheduler(), $fromStation, $toStation, $viaStation, $previousAdvices, $nextAdvices, $dateTime, $departure, $hslAllowed, $yearCard);
if ($xml === NULL)
{
$xml = $this->getRetriever()->getTreinplanner($fromStation, $toStation, $viaStation, $previousAdvices, $nextAdvices, $dateTime, $departure, $hslAllowed, $yearCard);
$this->putInCache(self::ID_TREINPLANNER, $xml, $fromStation, $toStation, $viaStation, $previousAdvices, $nextAdvices, $dateTime, $departure, $hslAllowed, $yearCard);
$xml = $this->getRetriever()->getTrainscheduler($fromStation, $toStation, $viaStation, $previousAdvices, $nextAdvices, $dateTime, $departure, $hslAllowed, $yearCard);
$this->putInCache(self::ID_TRAINSCHEDULER, $xml, $fromStation, $toStation, $viaStation, $previousAdvices, $nextAdvices, $dateTime, $departure, $hslAllowed, $yearCard);
}
return $xml;
}

public function getStoringen($station, $actual = null, $unplanned = null)
public function getOutages($station, $actual = null, $unplanned = null)
{
$xml = $this->tryFromCache(self::ID_STORINGEN, $this->getTimeToCacheStoringen(), $station, $actual, $unplanned);
$xml = $this->tryFromCache(self::ID_OUTAGES, $this->getTimeToCacheOutages(), $station, $actual, $unplanned);
if ($xml === NULL)
{
$xml = $this->getRetriever()->getStoringen($station, $actual, $unplanned);
$this->putInCache(self::ID_STORINGEN, $xml, $station, $actual, $unplanned);
$xml = $this->getRetriever()->getOutages($station, $actual, $unplanned);
$this->putInCache(self::ID_OUTAGES, $xml, $station, $actual, $unplanned);
}
return $xml;
}
}
?>
}
19 changes: 9 additions & 10 deletions cache/NoCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,23 @@ public function getStations()
return $this->getRetriever()->getStations();
}

public function getPrijzen($fromStation, $toStation, $viaStation = null, $dateTime = null)
public function getRates($fromStation, $toStation, $viaStation = null, $dateTime = null)
{
return $this->getRetriever()->getPrijzen($fromStation, $toStation, $viaStation, $dateTime);
return $this->getRetriever()->getRates($fromStation, $toStation, $viaStation, $dateTime);
}

public function getActueleVertrektijden($station)
public function getActualDepartureTimes($station)
{
return $this->getRetriever()->getActueleVertrektijden($station);
return $this->getRetriever()->getActualDepartureTimes($station);
}

public function getTreinplanner($fromStation, $toStation, $viaStation = null, $previousAdvices = null, $nextAdvices = null, $dateTime = null, $departure = null, $hslAllowed = null, $yearCard = null)
public function getTrainscheduler($fromStation, $toStation, $viaStation = null, $previousAdvices = null, $nextAdvices = null, $dateTime = null, $departure = null, $hslAllowed = null, $yearCard = null)
{
return $this->getRetriever()->getTreinplanner($fromStation, $toStation, $viaStation, $previousAdvices, $nextAdvices, $dateTime, $departure, $hslAllowed, $yearCard);
return $this->getRetriever()->getTrainscheduler($fromStation, $toStation, $viaStation, $previousAdvices, $nextAdvices, $dateTime, $departure, $hslAllowed, $yearCard);
}

public function getStoringen($station, $actual = null, $unplanned = null)
public function getOutages($station, $actual = null, $unplanned = null)
{
return $this->getRetriever()->getStoringen($station, $actual, $unplanned);
return $this->getRetriever()->getOutages($station, $actual, $unplanned);
}
}
?>
}
Loading