mirror of
https://github.com/wiosna-dev/limesurvey-api-client.git
synced 2026-03-12 02:11:45 +01:00
Result - store status provided by the LimeSurvey's API (instead of raw data) & do not process the data (because it's unknown)
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\LimeSurvey\ApiClient\Exception;
|
||||
|
||||
/**
|
||||
* An exception used while raw data returned by the LimeSurvey's API cannot be processed
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class CannotProcessDataException extends \Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param string $reason Reason why data cannot be processed, e.g. "Invalid user name or password"
|
||||
*/
|
||||
public function __construct($reason)
|
||||
{
|
||||
$template = 'Raw data returned by the LimeSurvey\'s API cannot be processed. Reason: \'%s\'.';
|
||||
$message = sprintf($template, $reason);
|
||||
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ namespace Meritoo\LimeSurvey\ApiClient\Result;
|
||||
|
||||
use Meritoo\Common\Collection\Collection;
|
||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor;
|
||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||
|
||||
@@ -35,6 +36,13 @@ class Result
|
||||
*/
|
||||
private $rawData;
|
||||
|
||||
/**
|
||||
* Status, information returned instead of usual/normal result
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $status;
|
||||
|
||||
/**
|
||||
* Processor of the raw data fetched while talking to the LimeSurvey's API
|
||||
*
|
||||
@@ -52,7 +60,7 @@ class Result
|
||||
public function __construct($method, array $rawData)
|
||||
{
|
||||
$this->method = MethodType::getValidatedMethod($method);
|
||||
$this->rawData = $rawData;
|
||||
$this->setRawDataAndStatus($rawData);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,14 +79,41 @@ class Result
|
||||
* @param bool $raw (optional) If is set to true, raw data provided by the LimeSurvey's API will be returned.
|
||||
* Otherwise - prepared/processed.
|
||||
* @return array|Collection|BaseItem
|
||||
* @throws CannotProcessDataException
|
||||
*/
|
||||
public function getData($raw = false)
|
||||
{
|
||||
/*
|
||||
* Raw data should be returned only?
|
||||
* Let's do it
|
||||
*/
|
||||
if ($raw) {
|
||||
return $this->rawData;
|
||||
}
|
||||
|
||||
return $this->getProcessedData($this->rawData);
|
||||
/*
|
||||
* Status is unknown?
|
||||
* Let's process the raw data
|
||||
*/
|
||||
if (empty($this->status)) {
|
||||
return $this->getProcessedData($this->rawData);
|
||||
}
|
||||
|
||||
/*
|
||||
* Oops, the raw data returned by the LimeSurvey's API cannot be processed, because status was provided.
|
||||
* Well, probably something is broken and... there is no data.
|
||||
*/
|
||||
throw new CannotProcessDataException($this->status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns status, information returned instead of usual/normal result
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getStatus()
|
||||
{
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,4 +154,23 @@ class Result
|
||||
|
||||
return $this->resultProcessor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets status, information returned instead of usual/normal result and raw data returned by the LimeSurvey's API
|
||||
*
|
||||
* @param array $rawData Raw data returned by the LimeSurvey's API
|
||||
*/
|
||||
private function setRawDataAndStatus(array $rawData)
|
||||
{
|
||||
/*
|
||||
* Status was provided?
|
||||
* Well, probably something is broken and... there is no data
|
||||
*/
|
||||
if (isset($rawData['status'])) {
|
||||
$this->status = trim($rawData['status']);
|
||||
$rawData = [];
|
||||
}
|
||||
|
||||
$this->rawData = $rawData;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user