Skip to content

Commit

Permalink
updated turk response with some more options
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl Hepler committed Feb 22, 2017
1 parent 5525665 commit 85d69e6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 15 deletions.
15 changes: 15 additions & 0 deletions src/OldTimeGuitarGuy/MechanicalTurk/Contracts/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
70 changes: 55 additions & 15 deletions src/OldTimeGuitarGuy/MechanicalTurk/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,15 +51,8 @@ class Response implements ResponseContract
public function __construct(ResponseInterface $response)
{
$this->status = $response->getStatusCode();

$contents = new SimpleXmlElement(
$response->getBody()->getContents()
?: '<empty status="'.$this->status.'"></empty>'
);

$this->isValid = count($contents->xpath('//Request[IsValid="True"]')) > 0;

$this->content = json_decode(json_encode($contents));
$this->content = $response->getBody()->getContents()
?: '<empty status="'.$this->status().'"></empty>';
}

/**
Expand All @@ -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};
}

/**
Expand Down

0 comments on commit 85d69e6

Please sign in to comment.