mirror of
https://github.com/wiosna-dev/limesurvey-api-client.git
synced 2026-03-12 02:11:45 +01:00
Base class for one item - add class constructor & allow to set values directly in the constructor
This commit is contained in:
@@ -16,13 +16,32 @@ namespace Meritoo\LimeSurvey\ApiClient\Base\Result;
|
||||
*/
|
||||
abstract class BaseItem
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param array $data (optional) Data to set in properties of the item
|
||||
*/
|
||||
public function __construct(array $data = [])
|
||||
{
|
||||
$this->setValues($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets given value in given property
|
||||
*
|
||||
* @param string $property Name of property to set the value
|
||||
* @param mixed $value Value to set in property
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function setValue($property, $value);
|
||||
|
||||
/**
|
||||
* Sets values in each property of the item
|
||||
*
|
||||
* @param array $data Data to set in properties of the item
|
||||
* @return $this
|
||||
*/
|
||||
public function setValues($data)
|
||||
private function setValues(array $data)
|
||||
{
|
||||
/*
|
||||
* Oops, no data
|
||||
@@ -40,13 +59,4 @@ abstract class BaseItem
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets given value in given property
|
||||
*
|
||||
* @param string $property Name of property to set the value
|
||||
* @param mixed $value Value to set in property
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function setValue($property, $value);
|
||||
}
|
||||
|
||||
35
src/Exception/IncorrectClassOfResultItemException.php
Normal file
35
src/Exception/IncorrectClassOfResultItemException.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?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;
|
||||
|
||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||
|
||||
/**
|
||||
* An exception used while class used to create instance of one item of the result, with data fetched from the
|
||||
* LimeSurvey's API, is incorrect
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class IncorrectClassOfResultItemException extends \Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param string $className Incorrect class name used to create instance of one item
|
||||
*/
|
||||
public function __construct($className)
|
||||
{
|
||||
$template = 'Class %s used to create instance of one item of the result should extend %s, but it does not. Did'
|
||||
. ' you forget to use proper base class?';
|
||||
|
||||
$message = sprintf($template, $className, BaseItem::class);
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,11 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,7 +12,8 @@ use Exception;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Processor\ResultProcessor;
|
||||
|
||||
/**
|
||||
* An exception used while instance of one item used by result, with data fetched from the LimeSurvey's API, is unknown
|
||||
* An exception used while class name used to create instance of one item of the result, with data fetched from the
|
||||
* LimeSurvey's API, is unknown
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
@@ -27,10 +28,10 @@ class UnknownInstanceOfResultItem extends Exception
|
||||
*/
|
||||
public function __construct($method)
|
||||
{
|
||||
$template = 'Instance of one item used by result the of \'%s\' LimeSurvey API\'s method is unknown. Proper'
|
||||
. ' class is not mapped in %s::%s() method. Did you forget about this?';
|
||||
$template = 'Class name used to create instance of one item used by result the of \'%s\' LimeSurvey API\'s'
|
||||
. ' method is unknown. Proper class is not mapped in %s::%s() method. Did you forget about this?';
|
||||
|
||||
$message = sprintf($template, $method, ResultProcessor::class, 'getItemInstance');
|
||||
$message = sprintf($template, $method, ResultProcessor::class, 'getItemClassName');
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,9 @@
|
||||
|
||||
namespace Meritoo\LimeSurvey\ApiClient\Result\Processor;
|
||||
|
||||
use Meritoo\Common\Utilities\Reflection;
|
||||
use Meritoo\LimeSurvey\ApiClient\Base\Result\BaseItem;
|
||||
use Meritoo\LimeSurvey\ApiClient\Exception\IncorrectClassOfResultItemException;
|
||||
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownInstanceOfResultItem;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\ParticipantShort;
|
||||
@@ -31,7 +33,9 @@ class ResultProcessor
|
||||
* @param string $method Name of called method while talking to the LimeSurvey's API. One of the MethodType
|
||||
* class constants.
|
||||
* @param array $rawData Data returned by the LimeSurvey's API
|
||||
* @return null|BaseItem|array
|
||||
* @return array|BaseItem|null
|
||||
*
|
||||
* @throws IncorrectClassOfResultItemException
|
||||
*/
|
||||
public function process($method, array $rawData)
|
||||
{
|
||||
@@ -46,9 +50,9 @@ class ResultProcessor
|
||||
}
|
||||
|
||||
/*
|
||||
* Prepare instance of one item
|
||||
* Prepare class name for instance of one item
|
||||
*/
|
||||
$item = $this->getItemInstance($method);
|
||||
$itemClassName = $this->getItemClassName($method);
|
||||
|
||||
/*
|
||||
* The raw data is or, actually, should be iterable?
|
||||
@@ -57,49 +61,50 @@ class ResultProcessor
|
||||
$items = [];
|
||||
|
||||
foreach ($rawData as $itemData) {
|
||||
$emptyItem = clone $item;
|
||||
$items[] = $emptyItem->setValues($itemData);
|
||||
$items[] = new $itemClassName($itemData);
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
return $item->setValues($rawData);
|
||||
return new $itemClassName($rawData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns instance of one item of the result
|
||||
* Returns class name used to create instance of one item of the result
|
||||
*
|
||||
* @param string $method Name of called method while talking to the LimeSurvey's API. One of the MethodType
|
||||
* class constants.
|
||||
* @return BaseItem
|
||||
* @return string
|
||||
*
|
||||
* @throws IncorrectClassOfResultItemException
|
||||
* @throws UnknownInstanceOfResultItem
|
||||
*/
|
||||
private function getItemInstance($method)
|
||||
private function getItemClassName($method)
|
||||
{
|
||||
$item = null;
|
||||
$className = null;
|
||||
$method = MethodType::getValidatedMethod($method);
|
||||
|
||||
switch ($method) {
|
||||
case MethodType::ADD_PARTICIPANTS:
|
||||
case MethodType::GET_PARTICIPANT_PROPERTIES:
|
||||
$item = new Participant();
|
||||
$className = Participant::class;
|
||||
break;
|
||||
|
||||
case MethodType::GET_QUESTION_PROPERTIES:
|
||||
$item = new Question();
|
||||
$className = Question::class;
|
||||
break;
|
||||
|
||||
case MethodType::LIST_PARTICIPANTS:
|
||||
$item = new ParticipantShort();
|
||||
$className = ParticipantShort::class;
|
||||
break;
|
||||
|
||||
case MethodType::LIST_QUESTIONS:
|
||||
$item = new QuestionShort();
|
||||
$className = QuestionShort::class;
|
||||
break;
|
||||
|
||||
case MethodType::LIST_SURVEYS:
|
||||
$item = new Survey();
|
||||
$className = Survey::class;
|
||||
break;
|
||||
|
||||
/*
|
||||
@@ -108,12 +113,19 @@ class ResultProcessor
|
||||
}
|
||||
|
||||
/*
|
||||
* Instance of the item is unknown?
|
||||
* Oops, class name for instance of the item is unknown
|
||||
*/
|
||||
if (null === $item) {
|
||||
if (null === $className) {
|
||||
throw new UnknownInstanceOfResultItem($method);
|
||||
}
|
||||
|
||||
return $item;
|
||||
if (Reflection::isChildOfClass($className, BaseItem::class)) {
|
||||
return $className;
|
||||
}
|
||||
|
||||
/*
|
||||
* Oops, class is incorrect (should extend BaseItem)
|
||||
*/
|
||||
throw new IncorrectClassOfResultItemException($className);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user