<?php
class TransportAPI
{
    private $appId;

    private $appKey;

    private $trace;

    function __construct($appId, $appKey, $trace = FALSE)
    {
        $this->appId = $appId;

        $this->appKey = $appKey;

        $this->trace = $trace;
    }

    private function call($tapiUrl)
    {
        try {
            $response = file_get_contents($tapiUrl);
            if ($response !== false) {
                $response = json_decode($response, true);
            }
        } catch (Exception $ex) {
            if ($this->trace) {
                $traceOutput["Message"] = $ex->getMessage();

                $traceOutput["url"] = str_replace($this->appId, "", str_replace($this->appKey, "", $tapiUrl));

                echo "<xmp>";
                print_r($traceOutput);
                echo "</xmp>";
            }
        }

        return (isset($response) ? $response : false);
    }

    public function BusLiveDepartures(string $loc, bool $useNextbuses = false)
    {
        return $this->call("https://transportapi.com/v3/uk/bus/stop/{$loc}/live.json?app_id={$this->appId}&app_key={$this->appKey}&group=no&nextbuses=" . ($useNextbuses ? 'yes' : 'no'));
    }
    public function BusTimetabledDepartures(string $loc, DateTime $at)
    {
        return $this->call("https://transportapi.com/v3/uk/bus/stop/{$loc}/" . $at->format('Y-m-d/H:i') . "/timetable.json?app_id={$this->appId}&app_key={$this->appKey}&group=no");
    }
}
