Skip to content

Commit

Permalink
Merge pull request #61 from stephenkuhn214/master
Browse files Browse the repository at this point in the history
Slim4 Support w/ Test updates
  • Loading branch information
DavidePastore authored Dec 19, 2022
2 parents 54fc21a + 9bcca97 commit e594d0b
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 123 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: ['5.6', '7.0', '7.1', '7.2', '7.3']
php-versions: ['7.2', '7.3']
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}
steps:
- uses: actions/checkout@v2
Expand All @@ -27,9 +27,7 @@ jobs:
run: composer install --prefer-dist --no-progress

- name: PHP Unit tests
run: |
vendor/bin/phpunit --testsuite travis-ci
vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
run: vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

- name: Upload code coverage data
run: php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover
run: php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Via Composer
$ composer require davidepastore/slim-validation
```

Requires Slim 3.0.0 or newer.
Requires Slim 4.0.0 or newer.

## Usage

Expand All @@ -43,8 +43,11 @@ as it is middleware, you can also register it for all routes.

```php
use Respect\Validation\Validator as v;
use Slim\Factory\AppFactory;

$app = new \Slim\App();
require __DIR__ . '/../vendor/autoload.php';

$app = AppFactory::create();

//Create the validators
$usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
Expand Down Expand Up @@ -84,8 +87,11 @@ $app->run();

```php
use Respect\Validation\Validator as v;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

$app = new \Slim\App();
$app = AppFactory::create();

//Create the validators
$usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
Expand Down Expand Up @@ -138,8 +144,11 @@ $app->run();

```php
use Respect\Validation\Validator as v;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

$app = new \Slim\App();
$app = AppFactory::create();

//Create the validators
$routeParamValidator = v::numeric()->positive();
Expand Down Expand Up @@ -193,8 +202,11 @@ and you want to validate the `email.name` key. You can do it in this way:

```php
use Respect\Validation\Validator as v;
use Slim\Factory\AppFactory;

$app = new \Slim\App();
require __DIR__ . '/../vendor/autoload.php';

$app = AppFactory::create();

//Create the validators
$typeValidator = v::alnum()->noWhitespace()->length(3, 5);
Expand Down Expand Up @@ -251,8 +263,11 @@ and you want to validate the `email.name` key. You can do it in this way:

```php
use Respect\Validation\Validator as v;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

$app = new \Slim\App();
$app = AppFactory::create();

//Create the validators
$typeValidator = v::alnum()->noWhitespace()->length(3, 5);
Expand Down Expand Up @@ -292,8 +307,11 @@ You can provide a callable function to translate the errors.

```php
use Respect\Validation\Validator as v;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

$app = new \Slim\App();
$app = AppFactory::create();

//Create the validators
$usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
Expand Down
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
}
],
"require": {
"php": ">=5.5.0",
"php": "^7.2",
"psr/http-message": "^1.0",
"respect/validation": "^1.1 >=1.1.23"
"respect/validation": "^1.1 >=1.1.23",
"slim/http": "^1.1",
"slim/psr7": "^0.3|^1.6"
},
"require-dev": {
"slim/slim": "~3.0",
"phpunit/phpunit": "^4.0",
"slim/slim": "~4.0",
"phpunit/phpunit": "^8.0",
"scrutinizer/ocular": "1.5.*"
},
"autoload": {
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
Expand Down
14 changes: 7 additions & 7 deletions src/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace DavidePastore\Slim\Validation;

use Psr\Http\Server\RequestHandlerInterface;
use Respect\Validation\Exceptions\NestedValidationException;

/**
Expand Down Expand Up @@ -89,24 +90,23 @@ public function __construct($validators = null, $translator = null, $options = [
* Validation middleware invokable class.
*
* @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
* @param \Psr\Http\Message\ResponseInterface $response PSR7 response
* @param callable $next Next middleware
* @param RequestHandlerInterface $handler RequestHandler
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function __invoke($request, $response, $next)
public function __invoke($request, RequestHandlerInterface $handler)
{
$this->errors = [];
$params = $request->getParams();
$params = array_merge((array) $request->getAttribute('routeInfo')[2], $params);
$params = array_merge((array)$request->getAttribute('routeInfo')[2], $params);
$this->validate($params, $this->validators);

$request = $request->withAttribute($this->errors_name, $this->getErrors());
$request = $request->withAttribute($this->has_errors_name, $this->hasErrors());
$request = $request->withAttribute($this->validators_name, $this->getValidators());
$request = $request->withAttribute($this->translator_name, $this->getTranslator());

return $next($request, $response);
return $handler->handle($request);
}

/**
Expand All @@ -119,7 +119,7 @@ public function __invoke($request, $response, $next)
*/
private function validate($params = [], $validators = [], $actualKeys = [])
{
//Validate every parameters in the validators array
//Validate every parameter in the validators array
foreach ($validators as $key => $validator) {
$actualKeys[] = $key;
$param = $this->getNestedParam($params, $actualKeys);
Expand Down Expand Up @@ -155,7 +155,7 @@ private function getNestedParam($params = [], $keys = [])
return $params;
} else {
$firstKey = array_shift($keys);
if ($this->isArrayLike($params) && array_key_exists($firstKey, $params)) {
if ($this->isArrayLike($params) && array_key_exists($firstKey, (array)$params)) {
$params = (array) $params;
$paramValue = $params[$firstKey];

Expand Down
Loading

0 comments on commit e594d0b

Please sign in to comment.