diff --git a/src/AnswerDto.php b/src/AnswerDto.php new file mode 100644 index 0000000..1ab545c --- /dev/null +++ b/src/AnswerDto.php @@ -0,0 +1,27 @@ +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/Controller.php b/src/Controller.php index ec006dc..d6c2cc5 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -1,25 +1,26 @@ newInstance($queue); return $relay->handle($request); } } \ No newline at end of file diff --git a/src/EnsureJson.php b/src/EnsureJson.php new file mode 100644 index 0000000..5358ae2 --- /dev/null +++ b/src/EnsureJson.php @@ -0,0 +1,32 @@ +getMethod(); + if($method === 'POST' || $method === 'PUT' || $method === 'PATCH') { + /** @noinspection PhpUndefinedMethodInspection it exists. */ + if(explode(';', $request->getHeaderLine('content-type') ?? '', 2)[0] !== 'application/json') { + return new TextResponse('Request body must contain JSON, check your Content-Type', 415); + } + $json = $request->getBody()->getContents(); + $json = json_decode($json, true); + if($json === null) { + return new JsonResponse(['error' => 'Cannot decode request body: ' . json_last_error_msg()], 400); + } + $request = $request->withAttribute('ParsedBody', $json); + } + + return $handler->handle($request); + } +} diff --git a/src/QuestionDto.php b/src/QuestionDto.php new file mode 100644 index 0000000..1761616 --- /dev/null +++ b/src/QuestionDto.php @@ -0,0 +1,62 @@ + 'IMG', + 'INPUT' => 'INPUT', // free text + 'OPTIONS' => 'OPTIONS' + ]; + + 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 new file mode 100644 index 0000000..68f939f --- /dev/null +++ b/src/Questions.php @@ -0,0 +1,29 @@ +getAttribute('ParsedBody', []); + + if(!isset($payload['language'])) { + return new JsonResponse(['error' => 'Add language to request', 400]); + } + + // TODO: query database + $id = 123; + + $response = [ + 'sessionId' => $id, + + ]; + } +} diff --git a/src/Router.php b/src/Router.php index 5774c8a..7481226 100644 --- a/src/Router.php +++ b/src/Router.php @@ -1,36 +1,37 @@ get('/test', Test::class); - $r->get('/whatever', ''); + $r->post('/questions', Questions::class); + $r->put('/answers', ''); }); $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/WikiApiDto.php b/src/WikiApiDto.php new file mode 100644 index 0000000..2d845ce --- /dev/null +++ b/src/WikiApiDto.php @@ -0,0 +1,24 @@ +sessionId = $sessionId; + $that->questionList = $questionList; + return $that; + } +}