| 1 | package Dancer2::Plugin::FormValidator::Processor; | ||||||||||
| 2 | |||||||||||
| 3 | 15 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 15 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 15 | ▶ | use List::Util 1.45 qw(uniqstr); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 15 | ▶ | use Dancer2::Plugin::FormValidator::Validator; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 15 | ▶ | use Dancer2::Plugin::FormValidator::Factory::Messages; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 15 | ▶ | use Dancer2::Plugin::FormValidator::Result; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 15 | ▶ | use Types::Standard qw(InstanceOf ConsumerOf); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 15 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 15 | has input => ( | |||||||||
| 15 | is => 'ro', | ||||||||||
| 16 | isa => InstanceOf['Dancer2::Plugin::FormValidator::Input'], | ||||||||||
| 17 | required => 1, | ||||||||||
| 18 | ); | ||||||||||
| 19 | |||||||||||
| 20 | 15 | has profile => ( | |||||||||
| 21 | is => 'ro', | ||||||||||
| 22 | isa => ConsumerOf['Dancer2::Plugin::FormValidator::Role::Profile'], | ||||||||||
| 23 | required => 1, | ||||||||||
| 24 | ); | ||||||||||
| 25 | |||||||||||
| 26 | 15 | has config => ( | |||||||||
| 27 | is => 'ro', | ||||||||||
| 28 | isa => InstanceOf['Dancer2::Plugin::FormValidator::Config'], | ||||||||||
| 29 | required => 1, | ||||||||||
| 30 | ); | ||||||||||
| 31 | |||||||||||
| 32 | 15 | has registry => ( | |||||||||
| 33 | is => 'ro', | ||||||||||
| 34 | isa => InstanceOf['Dancer2::Plugin::FormValidator::Registry'], | ||||||||||
| 35 | required => 1, | ||||||||||
| 36 | ); | ||||||||||
| 37 | |||||||||||
| 38 | has validator => ( | ||||||||||
| 39 | is => 'ro', | ||||||||||
| 40 | isa => InstanceOf ['Dancer2::Plugin::FormValidator::Validator'], | ||||||||||
| 41 | lazy => 1, | ||||||||||
| 42 | builder => sub { | ||||||||||
| 43 | 4 | ▶ | return Dancer2::Plugin::FormValidator::Validator->new( | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
_build_validator: called CC=1 SCAR=0.0
| |||||||||||
| 44 | config => $_[0]->config, | ||||||||||
| 45 | registry => $_[0]->registry, | ||||||||||
| 46 | ); | ||||||||||
| 47 | } | ||||||||||
| 48 | 15 | ); | |||||||||
| 49 | |||||||||||
| 50 | has messages_factory => ( | ||||||||||
| 51 | is => 'ro', | ||||||||||
| 52 | isa => InstanceOf ['Dancer2::Plugin::FormValidator::Factory::Messages'], | ||||||||||
| 53 | lazy => 1, | ||||||||||
| 54 | builder => sub { | ||||||||||
| 55 | 3 | ▶ | return Dancer2::Plugin::FormValidator::Factory::Messages->new( | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
_build_messages_factory: called CC=1 SCAR=0.0
| |||||||||||
| 56 | config => $_[0]->config, | ||||||||||
| 57 | registry => $_[0]->registry, | ||||||||||
| 58 | ); | ||||||||||
| 59 | } | ||||||||||
| 60 | 15 | ); | |||||||||
| 61 | |||||||||||
| 62 | sub run { | ||||||||||
| 63 | 17 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
run: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 64 | |||||||||||
| 65 | 17 | my $profile = $self->profile->profile; | |||||||||
| 66 | 17 | my $input = $self->input->get; | |||||||||
| 67 | 17 | my $messages = {}; | |||||||||
| 68 | |||||||||||
| 69 | # Run hook before. | ||||||||||
| 70 | 17 | $profile = $self->profile->hook_before($profile, $input); | |||||||||
| 71 | |||||||||||
| 72 | # Now run validation. | ||||||||||
| 73 | 17 | my ($success, $valid, $invalid) = $self->validator->validate($profile, $input); | |||||||||
| 74 | |||||||||||
| 75 | # If no success, build messages. | ||||||||||
| 76 | 17 | ▶ | if ($success != 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 13 | $messages = $self->_build_messages($invalid); | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | # Flatten $invalid array ref and leave only unique fields. | ||||||||||
| 81 | 17 | my @invalid_fields = uniqstr map { $_->[0] } @{ $invalid }; | |||||||||
| 82 | |||||||||||
| 83 | # Collect valid values from input. | ||||||||||
| 84 | 17 | my $valid_input = $self->_collect_valid($valid); | |||||||||
| 85 | |||||||||||
| 86 | 17 | return Dancer2::Plugin::FormValidator::Result->new( | |||||||||
| 87 | success => $success, | ||||||||||
| 88 | valid => $valid_input, | ||||||||||
| 89 | invalid => \@invalid_fields, | ||||||||||
| 90 | messages => $messages, | ||||||||||
| 91 | ); | ||||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | sub _build_messages { | ||||||||||
| 95 | 13 | ▶ | my ($self, $invalid) = @_; | ||||||||
|
Subroutine
_build_messages: called CC=2 SCAR=6.9
| |||||||||||
| 96 | |||||||||||
| 97 | 13 | ▶ | if ($self->profile->does('Dancer2::Plugin::FormValidator::Role::HasMessages')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 2 | return $self->messages_factory->build($invalid, $self->profile->messages); | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 11 | return $self->messages_factory->build($invalid); | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | sub _collect_valid { | ||||||||||
| 105 | 17 | ▶ | my ($self, $valid) = @_; | ||||||||
|
Subroutine
_collect_valid: called CC=3 SCAR=11.0
| |||||||||||
| 106 | |||||||||||
| 107 | 17 | my $input = $self->input->get; | |||||||||
| 108 | |||||||||||
| 109 | 17 | my %valid_input; | |||||||||
| 110 | 17 | for my $field (@ { $valid }) { | |||||||||
| 111 | 15 | ▶ | if (exists $input->{$field}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | 15 | $valid_input{$field} = $input->{$field}; | |||||||||
| 113 | } | ||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 17 | return \%valid_input; | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | 15 | 1; | |||||||||