Implement Surveys class, collection of surveys, to work with surveys

This commit is contained in:
Meritoo
2017-09-29 22:51:42 +02:00
parent 6159731768
commit 5243294bd5
7 changed files with 193 additions and 22 deletions

View File

@@ -13,7 +13,7 @@ use Meritoo\Common\Exception\Method\DisabledMethodException;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
/**
* Participants of survey.
* Collection of participants (of surveys).
* All participants grouped per survey.
*
* It's a collection of participants' collections.

View File

@@ -0,0 +1,58 @@
<?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\Result\Collection;
use Meritoo\Common\Collection\Collection;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
/**
* Collection of surveys (the Survey class instances)
*
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
* @copyright Meritoo.pl
*/
class Surveys extends Collection
{
/**
* {@inheritdoc}
*/
public function add($element, $index = null)
{
if (null === $index) {
/* @var Survey $element */
$index = $element->getId();
}
return parent::add($element, $index);
}
/**
* Returns all or active only surveys
*
* @param bool $onlyActive (optional) If is set to true, active surveys are returned only. Otherwise - all.
* @return $this
*/
public function getAll($onlyActive = false)
{
if ($this->isEmpty() || !$onlyActive) {
return $this;
}
$all = new static();
/* @var Survey $survey */
foreach ($this as $survey) {
if ($survey->isActive()) {
$all->add($survey);
}
}
return $all;
}
}

View File

@@ -33,7 +33,7 @@ class ParticipantService
private $client;
/**
* Participants of survey.
* Collection of participants (of surveys).
* All participants grouped per survey.
*
* @var Participants
@@ -44,7 +44,8 @@ class ParticipantService
* Class constructor
*
* @param Client $client Client of the LimeSurvey's API
* @param Participants $allParticipants (optional) Participants of survey. All participants grouped per survey.
* @param Participants $allParticipants (optional) Collection of participants (of surveys). All participants
* grouped per survey.
*/
public function __construct(Client $client, Participants $allParticipants = null)
{

View File

@@ -11,6 +11,7 @@ namespace Meritoo\LimeSurvey\ApiClient\Service;
use Meritoo\Common\Collection\Collection;
use Meritoo\LimeSurvey\ApiClient\Client\Client;
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Surveys;
use Meritoo\LimeSurvey\ApiClient\Result\Item\Survey;
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
@@ -31,22 +32,23 @@ class SurveyService
private $client;
/**
* All surveys
* All surveys.
* Collection of surveys (the Survey class instances).
*
* @var Collection
* @var Surveys
*/
private $allSurveys;
/**
* Class constructor
*
* @param Client $client Client of the LimeSurvey's API
* @param Collection $allSurveys (optional) All surveys
* @param Client $client Client of the LimeSurvey's API
* @param Surveys $allSurveys (optional) All surveys. Collection of surveys (the Survey class instances).
*/
public function __construct(Client $client, Collection $allSurveys = null)
public function __construct(Client $client, Surveys $allSurveys = null)
{
if (null === $allSurveys) {
$allSurveys = new Collection();
$allSurveys = new Surveys();
}
$this->client = $client;
@@ -66,12 +68,16 @@ class SurveyService
/**
* Returns all surveys
*
* @return Collection
* @param bool $onlyActive (optional) If is set to true, active surveys are returned only. Otherwise - all.
* @return Surveys
*
* @throws CannotProcessDataException
*/
public function getAllSurveys()
public function getAllSurveys($onlyActive = false)
{
if ($this->allSurveys->isEmpty()) {
$surveys = new Surveys();
try {
$surveys = $this
->client
@@ -87,16 +93,14 @@ class SurveyService
if (ReasonType::NO_SURVEYS_FOUND !== $reason) {
throw $exception;
}
$surveys = new Collection();
}
if (null !== $surveys) {
$this->allSurveys = $surveys;
if (null !== $surveys && $surveys instanceof Collection) {
$this->allSurveys = new Surveys($surveys->toArray());
}
}
return $this->allSurveys;
return $this->allSurveys->getAll($onlyActive);
}
/**