diff --git a/src/OldTimeGuitarGuy/MechanicalTurk/Contracts/Http/Response.php b/src/OldTimeGuitarGuy/MechanicalTurk/Contracts/Http/Response.php
index 1390916..4641d68 100644
--- a/src/OldTimeGuitarGuy/MechanicalTurk/Contracts/Http/Response.php
+++ b/src/OldTimeGuitarGuy/MechanicalTurk/Contracts/Http/Response.php
@@ -18,6 +18,21 @@ public function status();
*/
public function isValid();
+ /**
+ * Get the simple xml representation of the content
+ *
+ * @return \SimpleXmlElement
+ */
+ public function xml();
+
+ /**
+ * Get the json representation of the content
+ *
+ * @param boolean $isArray
+ * @return \stdClass
+ */
+ public function json($isArray = false);
+
/**
* Directly reference the content object
*
diff --git a/src/OldTimeGuitarGuy/MechanicalTurk/Http/Response.php b/src/OldTimeGuitarGuy/MechanicalTurk/Http/Response.php
index d6684c4..143152f 100644
--- a/src/OldTimeGuitarGuy/MechanicalTurk/Http/Response.php
+++ b/src/OldTimeGuitarGuy/MechanicalTurk/Http/Response.php
@@ -13,21 +13,35 @@ class Response implements ResponseContract
*
* @var integer
*/
- protected $status;
+ private $status;
/**
* The response content
*
- * @var \stdClass
+ * @var string
*/
- protected $content;
+ private $content;
/**
* Determines whether or not the response is valid
*
* @var boolean
*/
- protected $isValid;
+ private $isValid;
+
+ /**
+ * The SimpleXMLElement representation of the content
+ *
+ * @var \SimpleXMLElement
+ */
+ private $xml;
+
+ /**
+ * The json representation of the content
+ *
+ * @var \stdClass
+ */
+ private $json;
/**
* Create a new instance of Mechanical Turk response
@@ -37,15 +51,8 @@ class Response implements ResponseContract
public function __construct(ResponseInterface $response)
{
$this->status = $response->getStatusCode();
-
- $contents = new SimpleXmlElement(
- $response->getBody()->getContents()
- ?: ''
- );
-
- $this->isValid = count($contents->xpath('//Request[IsValid="True"]')) > 0;
-
- $this->content = json_decode(json_encode($contents));
+ $this->content = $response->getBody()->getContents()
+ ?: '';
}
/**
@@ -65,19 +72,52 @@ public function status()
*/
public function isValid()
{
+ if (! isset($this->isValid)) {
+ $this->isValid = count($this->xml()->xpath('//Request[IsValid="True"]')) > 0;
+ }
+
return $this->isValid;
}
+ /**
+ * Get the simple xml representation of the content
+ *
+ * @return \SimpleXmlElement
+ */
+ public function xml()
+ {
+ if (! isset($this->xml)) {
+ $this->xml = new SimpleXmlElement($this->content);
+ }
+
+ return $this->xml;
+ }
+
+ /**
+ * Get the json representation of the content
+ *
+ * @param boolean $isArray
+ * @return \stdClass
+ */
+ public function json($isArray = false)
+ {
+ if (! isset($this->json)) {
+ $this->json = json_decode(json_encode($this->content), $isArray);
+ }
+
+ return $this->json;
+ }
+
/**
* Directly reference the content object
*
* @param string $value
*
- * @return mixed
+ * @return \SimpleXMLElement
*/
public function __get($value)
{
- return $this->content->{$value};
+ return $this->json()->{$value};
}
/**