diff --git a/composer.json b/composer.json index 1c32169..d5b751b 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,6 @@ }, "suggest": { "illuminate/events": "Required to use Laravel Dispatcher as form Event Dispatcher (^8.0).", - "illuminate/validation": "Required to use step data validation on Before Handle Step event (^8.0).", "illuminate/view": "Required to render Laravel views (^8.0).", "illuminate/session": "Required to use Laravel Session as Stepped Form storage (^8.0).", "illuminate/config": "Required to use Laravel config (^8.0)." diff --git a/src/Entity/RulesDefinition.php b/src/Entity/RulesDefinition.php deleted file mode 100644 index 4584475..0000000 --- a/src/Entity/RulesDefinition.php +++ /dev/null @@ -1,48 +0,0 @@ - - */ - private array $rules, - /** - * @var array - */ - private array $messages = [], - /** - * @var array - */ - private array $customAttributes = [], - ) { - } - - /** - * @return array - */ - public function getRules(): array - { - return $this->rules; - } - - /** - * @return array - */ - public function getMessages(): array - { - return $this->messages; - } - - /** - * @return array - */ - public function getCustomAttributes(): array - { - return $this->customAttributes; - } -} diff --git a/src/Event/Listener/BeforeHandleStepListener.php b/src/Event/Listener/BeforeHandleStepListener.php deleted file mode 100644 index aa106b3..0000000 --- a/src/Event/Listener/BeforeHandleStepListener.php +++ /dev/null @@ -1,31 +0,0 @@ -getStep()->getStep(); - - if ($step instanceof ValidateStepInterface && is_array($event->getData())) { - $this->validator->validate($event->getData(), $step->getRulesDefinition($event->getEntity())); - } - } -} diff --git a/src/ServiceProvider/ServiceProvider.php b/src/ServiceProvider/ServiceProvider.php index c735fd8..4ca5751 100644 --- a/src/ServiceProvider/ServiceProvider.php +++ b/src/ServiceProvider/ServiceProvider.php @@ -7,7 +7,6 @@ use Illuminate\Contracts\Config\Repository; use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Contracts\Events\Dispatcher; -use Illuminate\Contracts\Validation\Factory as ValidationFactory; use Illuminate\Contracts\View\Factory as ViewFactory; use Illuminate\Support\ServiceProvider as LaravelServiceProvider; use Lexal\HttpSteppedForm\ExceptionNormalizer\ExceptionNormalizer; @@ -15,11 +14,8 @@ use Lexal\HttpSteppedForm\Renderer\RendererInterface; use Lexal\HttpSteppedForm\Routing\RedirectorInterface; use Lexal\LaravelSteppedForm\Event\Dispatcher\EventDispatcher; -use Lexal\LaravelSteppedForm\Event\Listener\BeforeHandleStepListener; use Lexal\LaravelSteppedForm\Renderer\Renderer; use Lexal\LaravelSteppedForm\Routing\Redirector; -use Lexal\LaravelSteppedForm\Validator\Validator; -use Lexal\LaravelSteppedForm\Validator\ValidatorInterface; use Lexal\SteppedForm\Data\FormDataStorage; use Lexal\SteppedForm\Data\FormDataStorageInterface; use Lexal\SteppedForm\Data\StepControl; @@ -27,7 +23,6 @@ use Lexal\SteppedForm\Data\Storage\StorageInterface; use Lexal\SteppedForm\EntityCopy\EntityCopyInterface; use Lexal\SteppedForm\EntityCopy\SimpleEntityCopy; -use Lexal\SteppedForm\EventDispatcher\Event\BeforeHandleStep; use Lexal\SteppedForm\EventDispatcher\EventDispatcherInterface; use Lexal\SteppedForm\State\FormState; use Lexal\SteppedForm\State\FormStateInterface; @@ -46,10 +41,6 @@ class ServiceProvider extends LaravelServiceProvider { private const CONFIG_FILENAME = 'stepped-form.php'; - /** - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface - */ public function boot(): void { $path = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . self::CONFIG_FILENAME; @@ -59,13 +50,6 @@ public function boot(): void ]); $this->mergeConfigFrom($path, 'stepped-form'); - - if ($this->app->bound(Dispatcher::class) && $this->app->bound(ValidationFactory::class)) { - /** @var Dispatcher $dispatcher */ - $dispatcher = $this->app->get(Dispatcher::class); - - $dispatcher->listen(BeforeHandleStep::class, [BeforeHandleStepListener::class, 'handle']); - } } public function register(): void @@ -86,7 +70,6 @@ public function register(): void $this->registerFormState(); $this->registerStepsBuilder(); $this->registerEventDispatcher(); - $this->registerValidator(); $this->registerEntityCopy(); } @@ -141,11 +124,6 @@ private function registerEventDispatcher(): void } } - private function registerValidator(): void - { - $this->app->singleton(ValidatorInterface::class, Validator::class); - } - private function registerEntityCopy(): void { $this->app->singleton(EntityCopyInterface::class, SimpleEntityCopy::class); diff --git a/src/Steps/ValidateStepInterface.php b/src/Steps/ValidateStepInterface.php deleted file mode 100644 index ee8aace..0000000 --- a/src/Steps/ValidateStepInterface.php +++ /dev/null @@ -1,15 +0,0 @@ -validatorFactory->make( - $data, - $definition->getRules(), - $definition->getMessages(), - $definition->getCustomAttributes(), - ); - - if ($validator->fails()) { - throw new ValidatorException($validator->errors()->messages()); - } - } -} diff --git a/src/Validator/ValidatorInterface.php b/src/Validator/ValidatorInterface.php deleted file mode 100644 index 0107ec7..0000000 --- a/src/Validator/ValidatorInterface.php +++ /dev/null @@ -1,20 +0,0 @@ - $data - * - * @throws ValidatorException - */ - public function validate(array $data, RulesDefinition $definition): void; -} diff --git a/tests/Event/Listener/BeforeHandleStepListenerTest.php b/tests/Event/Listener/BeforeHandleStepListenerTest.php deleted file mode 100644 index 2619545..0000000 --- a/tests/Event/Listener/BeforeHandleStepListenerTest.php +++ /dev/null @@ -1,101 +0,0 @@ -validator->expects($this->once()) - ->method('validate') - ->with(['data' => 'test'], new RulesDefinition(['data' => 'required'], ['data' => 'test message'])); - - $step = $this->createMock(ValidatableStepInterface::class); - - $step->expects($this->once()) - ->method('getRulesDefinition') - ->with(['entity']) - ->willReturn(new RulesDefinition(['data' => 'required'], ['data' => 'test message'])); - - $event = new BeforeHandleStep(['data' => 'test'], ['entity'], new Step('key', $step)); - - $this->listener->handle($event); - } - - public function testHandleWithErrors(): void - { - $this->expectExceptionObject(new ValidatorException(['data' => 'required message'])); - - $this->validator->expects($this->once()) - ->method('validate') - ->with(['data' => 'test'], new RulesDefinition(['data' => 'required'])) - ->willThrowException(new ValidatorException(['data' => 'required message'])); - - $step = $this->createMock(ValidatableStepInterface::class); - - $step->expects($this->once()) - ->method('getRulesDefinition') - ->with(['entity']) - ->willReturn(new RulesDefinition(['data' => 'required'])); - - $event = new BeforeHandleStep(['data' => 'test'], ['entity'], new Step('key', $step)); - - $this->listener->handle($event); - } - - public function testHandleDataIsNorArray(): void - { - $this->validator->expects($this->never()) - ->method('validate'); - - $step = $this->createMock(ValidatableStepInterface::class); - - $step->expects($this->never()) - ->method('getRulesDefinition'); - - $event = new BeforeHandleStep('data', ['entity'], new Step('key', $step)); - - $this->listener->handle($event); - } - - public function testHandleStepIsNotValidatable(): void - { - $this->validator->expects($this->never()) - ->method('validate'); - - $step = $this->createMock(StepInterface::class); - - $event = new BeforeHandleStep(['data' => 'test'], ['entity'], new Step('key', $step)); - - $this->listener->handle($event); - } - - protected function setUp(): void - { - $this->validator = $this->createMock(ValidatorInterface::class); - - $this->listener = new BeforeHandleStepListener($this->validator); - - parent::setUp(); - } -} diff --git a/tests/Event/Listener/ValidatableStepInterface.php b/tests/Event/Listener/ValidatableStepInterface.php deleted file mode 100644 index e2e5c0e..0000000 --- a/tests/Event/Listener/ValidatableStepInterface.php +++ /dev/null @@ -1,12 +0,0 @@ -createMock(LaravelValidator::class); - - $validator->expects($this->once()) - ->method('fails') - ->willReturn(false); - - $this->validatorFactory->expects($this->once()) - ->method('make') - ->with(['data' => 'test'], ['data' => 'required'], [], []) - ->willReturn($validator); - - $this->validator->validate(['data' => 'test'], new RulesDefinition(['data' => 'required'])); - } - - public function testValidateWithException(): void - { - /** @phpstan-ignore-next-line */ - $this->expectExceptionObject(new ValidatorException(['data' => ['required message']])); - - $validator = $this->createMock(LaravelValidator::class); - - $validator->expects($this->once()) - ->method('fails') - ->willReturn(true); - - $validator->expects($this->once()) - ->method('errors') - ->willReturn(new MessageBag(['data' => 'required message'])); - - $this->validatorFactory->expects($this->once()) - ->method('make') - ->with(['data' => 'test'], ['data' => 'required'], ['data' => 'test message'], []) - ->willReturn($validator); - - $this->validator->validate( - ['data' => 'test'], - new RulesDefinition(['data' => 'required'], ['data' => 'test message']), - ); - } - - protected function setUp(): void - { - $this->validatorFactory = $this->createMock(ValidationFactory::class); - - $this->validator = new Validator($this->validatorFactory); - - parent::setUp(); - } -}