From 764855514a4c5f0321da615471ccfb0fd6dfd7a1 Mon Sep 17 00:00:00 2001 From: zaliqarosli Date: Fri, 16 Dec 2022 22:07:58 -0500 Subject: [PATCH] add switch for versions --- .../visit/instrument/instrument.class.inc | 19 +++-- .../visit/instrument_0_0_4_dev.class.inc | 72 +++++++++++++++++++ 2 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 modules/api/php/views/visit/instrument_0_0_4_dev.class.inc diff --git a/modules/api/php/endpoints/candidate/visit/instrument/instrument.class.inc b/modules/api/php/endpoints/candidate/visit/instrument/instrument.class.inc index ee2047b3239..4fa75f63ed5 100644 --- a/modules/api/php/endpoints/candidate/visit/instrument/instrument.class.inc +++ b/modules/api/php/endpoints/candidate/visit/instrument/instrument.class.inc @@ -138,10 +138,21 @@ class Instrument extends Endpoint implements \LORIS\Middleware\ETagCalculator */ private function _handleGET() : ResponseInterface { - $body = (new \LORIS\api\Views\Visit\Instrument( - $this->_visit, - $this->_instrument - ))->toArray(); + + $version = $request->getAttribute('LORIS-API-Version'); + switch ($version) { + case 'v0.0.3': + $body = (new \LORIS\api\Views\Visit\Instrument( + $this->_visit, + $this->_instrument + ))->toArray(); + break; + default: + $body = (new \LORIS\api\Views\Visit\Instrument_0_0_4_Dev( + $this->_visit, + $this->_instrument + ))->toArray(); + } return new \LORIS\Http\Response\JsonResponse( $body diff --git a/modules/api/php/views/visit/instrument_0_0_4_dev.class.inc b/modules/api/php/views/visit/instrument_0_0_4_dev.class.inc new file mode 100644 index 00000000000..1a732c331ac --- /dev/null +++ b/modules/api/php/views/visit/instrument_0_0_4_dev.class.inc @@ -0,0 +1,72 @@ + + * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 + * @link https://www.github.com/aces/Loris/ + */ + +namespace LORIS\api\Views\Visit; + +/** + * Creates a representation of a visit intrument following the api response + * specifications. + * + * @category ApiViews + * @package Loris + * @author Xavier Lecours Boucher + * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 + * @link https://www.github.com/aces/Loris/ + */ + +class Instrument_0_0_4_Dev +{ + private $_timepoint; + private $_instrument; + + /** + * Constructor which sets the instance variables based on the provided timepoint + * and instrument. + * + * @param \Timepoint $timepoint The timepoint to represent + * @param \NDB_BVL_Instrument $instrument The instrument. + */ + public function __construct( + \Timepoint $timepoint, + \NDB_BVL_Instrument $instrument + ) { + $this->_timepoint = $timepoint; + $this->_instrument = $instrument; + } + + /** + * Creates an serializable array of this object's data + * + * @return array + */ + public function toArray(): array + { + $instrumentname = $this->_instrument->testName; + $instrumentdata = $this->_instrument->getInstanceData(); + + $isDDE = strpos($instrumentdata['CommentID'], 'DDE_') === 0; + + $meta = [ + 'Candidate' => $this->_timepoint->getCandID(), + 'Visit' => $this->_timepoint->getVisitLabel(), + 'DDE' => $isDDE, + 'Instrument' => $instrumentname, + ]; + + $instrument = ['Data' => $instrumentdata]; + + return array_merge( + ['Meta' => $meta], + $instrument + ); + } +} +