Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into task/LS-1027_stop_…
Browse files Browse the repository at this point in the history
…survey_design
  • Loading branch information
Trischi80 committed Jun 8, 2023
2 parents 0b2ea6a + 200583c commit d68cc5d
Show file tree
Hide file tree
Showing 21 changed files with 171 additions and 123 deletions.
4 changes: 2 additions & 2 deletions application/controllers/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public function beforeAction($action)
/**
* Run REST controller action.
*
* @param string $actionId
* @param string $actionID
* @return void
*/
public function run($actionId = null)
public function run($actionID = null)
{
$endpointFactory = DI::getContainer()
->get(EndpointFactory::class);
Expand Down
8 changes: 4 additions & 4 deletions application/libraries/Api/Command/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Response
{
private ?Status $status = null;
private Status $status;

/**
* @var mixed
Expand All @@ -15,16 +15,16 @@ class Response

/**
* @param mixed $data
* @param ?Status $status
* @param Status $status
*/
public function __construct($data, Status $status = null)
public function __construct($data, Status $status)
{
$this->status = $status;
$this->data = $data;
}

/**
* @return ?Status
* @return Status
*/
public function getStatus()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,22 @@ public function __construct()
}

/**
* @param array $array
* @param mixed $array
* @return array
*/
public function transformAll($array)
{
$array = parent::transformAll($array);

usort($array, array($this, 'sort'));
usort(
$array,
function ($a, $b) {
return (int)(
(int)$a['sortOrder'] > (int)$b['sortOrder']
);
}
);

return $array;
}

/**
* @param int $a
* @param int $b
* @return bool
*/
private function sort($a, $b)
{
return ((int)$a['sortOrder']) > ((int)$b['sortOrder']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ public function transformAll($array)
{
$array = parent::transformAll($array);

usort($array, function ($a, $b) {
return ((int)$a['questionOrder']) > ((int)$b['questionOrder']);
});
usort(
$array,
function ($a, $b) {
return (int)(
(int)$a['questionOrder'] > (int)$b['questionOrder']
);
}
);

return $array;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ public function transformAll($array)
{
$array = parent::transformAll($array);

usort($array, function ($a, $b) {
return ((int)$a['groupOrder']) > ((int)$b['groupOrder']);
});
usort(
$array,
function ($a, $b) {
return (int)(
(int)$a['groupOrder'] > (int)$b['groupOrder']
);
}
);

return $array;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace LimeSurvey\Api\Command\V1\Transformer\Output;

use Survey;
use LimeSurvey\Api\Transformer\Output\TransformerOutputActiveRecord;
use LimeSurvey\Api\Transformer\Formatter\FormatterYnToBool;
use LimeSurvey\Api\Transformer\Formatter\FormatterDateTimeToJson;

class TransformerOutputSurvey extends TransformerOutputActiveRecord
{
private $transformerOutputSurveyLanguageSettings = null;
private TransformerOutputSurveyLanguageSettings $transformerOutputSurveyLanguageSettings;

/**
* Construct
Expand Down Expand Up @@ -78,14 +79,18 @@ public function __construct(
]);
}

public function transform($surveyModel)
public function transform($data)
{
$survey = parent::transform($surveyModel);
$survey = null;
if (!$data instanceof Survey) {
return null;
}
$survey = parent::transform($data);
$survey['defaultLanguage'] = $this->transformerOutputSurveyLanguageSettings->transform(
$surveyModel->defaultlanguage
$data->defaultlanguage
);
$survey['languageSettings'] = $this->transformerOutputSurveyLanguageSettings->transformAll(
$surveyModel->languagesettings
$data->languagesettings
);
return $survey;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,21 @@ public function __construct(
* Transform
*
* Returns an array of entity references indexed by the specified key.
*
* @param Survey $surveyModel
* @return array
*/
public function transform($surveyModel)
public function transform($data)
{
$survey = $this->transformerSurvey->transform($surveyModel);
if (!$data instanceof Survey) {
return null;
}

$survey = $this->transformerSurvey->transform($data);

$survey['languages'] = $surveyModel->allLanguages;
$survey['languages'] = $data->allLanguages;

// transformAll() can apply required entity sort so we must retain the sort order going forward
// - We use a lookup array later to access entities without needing to know their position in the collection
$survey['questionGroups'] = $this->transformerQuestionGroup->transformAll(
$surveyModel->groups
$data->groups
);

// An array of groups indexed by gid for easy look up
Expand All @@ -66,7 +67,7 @@ public function transform($surveyModel)
$survey['questionGroups']
);

foreach ($surveyModel->groups as $questionGroupModel) {
foreach ($data->groups as $questionGroupModel) {
// order of groups from the model relation may be different than from the transformed data
// - so we use the lookup to get a reference to the required entity without needing to
// - know its position in the output array
Expand Down Expand Up @@ -101,7 +102,7 @@ public function transform($surveyModel)
*
* @param array $questionLookup
* @param array $questions
* @return array
* @return void
*/
private function transformQuestions($questionLookup, $questions)
{
Expand Down
20 changes: 15 additions & 5 deletions application/libraries/Api/Rest/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace LimeSurvey\Api\Rest;

use LimeSurvey\Api\Command\Request\Request;
use LimeSurvey\Api\Command\{
CommandInterface,
Request\Request
};
use LimeSurvey\Api\Rest\Renderer\RendererInterface;
use Psr\Container\ContainerInterface;

/**
Expand All @@ -11,9 +15,11 @@
*/
class Endpoint
{
/** @var array */
protected $config = [];
/** @var array */
protected $commandParams = [];
protected ?ContainerInterface $diContainer = null;
protected ContainerInterface $diContainer;

/**
* Constructor
Expand All @@ -33,16 +39,20 @@ public function __construct($config, $commandParams, ContainerInterface $diConta
/**
* Get Command
*
* @return LimeSurvey\Api\Command\CommandInterface
* @return CommandInterface
*/
protected function getCommand()
{
return $this->diContainer->get($this->config['commandClass']);
}

/**
* Get Response Renderer
*
* @return RendererInterface
*/
protected function getResponseRenderer()
{

$apiVersion = ucfirst($this->config['apiVersion']);
$class = 'LimeSurvey\Api\Rest\\'
. $apiVersion
Expand All @@ -62,7 +72,7 @@ public function run()
$response = $this->getCommand()->run(
new Request($this->commandParams)
);
return $renderer->returnResponse($response);
$renderer->returnResponse($response);
} catch (\Exception $e) {
$renderer->returnException($e);
}
Expand Down

0 comments on commit d68cc5d

Please sign in to comment.