diff --git a/src/Response.php b/src/Response.php index 7b81c3c..766cddc 100644 --- a/src/Response.php +++ b/src/Response.php @@ -6,52 +6,52 @@ class Response { # The actual Guzzle response: private $response; - + # Core response: private $headers; private $bodyRaw; private $body; - + # Properties: private $statusCode; private $timestamp; private $baseCurrency; - + private $rates = [ ]; - + function __construct( \GuzzleHttp\Psr7\Response $response = null ) { $this->response = $response; - + $this->headers = $response->getHeaders(); $this->bodyRaw = (string) $response->getBody(); $this->body = json_decode( $this->bodyRaw ); - + # Set our properties: $this->statusCode = $response->getStatusCode(); - $this->timestamp = $this->body->timestamp; + $this->timestamp = $this->body->timestamp ?? null; $this->baseCurrency = $this->body->source; $this->rates = $this->body->quotes; } - + /****************************/ /* */ /* GETTERS */ /* */ /****************************/ - + # Get the status code: public function getStatusCode() { return (int) $this->statusCode; } - + # Get the timestamp of the request: public function getTimestamp() { return $this->timestamp; } - + # Get the base currency: public function getBaseCurrency() { @@ -64,26 +64,26 @@ public function getRates() # Convert the rates to a key / value array: return json_decode( json_encode($this->rates), true ); } - + # Return a specific rate: public function getRate( $code = null ) { $rates = $this->getRates(); - + # If there's only one rate, and the code is null, return the first one: if( count($rates) == 1 && $code == null ) { return reset( $rates ); } - + if( $this->body->rates->{$code} ) { return $this->body->rates->{$code}; } - + return null; } - + # Convert the response to JSON: public function toJSON() { @@ -94,4 +94,4 @@ public function toJSON() 'rates' => $this->getRates() ]); } -} \ No newline at end of file +}