Source of file EBSResponse.php
Size: 3,127 Bytes - Last Modified: 2021-12-23T10:09:35+00:00
/var/www/docs.ssmods.com/process/src/src/EBSResponse.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 | <?php namespace OP; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\ArrayList; use SilverStripe\Dev\Debug; /** * Class used to respond with JSON requests. Supports debugging. */ class EBSResponse { private $code; private $content; private $error; private $raw; private $url; public function __construct($content, $code, $url) { $this->code = $code; $this->content = null; $this->raw = $content; $this->url = $url; if ($code == 200 && is_string($content)) { $this->content = json_decode($content); } elseif (is_string($content)) { $this->error = json_decode($content); } } /** * JSON array of the result of the response * @return json array */ public function Content() { return $this->content; } /** * 400 error. 500 server error. 200 OK etc. * @return int */ public function Code() { return $this->code; } /** * Raw content * @return string */ public function Raw() { return $this->raw; } /** * Recursivity creates the SilverStripe dataobject representation of content * @param mixed $array * @return \DataObject|\DataList|null */ private function parseobject($array) { if (is_object($array)) { if (get_class($array) == 'DataObject') { return $array; } $do = DataObject::create(); foreach (get_object_vars($array) as $key => $obj) { if ($key == '__Type') { $do->setField('Title', $obj); } elseif (is_array($obj) || is_object($obj)) { $do->setField($key, $this->parseobject($obj)); } else { $do->setField($key, $obj); } } return $do; } elseif (is_array($array)) { $dataList = ArrayList::create(); foreach ($array as $key => $obj) { $dataList->push($this->parseobject($obj)); } return $dataList; } return null; } /** * Returns SilverStripe object representations of content * @return \DataObject|\DataList|null */ public function Data() { return $this->parseobject($this->content); } /** * Returns SilverStripe object representations of content * @return \DataObject|\DataList|null */ public function Errors() { return $this->parseobject($this->error); } /** * Makes debugging the webservice more human readable * @return HTML */ public function debug() { return '<dl> <dt><strong>URL</strong></dt> <dd>' . $this->url . '</dd> <dt><strong>Code</strong></dt> <dd>' . $this->code . '</dd> <dt><strong>Response</strong></dt> <dd><pre>' . $this->raw . '</pre></dd> <dt><strong>Errors</strong></dt> <dd>' . Debug::text(json_decode(json_encode($this->error), true)) . '</dd> </dl>'; } } |