Skip to content

Commit

Permalink
Require php 7.1, Using php 7.1 return types + method argumetn types d…
Browse files Browse the repository at this point in the history
…eclarations, ApiRouterExtension: add ApiRouter annotated presenter route only if there are some remaining methods without ApiRouter annotated presenter method
  • Loading branch information
paveljanda committed Jul 20, 2017
1 parent d91a9a0 commit d63c614
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 218 deletions.
1 change: 0 additions & 1 deletion .travis.yml
@@ -1,7 +1,6 @@
language: php

php:
- 7.0
- 7.1

install:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -25,7 +25,7 @@
}
},
"require": {
"php": ">= 7.0",
"php": ">= 7.1",
"nette/http" : "^2.3|^2.4",
"nette/utils": "^2.3|^2.4",
"nette/application": "^2.3|^2.4",
Expand Down
72 changes: 16 additions & 56 deletions src/ApiRoute.php
Expand Up @@ -13,6 +13,7 @@
use Nette;
use Nette\Application\IRouter;
use Nette\Application\Request;
use Nette\SmartObject;
use Nette\Utils\Strings;

/**
Expand All @@ -23,14 +24,15 @@
*/
class ApiRoute extends ApiRouteSpec implements IRouter
{
use SmartObject;

/**
* @var callable[]
*/
public $onMatch;

/**
* @var string
* @var string|null
*/
private $presenter;

Expand Down Expand Up @@ -72,12 +74,7 @@ class ApiRoute extends ApiRouteSpec implements IRouter
private $placeholder_order = [];


/**
* @param mixed $data
* @param string $presenter
* @param array $data
*/
public function __construct($path, $presenter = null, array $data = [])
public function __construct($path, string $presenter = null, array $data = [])
{
/**
* Interface for setting route via annotation or directly
Expand Down Expand Up @@ -117,31 +114,19 @@ public function __construct($path, $presenter = null, array $data = [])
}


/**
* @param string $presenter
* @return void
*/
public function setPresenter($presenter)
public function setPresenter(?string $presenter): void
{
$this->presenter = $presenter;
}


/**
* @return string
*/
public function getPresenter()
public function getPresenter(): ?string
{
return $this->presenter;
}


/**
* @param string $action
* @param string $method
* @return void
*/
public function setAction($action, $method = null)
public function setAction(string $action, string $method = null): void
{
if ($method === null) {
$method = array_search($action, $this->default_actions, true);
Expand All @@ -155,21 +140,16 @@ public function setAction($action, $method = null)
}


/**
* @param string $string
* @return string
*/
private function prepareForMatch($string)
private function prepareForMatch(string $string): string
{
return sprintf('/%s/', str_replace('/', '\/', $string));
}


/**
* Get all parameters from url mask
* @return array
*/
public function getPlacehodlerParameters()
public function getPlacehodlerParameters(): array
{
if (!empty($this->placeholder_order)) {
return array_filter($this->placeholder_order);
Expand All @@ -187,9 +167,8 @@ public function getPlacehodlerParameters()

/**
* Get required parameters from url mask
* @return array
*/
public function getRequiredParams()
public function getRequiredParams(): array
{
$regex = '/\[[^\[]+?\]/';
$path = $this->getPath();
Expand All @@ -208,11 +187,7 @@ public function getRequiredParams()
}


/**
* @param Nette\Http\IRequest $httpRequest
* @return void
*/
public function resolveFormat(Nette\Http\IRequest $httpRequest)
public function resolveFormat(Nette\Http\IRequest $httpRequest): void
{
if ($this->getFormat()) {
return;
Expand All @@ -232,18 +207,12 @@ public function resolveFormat(Nette\Http\IRequest $httpRequest)
}


/**
* @return string
*/
public function getFormatFull()
public function getFormatFull(): string
{
return $this->formats[$this->getFormat()];
}


/**
* @param array $methods
*/
public function setMethods(array $methods)
{
foreach ($methods as $method => $action) {
Expand All @@ -260,20 +229,13 @@ public function setMethods(array $methods)
}


/**
* @return array
*/
public function getMethods()
public function getMethods(): array
{
return array_keys(array_filter($this->actions));
}


/**
* @param Nette\Http\IRequest $request
* @return string
*/
public function resolveMethod(Nette\Http\IRequest $request)
public function resolveMethod(Nette\Http\IRequest $request): string
{
if (!empty($request->getHeader('X-HTTP-Method-Override'))) {
return Strings::upper($request->getHeader('X-HTTP-Method-Override'));
Expand All @@ -296,9 +258,8 @@ public function resolveMethod(Nette\Http\IRequest $request)

/**
* Maps HTTP request to a Request object.
* @return Request|NULL
*/
public function match(Nette\Http\IRequest $httpRequest)
public function match(Nette\Http\IRequest $httpRequest): ?Request
{
/**
* ApiRoute can be easily disabled
Expand Down Expand Up @@ -410,9 +371,8 @@ public function match(Nette\Http\IRequest $httpRequest)

/**
* Constructs absolute URL from Request object.
* @return string|NULL
*/
public function constructUrl(Request $request, Nette\Http\Url $url)
public function constructUrl(Request $request, Nette\Http\Url $url): ?string
{
if ($this->presenter != $request->getPresenterName()) {
return null;
Expand Down

0 comments on commit d63c614

Please sign in to comment.