Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: validate only fullpath (GroupPath+ControllerPath+Path) to al…
…low tiny controllers.
- Loading branch information
Showing
5 changed files
with
75 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Apitte\Core\Schema\Validation; | ||
|
||
use Apitte\Core\Exception\Logical\InvalidSchemaException; | ||
use Apitte\Core\Schema\Builder\SchemaBuilder; | ||
use Apitte\Core\Schema\Endpoint; | ||
use Apitte\Core\Utils\Helpers; | ||
|
||
class FullpathValidation implements IValidation | ||
{ | ||
|
||
/** | ||
* @param SchemaBuilder $builder | ||
* @return void | ||
*/ | ||
public function validate(SchemaBuilder $builder) | ||
{ | ||
$this->validateDuplicities($builder); | ||
} | ||
|
||
/** | ||
* @param SchemaBuilder $builder | ||
* @return void | ||
*/ | ||
protected function validateDuplicities(SchemaBuilder $builder) | ||
{ | ||
$controllers = $builder->getControllers(); | ||
|
||
// Init paths | ||
$paths = [ | ||
Endpoint::METHOD_GET => [], | ||
Endpoint::METHOD_POST => [], | ||
Endpoint::METHOD_PUT => [], | ||
Endpoint::METHOD_DELETE => [], | ||
Endpoint::METHOD_OPTIONS => [], | ||
Endpoint::METHOD_PATCH => [], | ||
]; | ||
|
||
foreach ($controllers as $controller) { | ||
foreach ($controller->getMethods() as $method) { | ||
foreach ($method->getMethods() as $httpMethod) { | ||
|
||
$maskp = array_merge( | ||
$controller->getGroupPaths(), | ||
[$controller->getPath()], | ||
[$method->getPath()] | ||
); | ||
$mask = implode('/', $maskp); | ||
$mask = Helpers::slashless($mask); | ||
$mask = '/' . trim($mask, '/'); | ||
|
||
if (array_key_exists($mask, $paths[$httpMethod])) { | ||
throw new InvalidSchemaException( | ||
sprintf( | ||
'Duplicate path "%s" in "%s()" and "%s()"', | ||
$mask, | ||
$controller->getClass() . '::' . $method->getName(), | ||
$paths[$httpMethod][$mask]['controller']->getClass() . '::' . $paths[$httpMethod][$mask]['method']->getName() | ||
) | ||
); | ||
} | ||
|
||
$paths[$httpMethod][$mask] = [ | ||
'controller' => $controller, | ||
'method' => $method, | ||
]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters