diff --git a/src/AnswerDto.php b/src/AnswerDto.php index 1ab545c..58b0167 100644 --- a/src/AnswerDto.php +++ b/src/AnswerDto.php @@ -1,27 +1,31 @@ imgUrl = $imgUrl; $that->text = $text; return $that; } public static function fromArray(array $array): AnswerDto { return self::fromParts($array['imgUrl'], $array['text']); } public function jsonSerialize() { $result = []; $result['imgUrl'] = $this->imgUrl; $result['text'] = $this->text; return $result; } -} \ No newline at end of file +} diff --git a/src/Answers.php b/src/Answers.php new file mode 100644 index 0000000..e9c59b8 --- /dev/null +++ b/src/Answers.php @@ -0,0 +1,8 @@ + 'IMG', 'INPUT' => 'INPUT', // free text 'OPTIONS' => 'OPTIONS' ]; + private function __consruct() { + + } + protected $questionText; protected $questionId; protected $questionType; protected $answersAvailable; public static function fromParts(string $id, string $text, string $type, array $answersAvailable): QuestionDto { $that = new QuestionDto(); $that->questionId = $id; $that->questionText = $text; $that->setQuestionType($type); $that->setAnswersAvailable($answersAvailable); return $that; } protected function setQuestionType(string $questionType) { if(!isset(self::QUESTION_TYPES[$questionType])) { throw new InvalidArgumentException("$questionType is not a valid question type"); } $this->questionType = $questionType; } protected function setAnswersAvailable(array $answersAvailable) { foreach($answersAvailable as $item) { if(!($item instanceof AnswerDto)) { throw new InvalidArgumentException("answersAvailable must be instanceof AnswerDto"); } } $this->answersAvailable = $answersAvailable; } public static function fromArray(array $array): QuestionDto { $a = []; foreach($array['answersAvailable'] as $el) { $a[] = AnswerDto::fromArray($el); } return self::fromParts($array['id'], $array['text'], $array['type'], $a); } public function jsonSerialize() { $result = []; $result['questionText'] = $this->questionText; $result['questionId'] = $this->questionId; $result['questionType'] = $this->questionType; $result['answersAvailable'] = $this->answersAvailable; return $result; } } diff --git a/src/Questions.php b/src/Questions.php index 68f939f..1fb3271 100644 --- a/src/Questions.php +++ b/src/Questions.php @@ -1,29 +1,33 @@ getAttribute('ParsedBody', []); if(!isset($payload['language'])) { - return new JsonResponse(['error' => 'Add language to request', 400]); + return new JsonResponse(['error' => 'Add language to request'], 400); } // TODO: query database $id = 123; + $questions = []; + for($i = 0; $i < 3; $i++) { + $answers = []; + $answers[0] = AnswerDto::fromParts('https://upload.wikimedia.org/wikipedia/commons/thumb/d/d4/George-W-Bush.jpeg/440px-George-W-Bush.jpeg', 'This is some text'); + $answers[1] = AnswerDto::fromParts('https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.jpg/440px-President_Barack_Obama.jpg', 'This is some text'); + $answers[2] = AnswerDto::fromParts('https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Donald_Trump_official_portrait.jpg/440px-Donald_Trump_official_portrait.jpg', 'This is some text'); + $questions[$i] = QuestionDto::fromParts('q' . mt_rand(100, 999), 'Which one of these persons is Barack Obama?', QuestionDto::QUESTION_TYPES['IMG'], $answers); + } - $response = [ - 'sessionId' => $id, - - ]; + return new JsonResponse(WikiApiDto::fromParts($id, $questions), 200); } } diff --git a/src/Router.php b/src/Router.php index 7481226..c7e9697 100644 --- a/src/Router.php +++ b/src/Router.php @@ -1,37 +1,37 @@ get('/test', Test::class); $r->post('/questions', Questions::class); - $r->put('/answers', ''); + $r->put('/answers', Answers::class); }); $route = $dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath()); switch($route[0]) { case \FastRoute\Dispatcher::FOUND: /** @var MiddlewareInterface $method */ $method = $route[1]; $request = $request->withAttribute('Method', $method); return $handler->handle($request); default: case \FastRoute\Dispatcher::NOT_FOUND: return new JsonResponse(['error' => 'Not found'], 404); case \FastRoute\Dispatcher::METHOD_NOT_ALLOWED: return new JsonResponse(['error' => 'Method not allowed'], 405, ['Allow' => implode(', ', $route[1])]); } } } diff --git a/src/UserAnswerDto.php b/src/UserAnswerDto.php new file mode 100644 index 0000000..401f1b0 --- /dev/null +++ b/src/UserAnswerDto.php @@ -0,0 +1,25 @@ + $v) { + $userAnswerDto->$k = $v; + } + return $userAnswerDto; + } + + public function jsonSerialize() { + $result = parent::jsonSerialize(); + foreach($this as $k => $v) { + $result[$k] = $v; + } + return $result; + } +} diff --git a/src/WikiApiDto.php b/src/WikiApiDto.php index 2d845ce..37c5f2a 100644 --- a/src/WikiApiDto.php +++ b/src/WikiApiDto.php @@ -1,24 +1,31 @@ sessionId = $sessionId; $that->questionList = $questionList; return $that; } + + public function jsonSerialize() { + $result = []; + $result['sessionId'] = $this->sessionId; + $result['questionList'] = $this->questionList; + return $result; + } }