diff --git a/src/AnswerDto.php b/src/AnswerDto.php index 58b0167..4cfbe6a 100644 --- a/src/AnswerDto.php +++ b/src/AnswerDto.php @@ -1,31 +1,42 @@ imgUrl = $imgUrl; $that->text = $text; + if($userAnswer !== null) { + $that->userAnswer = UserAnswerDto::fromArray($userAnswer); + } return $that; } public static function fromArray(array $array): AnswerDto { - return self::fromParts($array['imgUrl'], $array['text']); + if(isset($array['userAnswer'])) { + return self::fromParts($array['imgUrl'], $array['text'], $array['userAnswer']); + } else { + return self::fromParts($array['imgUrl'], $array['text']); + } } public function jsonSerialize() { $result = []; + if($this->userAnswer !== null) { + $result['userAnswer'] = $this->userAnswer; + } $result['imgUrl'] = $this->imgUrl; $result['text'] = $this->text; return $result; } } diff --git a/src/Answers.php b/src/Answers.php index e9c59b8..4277133 100644 --- a/src/Answers.php +++ b/src/Answers.php @@ -1,8 +1,21 @@ getAttribute('ParsedBody', []); + + $questions = WikiApiDto::fromArray($payload); + + // TODO: check that user is a human or a bot + + return new JsonResponse($questions); + } } \ No newline at end of file diff --git a/src/Index.php b/src/Index.php new file mode 100644 index 0000000..14e28b4 --- /dev/null +++ b/src/Index.php @@ -0,0 +1,15 @@ + '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); + return self::fromParts($array['questionId'], $array['questionText'], $array['questionType'], $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/Router.php b/src/Router.php index c7e9697..e2e2feb 100644 --- a/src/Router.php +++ b/src/Router.php @@ -1,37 +1,38 @@ get('/', Index::class); $r->get('/test', Test::class); $r->post('/questions', Questions::class); $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 index 401f1b0..15ac42b 100644 --- a/src/UserAnswerDto.php +++ b/src/UserAnswerDto.php @@ -1,25 +1,29 @@ $v) { - $userAnswerDto->$k = $v; + $that->$k = $v; } - return $userAnswerDto; + return $that; + } + + public static function fromParts(): UserAnswerDto { + return new UserAnswerDto(); } public function jsonSerialize() { - $result = parent::jsonSerialize(); + $result = []; foreach($this as $k => $v) { $result[$k] = $v; } return $result; } }