| 1 | package FormValidator::Nested::Profile::Param; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 18 | ▶ | use Any::Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 18 | ▶ | use Any::Moose 'X::AttributeHelpers'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 18 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 18 | ▶ | use FormValidator::Nested::Profile::Param::Validator; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 18 | ▶ | use FormValidator::Nested::Profile::Param::Filter; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 18 | ▶ | use FormValidator::Nested::Validator::Internal; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 18 | ▶ | use FormValidator::Nested::Result; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 18 | ▶ | use UNIVERSAL::require; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 18 | has 'profile' => ( | |||||||||||||||||||||||||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 15 | isa => 'FormValidator::Nested::Profile', | ||||||||||||||||||||||||||||||||||||||||||
| 16 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 17 | weak_ref => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 18 | has 'key' => ( | |||||||||||||||||||||||||||||||||||||||||
| 20 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 21 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 22 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 18 | has 'name' => ( | |||||||||||||||||||||||||||||||||||||||||
| 25 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 26 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 27 | default => '', | ||||||||||||||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 18 | has 'data' => ( | |||||||||||||||||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | isa => 'HashRef', | ||||||||||||||||||||||||||||||||||||||||||
| 32 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 18 | has 'array' => ( | |||||||||||||||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | isa => 'Bool', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 18 | has 'nested' => ( | |||||||||||||||||||||||||||||||||||||||||
| 40 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 41 | isa => 'FormValidator::Nested::Profile', | ||||||||||||||||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 18 | has 'validators' => ( | |||||||||||||||||||||||||||||||||||||||||
| 44 | metaclass => 'Collection::Array', | ||||||||||||||||||||||||||||||||||||||||||
| 45 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 46 | isa => 'ArrayRef[FormValidator::Nested::Profile::Param::Validator]', | ||||||||||||||||||||||||||||||||||||||||||
| 47 | lazy_build => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 48 | provides => { | ||||||||||||||||||||||||||||||||||||||||||
| 49 | elements => 'get_validators', | ||||||||||||||||||||||||||||||||||||||||||
| 50 | get => 'get_validator', | ||||||||||||||||||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 18 | has 'filters' => ( | |||||||||||||||||||||||||||||||||||||||||
| 54 | metaclass => 'Collection::Array', | ||||||||||||||||||||||||||||||||||||||||||
| 55 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 56 | isa => 'ArrayRef[FormValidator::Nested::Profile::Param::Filter]', | ||||||||||||||||||||||||||||||||||||||||||
| 57 | lazy_build => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 58 | provides => { | ||||||||||||||||||||||||||||||||||||||||||
| 59 | elements => 'get_filters', | ||||||||||||||||||||||||||||||||||||||||||
| 60 | get => 'get_filter', | ||||||||||||||||||||||||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 18 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 118 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 118 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | # processorの初期化 | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 118 | $self->validators; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 117 | $self->filters; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 118 | ▶ | sub _build_validators { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_validators: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 118 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 118 | my @internal_validators = (); | |||||||||||||||||||||||||||||||||||||||||
| 77 | 118 | ▶ | if ( $self->nested ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | # nestしてる場合はハッシュチェックも | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 4 | push @internal_validators, FormValidator::Nested::Profile::Param::Validator->new({ | |||||||||||||||||||||||||||||||||||||||||
| 80 | param => $self, | ||||||||||||||||||||||||||||||||||||||||||
| 81 | class => 'FormValidator::Nested::Validator::Internal', | ||||||||||||||||||||||||||||||||||||||||||
| 82 | method => 'nested_hash', | ||||||||||||||||||||||||||||||||||||||||||
| 83 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 118 | return [@internal_validators, $self->__build_processors('validators')]; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 117 | ▶ | sub _build_filters { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_filters: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 117 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 117 | return [$self->__build_processors('filters')]; | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 235 | ▶ | sub __build_processors { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__build_processors: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 235 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 235 | my $name = shift; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 235 | my ($name_single) = $name =~ m/^(.*)s$/; | |||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 235 | my $processor_class = 'FormValidator::Nested::Profile::Param::' . ucfirst($name_single); | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 235 | my @processors = (); | |||||||||||||||||||||||||||||||||||||||||
| 101 | 235 | foreach my $processor ( @{$self->data->{$name}} ) { | |||||||||||||||||||||||||||||||||||||||||
| 102 | 192 | my $class_method = $processor; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 192 | my $options = {}; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 192 | ▶ | if ( ref $processor ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 98 | ($class_method) = keys %{$processor}; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 98 | ($options) = values %{$processor}; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 192 | my ( $class, $method ) = split /#/, $class_method; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 192 | ▶ | if ( $name eq 'validators' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 162 | ▶ | if ( $class =~ /^\+(.*)/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 2 | $class = $1; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 160 | $class = 'FormValidator::Nested::Validator::' . $class; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | elsif ( $name eq 'filters' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 30 | ▶ | if ( $class =~ /^\+(.*)/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | $class = $1; | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 30 | $class = 'FormValidator::Nested::Filter::' . $class; | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 192 | ▶ | if ( !$class->require ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | die("no require $class"); | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 191 | push @processors, $processor_class->new({ | |||||||||||||||||||||||||||||||||||||||||
| 130 | param => $self, | ||||||||||||||||||||||||||||||||||||||||||
| 131 | class => $class, | ||||||||||||||||||||||||||||||||||||||||||
| 132 | method => $method, | ||||||||||||||||||||||||||||||||||||||||||
| 133 | options => $options, | ||||||||||||||||||||||||||||||||||||||||||
| 134 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 234 | return @processors; | |||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 551 | ▶ | sub validate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate: called CC=9 SCAR=22.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 551 | my ( $self, $req, $parent_names ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 551 | my $values_ref = $self->get_values($req); | |||||||||||||||||||||||||||||||||||||||||
| 143 | 551 | my $param_result = FormValidator::Nested::Result->new; | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 551 | foreach my $validator ( $self->get_validators ) { | |||||||||||||||||||||||||||||||||||||||||
| 146 | 650 | $param_result->merge($validator->process($req, $values_ref, $self->key, $parent_names)); | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 551 | ▶ | if ( $self->nested && !$param_result->has_error && $values_ref ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$self->nested and not $param_result->has_error and $values_ref
$self->nested and not $param_result->has_error
MC/DC66%2/3
$self->nested and not $param_result->has_error and $values_ref
$self->nested
not $param_result->has_error
$values_ref
Truth table75%3/4
A and not(B) and C
A= $self->nestedB= not $param_result->has_errorC= $values_ref
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 15 | ▶ | if ( $self->array ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 7 | for my $count ( 0 .. $#{$values_ref} ) { | |||||||||||||||||||||||||||||||||||||||||
| 152 | $param_result->merge( | ||||||||||||||||||||||||||||||||||||||||||
| 153 | $self->nested->validate( | ||||||||||||||||||||||||||||||||||||||||||
| 154 | $values_ref->[$count], | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | $parent_names ? [@{$parent_names}, $self->key, $count] : [$self->key, $count] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 157 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 161 | $param_result->merge( | ||||||||||||||||||||||||||||||||||||||||||
| 162 | $self->nested->validate( | ||||||||||||||||||||||||||||||||||||||||||
| 163 | $values_ref, | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | ▶ | $parent_names ? [@{$parent_names}, $self->key] : [$self->key] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 166 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 551 | return $param_result; | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 683 | ▶ | sub get_values { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_values: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 683 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 683 | my $req = shift; | |||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 683 | ▶ | if ( $self->array ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 19 | return [$req->param($self->key)]; | |||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 664 | my @values = $req->param($self->key); | |||||||||||||||||||||||||||||||||||||||||
| 181 | 664 | ▶ | if ( @values > 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | # なんかエラーだす必要あるかも | ||||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | 664 | return $values[0]; | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | 18 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||