diff --git a/.gitignore b/.gitignore index eddf617..51a274a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ .idea/ *~ +tests/tmp +composer.lock +vendor \ No newline at end of file diff --git a/src/IParameters.php b/src/IParameters.php index bd8ec1b..59eb84f 100644 --- a/src/IParameters.php +++ b/src/IParameters.php @@ -13,11 +13,11 @@ interface IParameters { - public function post($key = NULL); + public function post($key = NULL, $isRequired = FALSE); - public function get($key = NULL, $acceptPost = TRUE); + public function get($key = NULL, $isRequired = FALSE); - public function path($key); + public function path($key, $isRequired = FALSE); } diff --git a/src/Parameters.php b/src/Parameters.php index 37cb04c..0e8844b 100644 --- a/src/Parameters.php +++ b/src/Parameters.php @@ -11,6 +11,7 @@ use Nette\Utils\ArrayHash; use Nette\Utils\Json; use Nette\Utils\Strings; +use RestServer\Exceptions\MissingRequiredParameterException; use Tracy\Debugger; /** @@ -33,24 +34,29 @@ public function __construct(IRequest $request, array $pathParameters){ $this->contentType = isset($request->headers['content-type']) ? $request->headers['content-type'] : 'text/plain'; } - public function post($key = NULL) { + public function post($key = NULL, $isRequired = FALSE) { if(Strings::substring($this->contentType, 0, 16) == 'application/json'){ $post = ArrayHash::from(json_decode(file_get_contents('php://input'), TRUE)); - return isset($post[$key]) ? $post[$key] : null; - } - return $this->request->getPost($key); + $return = isset($post[$key]) ? $post[$key] : null; + } else + $return = $this->request->getPost($key); + if(!$return && $isRequired) + throw new MissingRequiredParameterException('Parameter "'.$key.'" is required.'); + return $return; } - public function get($key = NULL, $acceptPost = TRUE){ + public function get($key = NULL, $isRequired = FALSE){ $return = $this->request->getQuery($key); - if(!$return && $acceptPost){ - $return = $this->post($key); - } + if(!$return && $isRequired) + throw new MissingRequiredParameterException('Parameter "'.$key.'" is required.'); return $return; } - public function path($name){ - return array_key_exists($name, $this->pathParameters) ? $this->pathParameters[$name] : null; + public function path($name, $isRequired = FALSE){ + $return = array_key_exists($name, $this->pathParameters) ? $this->pathParameters[$name] : null; + if(!$return && $isRequired) + throw new MissingRequiredParameterException('Parameter "'.$name.'" is required.'); + return $return; } } diff --git a/tests/Route.phpt b/tests/Route.phpt new file mode 100644 index 0000000..72c5595 --- /dev/null +++ b/tests/Route.phpt @@ -0,0 +1,13 @@ +/detail', Route::GET, 'x'); +Assert::type('array', $route->match('/user/10/detail', Route::GET)); + +$route = new Route('/user//detail', Route::GET, 'x'); +Assert::type('array', $route->match('/user/jksdhk.ksjd-dskjlf/detail', Route::GET)); + diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..f400d34 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,22 @@ +