Skip to content

Commit

Permalink
Merge pull request #3 from zfegg/develop
Browse files Browse the repository at this point in the history
1. Fix default invalidHandler.
2. Add set psr attribute
  • Loading branch information
Moln committed Jan 17, 2017
2 parents d377109 + 68bf16b commit 1f629c5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/ContentValidationListenerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Zfegg\ContentValidation;

use Interop\Container\ContainerInterface;

/**
* Class ContentValidatioinListenerFactory
* @package Zfegg\ContentValidation
*/
class ContentValidationListenerFactory
{

public function __invoke(ContainerInterface $container)
{
$listener = new ContentValidationListener();
$listener->setInputFilterManager($container->get('InputFilterManager'));

return $listener;
}
}
29 changes: 26 additions & 3 deletions src/ContentValidationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,33 @@
*/
class ContentValidationMiddleware
{
const INPUT_FILTER_NAME = 'Zfegg\ContentValidation\InputFilter';
use ContentValidationTrait;

const INPUT_FILTER_NAME = 'Zfegg\ContentValidation\InputFilter';
const INPUT_FILTER = 'input_filter';

protected $inputFilter;

protected $requestInputFilterKeyName = self::INPUT_FILTER;

/**
* @return string
*/
public function getRequestInputFilterKeyName()
{
return $this->requestInputFilterKeyName;
}

/**
* @param string $requestInputFilterKeyName
* @return $this
*/
public function setRequestInputFilterKeyName($requestInputFilterKeyName)
{
$this->requestInputFilterKeyName = $requestInputFilterKeyName;
return $this;
}

/**
* @return InputFilterInterface
*/
Expand All @@ -39,8 +61,8 @@ public function setInputFilter(InputFilterInterface $inputFilter)
public function __construct(InputFilterPluginManager $inputFilters = null, callable $invalidHandler = null)
{
$defaultInvalidHandler = function ($self, $request, ResponseInterface $response, $next) {
$response->withStatus(422);
$response->withHeader('Content-Type', 'application/json');
$response = $response->withStatus(422);
$response = $response->withHeader('Content-Type', 'application/json');
$response->getBody()->write(json_encode([
'status' => 422,
'detail' => 'Failed Validation',
Expand Down Expand Up @@ -73,6 +95,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
$inputFilter = $inputFilters->get($inputFilterName);

$this->setInputFilter($inputFilter);
$request = $request->withAttribute(self::INPUT_FILTER, $inputFilter);

if ($request->getMethod() == 'GET') {
$data = $request->getQueryParams();
Expand Down
5 changes: 4 additions & 1 deletion test/ContentValidationMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ public function testInvoke($inputFilterName, ServerRequest $request, $responseBo
$request = $request->withAttribute(ContentValidationMiddleware::INPUT_FILTER_NAME, $inputFilterName);

$this->initInputFilters($middleware->getInputFilterManager());
$response = $middleware($request, $response, function ($request, Response $response) {
$response = $middleware($request, $response, function (ServerRequestInterface $request, Response $response)
use ($middleware) {
$inputFilter = $request->getAttribute(ContentValidationMiddleware::INPUT_FILTER);
$this->assertEquals($middleware->getInputFilter(), $inputFilter);
$this->assertInstanceOf(ServerRequestInterface::class, $request);
$this->assertInstanceOf(ResponseInterface::class, $response);

Expand Down

0 comments on commit 1f629c5

Please sign in to comment.