Skip to content

Commit

Permalink
Merge pull request #15 from koriym/input
Browse files Browse the repository at this point in the history
@inputvalidation annotated method throw exception when validation failed
  • Loading branch information
koriym committed Sep 15, 2015
2 parents 98f649f + 46f7750 commit ba957db
Show file tree
Hide file tree
Showing 17 changed files with 142 additions and 34 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ build
vendor/
composer.phar
composer.lock
tmp/
15 changes: 15 additions & 0 deletions README.JA.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ class MyForm extends AbstractAuraForm

## Validation Exception

`@FormValidation`の代わりに`@InputValidation`とアノテートするとバリデーションが失敗したときに`Ray\WebFormModule\Exception\ValidationException`が投げられるよになります。この場合はHTML表現は使われません。Web APIアプリケーションなどに便利です。

```php
use Ray\WebFormModule\Annotation\InputValidation;

class Foo
{
/**
* @InputValidation(form="form1")
*/
public function createAction($name)
{
// ...
}
```
以下のように `Ray\WebFormModule\FormVndErrorModule`をインストールするとフォームのバリデーションが失敗したときに`Ray\WebFormModule\Exception\ValidationException`例外が投げられるよになります。

```php
Expand Down
3 changes: 0 additions & 3 deletions src/AbstractForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,10 @@ public function getFailureMessages()
return $messages;
}


/**
*
* Returns all the fields collection
*
* @return \ArrayIterator
*
*/
public function getIterator()
{
Expand Down
19 changes: 19 additions & 0 deletions src/Annotation/AbstractValidation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* This file is part of the Ray.WebFormModule package
*
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Ray\WebFormModule\Annotation;

/**
* @Annotation
* @Target("METHOD")
*/
class AbstractValidation
{
/**
* @var string
*/
public $form = 'form';
}
7 changes: 1 addition & 6 deletions src/Annotation/FormValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @Annotation
* @Target("METHOD")
*/
final class FormValidation
final class FormValidation extends AbstractValidation
{
/**
* @var bool
Expand All @@ -23,9 +23,4 @@ final class FormValidation
* @var string
*/
public $onFailure;

/**
* @var string
*/
public $form = 'form';
}
15 changes: 15 additions & 0 deletions src/Annotation/InputValidation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* This file is part of the Ray.WebFormModule package
*
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Ray\WebFormModule\Annotation;

/**
* @Annotation
* @Target("METHOD")
*/
final class InputValidation extends AbstractValidation
{
}
6 changes: 3 additions & 3 deletions src/AntiCsrf.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ final class AntiCsrf implements AntiCsrfInterface
private $session;

/**
* @param Session $session
* @param bool|null $isCli
s */
* @param Session $session
* @param bool|null $isCli
s */
public function __construct(Session $session, $isCli = null)
{
$this->session = $session;
Expand Down
12 changes: 7 additions & 5 deletions src/AuraInputInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\Common\Annotations\Reader;
use Ray\Aop\MethodInterceptor;
use Ray\Aop\MethodInvocation;
use Ray\WebFormModule\Annotation\AbstractValidation;
use Ray\WebFormModule\Annotation\FormValidation;
use Ray\WebFormModule\Exception\InvalidArgumentException;
use Ray\WebFormModule\Exception\InvalidFormPropertyException;
Expand All @@ -19,15 +20,16 @@ class AuraInputInterceptor implements MethodInterceptor
/**
* @var Reader
*/
private $reader;
protected $reader;

/**
* @var FailureHandlerInterface
*/
private $failureHandler;
protected $failureHandler;

/**
* @param Reader $reader Annotation reader
* @param Reader $reader
* @param FailureHandlerInterface $handler
*/
public function __construct(Reader $reader, FailureHandlerInterface $handler)
{
Expand All @@ -44,7 +46,7 @@ public function invoke(MethodInvocation $invocation)
{
$object = $invocation->getThis();
/* @var $formValidation FormValidation */
$formValidation = $this->reader->getMethodAnnotation($invocation->getMethod(), FormValidation::class);
$formValidation = $this->reader->getMethodAnnotation($invocation->getMethod(), AbstractValidation::class);
$form = $this->getFormProperty($formValidation, $object);
$data = $object instanceof SubmitInterface ? $object->submit() : $this->getNamedArguments($invocation);
$isValid = $this->isValid($data, $form);
Expand Down Expand Up @@ -99,7 +101,7 @@ public function isValid(array $submit, AbstractForm $form)
*
* @return AbstractForm
*/
private function getFormProperty(FormValidation $formValidation, $object)
private function getFormProperty(AbstractValidation $formValidation, $object)
{
if (! property_exists($object, $formValidation->form)) {
throw new InvalidFormPropertyException($formValidation->form);
Expand Down
7 changes: 7 additions & 0 deletions src/AuraInputModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Ray\Di\AbstractModule;
use Ray\Di\Scope;
use Ray\WebFormModule\Annotation\FormValidation;
use Ray\WebFormModule\Annotation\InputValidation;

class AuraInputModule extends AbstractModule
{
Expand All @@ -33,8 +34,14 @@ protected function configure()
$this->bind(FilterInterface::class)->to(Filter::class);
$this->bind(AntiCsrfInterface::class)->to(AntiCsrf::class)->in(Scope::SINGLETON);
$this->bind(FailureHandlerInterface::class)->to(OnFailureMethodHandler::class);
$this->bind(FailureHandlerInterface::class)->annotatedWith('vnd_error')->to(VndErrorHandler::class)->in(Scope::SINGLETON);
$this->bind(HelperLocatorFactory::class);
$this->bind(FilterFactory::class);
$this->bindInterceptor(
$this->matcher->any(),
$this->matcher->annotatedWith(InputValidation::class),
[InputValidationInterceptor::class]
);
$this->bindInterceptor(
$this->matcher->any(),
$this->matcher->annotatedWith(FormValidation::class),
Expand Down
5 changes: 2 additions & 3 deletions src/FailureHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
*/
namespace Ray\WebFormModule;

use Aura\Input\Form;
use Ray\Aop\MethodInvocation;
use Ray\WebFormModule\Annotation\FormValidation;
use Ray\WebFormModule\Annotation\AbstractValidation;

interface FailureHandlerInterface
{
public function handle(FormValidation $formValidation, MethodInvocation $invocation, AbstractForm $form);
public function handle(AbstractValidation $formValidation, MethodInvocation $invocation, AbstractForm $form);
}
29 changes: 29 additions & 0 deletions src/InputValidationInterceptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* This file is part of the Ray.WebFormModule package
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*/
namespace Ray\WebFormModule;

use Doctrine\Common\Annotations\Reader;
use Ray\Di\Di\Named;

class InputValidationInterceptor extends AuraInputInterceptor
{
/**
* @var FailureHandlerInterface
*/
protected $failureHandler;

/**
* @param Reader $reader Annotation reader
*
* @Named("handler=vnd_error")
*/
public function __construct(Reader $reader, FailureHandlerInterface $handler)
{
$this->reader = $reader;
$this->failureHandler = $handler;
}
}
8 changes: 4 additions & 4 deletions src/OnFailureMethodHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/
namespace Ray\WebFormModule;

use Aura\Input\Form;
use Ray\Aop\MethodInvocation;
use Ray\WebFormModule\Annotation\AbstractValidation;
use Ray\WebFormModule\Annotation\FormValidation;
use Ray\WebFormModule\Exception\InvalidOnFailureMethod;

Expand All @@ -16,13 +16,13 @@ final class OnFailureMethodHandler implements FailureHandlerInterface
/**
* {@inheritdoc}
*/
public function handle(FormValidation $formValidation, MethodInvocation $invocation, AbstractForm $form)
public function handle(AbstractValidation $formValidation, MethodInvocation $invocation, AbstractForm $form)
{
unset($form);
$args = (array) $invocation->getArguments();
$object = $invocation->getThis();
if (! method_exists($object, $formValidation->onFailure)) {
throw new InvalidOnFailureMethod($formValidation->onFailure);
if (! $formValidation instanceof FormValidation || ! method_exists($object, $formValidation->onFailure)) {
throw new InvalidOnFailureMethod(get_class($invocation->getThis()));
}

return call_user_func_array([$invocation->getThis(), $formValidation->onFailure], $args);
Expand Down
5 changes: 2 additions & 3 deletions src/VndErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
*/
namespace Ray\WebFormModule;

use Aura\Input\Form;
use Doctrine\Common\Annotations\Reader;
use Ray\Aop\MethodInvocation;
use Ray\WebFormModule\Annotation\FormValidation;
use Ray\WebFormModule\Annotation\AbstractValidation;
use Ray\WebFormModule\Annotation\VndError;
use Ray\WebFormModule\Exception\ValidationException;

Expand All @@ -28,7 +27,7 @@ public function __construct(Reader $reader)
/**
* {@inheritdoc}
*/
public function handle(FormValidation $formValidation, MethodInvocation $invocation, AbstractForm $form)
public function handle(AbstractValidation $formValidation, MethodInvocation $invocation, AbstractForm $form)
{
unset($formValidation);
$vndError = $this->reader->getMethodAnnotation($invocation->getMethod(), VndError::class);
Expand Down
4 changes: 0 additions & 4 deletions tests/AbstractAuraFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

namespace Ray\WebFormModule;

use Aura\Html\HelperLocatorFactory;
use Aura\Input\Builder;
use Aura\Input\Filter;

class AbstractAuraFormTest extends \PHPUnit_Framework_TestCase
{
/**
Expand Down
1 change: 0 additions & 1 deletion tests/AbstractFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,4 @@ public function testAntiCsrfViolation()
$this->form->setAntiCsrf(new AntiCsrf($session, false));
$this->form->apply([]);
}

}
8 changes: 7 additions & 1 deletion tests/AuraInputModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Ray\Aop\WeavedInterface;
use Ray\Di\Injector;
use Ray\WebFormModule\Exception\ValidationException;

class AuraInputModuleTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -28,7 +29,12 @@ public function testAuraInputModule()
$this->assertInstanceOf(WeavedInterface::class, $controller);
}

public function testFormModule()
public function testExceptionOnFailure()
{
$this->setExpectedException(ValidationException::class);
$injector = new Injector(new FakeModule, __DIR__ . '/tmp');
/** @var $controller FakeInputValidationController */
$controller = $injector->getInstance(FakeInputValidationController::class);
$controller->createAction('');
}
}
31 changes: 31 additions & 0 deletions tests/Fake/FakeInputValidationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Ray\WebFormModule;

use Ray\Di\Di\Inject;
use Ray\Di\Di\Named;
use Ray\WebFormModule\Annotation\InputValidation;

class FakeInputValidationController
{
/**
* @var FormInterface
*/
protected $form1;

/**
* @Inject
* @Named("contact_form")
*/
public function setForm(FormInterface $form)
{
$this->form1 = $form;
}

/**
* @InputValidation(form="form1")
*/
public function createAction($name)
{
}
}

0 comments on commit ba957db

Please sign in to comment.