Skip to content

Commit

Permalink
Fix undefined property warning
Browse files Browse the repository at this point in the history
  • Loading branch information
benmajor committed May 8, 2024
1 parent e545948 commit acdaaea
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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()
{
Expand All @@ -94,4 +94,4 @@ public function toJSON()
'rates' => $this->getRates()
]);
}
}
}

0 comments on commit acdaaea

Please sign in to comment.