| 1 | package Dancer2::Plugin::FormValidator::Registry; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 16 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 16 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 16 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 16 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 16 | ▶ | use Module::Load qw(autoload); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 16 | ▶ | use Types::Standard qw(ConsumerOf ArrayRef HashRef); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 16 | ▶ | use namespace::clean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 16 | has extensions => ( | |||||||||||||
| 13 | is => 'ro', | ||||||||||||||
| 14 | isa => ArrayRef[ConsumerOf['Dancer2::Plugin::FormValidator::Role::Extension']], | ||||||||||||||
| 15 | predicate => 1, | ||||||||||||||
| 16 | ); | ||||||||||||||
| 17 | |||||||||||||||
| 18 | has validators => ( | ||||||||||||||
| 19 | is => 'ro', | ||||||||||||||
| 20 | isa => HashRef, | ||||||||||||||
| 21 | lazy => 1, | ||||||||||||||
| 22 | builder => sub { | ||||||||||||||
| 23 | 6 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
Subroutine
_build_validators: called CC=5 SCAR=16.1
| |||||||||||||||
| 24 | |||||||||||||||
| 25 | 6 | my %plugin_validators; | |||||||||||||
| 26 | |||||||||||||||
| 27 | 6 | for my $validator (keys % { $self->_validators }) { | |||||||||||||
| 28 | $plugin_validators{$validator} = { | ||||||||||||||
| 29 | extension => undef, | ||||||||||||||
| 30 | 96 | validator => $self->_validators->{$validator}, | |||||||||||||
| 31 | }; | ||||||||||||||
| 32 | } | ||||||||||||||
| 33 | |||||||||||||||
| 34 | 6 | ▶ | if ($self->has_extensions) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 35 | 4 | for my $extension (@{ $self->extensions }) { | |||||||||||||
| 36 | 1 | my %extension_validators; | |||||||||||||
| 37 | |||||||||||||||
| 38 | 1 | for my $validator (keys %{ $extension->validators } ) { | |||||||||||||
| 39 | $extension_validators{$validator} = { | ||||||||||||||
| 40 | extension => $extension, | ||||||||||||||
| 41 | 1 | validator => $extension->validators->{$validator}, | |||||||||||||
| 42 | }; | ||||||||||||||
| 43 | } | ||||||||||||||
| 44 | |||||||||||||||
| 45 | 1 | %plugin_validators = (%plugin_validators, %extension_validators); | |||||||||||||
| 46 | }; | ||||||||||||||
| 47 | } | ||||||||||||||
| 48 | |||||||||||||||
| 49 | 6 | return \%plugin_validators; | |||||||||||||
| 50 | } | ||||||||||||||
| 51 | 16 | ); | |||||||||||||
| 52 | |||||||||||||||
| 53 | sub get { | ||||||||||||||
| 54 | 88 | ▶ | my ($self, $name) = @_; | ||||||||||||
|
Subroutine
get: called CC=4 SCAR=14.4
Pod
undocumented
| |||||||||||||||
| 55 | |||||||||||||||
| 56 | 88 | ▶ | if (my $validator_struct = $self->validators->{$name}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | 88 | my $extension = $validator_struct->{extension}; | |||||||||||||
| 58 | 88 | my $class = $validator_struct->{validator}; | |||||||||||||
| 59 | |||||||||||||||
| 60 | 88 | autoload $class; | |||||||||||||
| 61 | |||||||||||||||
| 62 | 88 | my $role = 'Dancer2::Plugin::FormValidator::Role::Validator'; | |||||||||||||
| 63 | |||||||||||||||
| 64 | 88 | ▶ | my $validator = $extension | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 65 | ? $class->new(extension => $extension) | ||||||||||||||
| 66 | : $class->new; | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 88 | ▶ | if (not $validator->does($role)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | 0 | Carp::croak("Validator: $class should implement $role\n"); | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | |||||||||||||||
| 72 | 88 | return $validator; | |||||||||||||
| 73 | } | ||||||||||||||
| 74 | |||||||||||||||
| 75 | 0 | Carp::croak("$name is not defined\n"); | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | |||||||||||||||
| 78 | # Validators map. | ||||||||||||||
| 79 | sub _validators { | ||||||||||||||
| 80 | return { | ||||||||||||||
| 81 | 272 | ▶ | accepted => 'Dancer2::Plugin::FormValidator::Validator::Accepted', | ||||||||||||
|
Subroutine
_validators: called CC=1 SCAR=0.0
| |||||||||||||||
| 82 | alpha => 'Dancer2::Plugin::FormValidator::Validator::Alpha', | ||||||||||||||
| 83 | alpha_num => 'Dancer2::Plugin::FormValidator::Validator::AlphaNum', | ||||||||||||||
| 84 | boolean => 'Dancer2::Plugin::FormValidator::Validator::Boolean', | ||||||||||||||
| 85 | enum => 'Dancer2::Plugin::FormValidator::Validator::Enum', | ||||||||||||||
| 86 | email => 'Dancer2::Plugin::FormValidator::Validator::Email', | ||||||||||||||
| 87 | email_dns => 'Dancer2::Plugin::FormValidator::Validator::EmailDns', | ||||||||||||||
| 88 | integer => 'Dancer2::Plugin::FormValidator::Validator::Integer', | ||||||||||||||
| 89 | length_max => 'Dancer2::Plugin::FormValidator::Validator::LengthMax', | ||||||||||||||
| 90 | length_min => 'Dancer2::Plugin::FormValidator::Validator::LengthMin', | ||||||||||||||
| 91 | max => 'Dancer2::Plugin::FormValidator::Validator::Max', | ||||||||||||||
| 92 | min => 'Dancer2::Plugin::FormValidator::Validator::Min', | ||||||||||||||
| 93 | numeric => 'Dancer2::Plugin::FormValidator::Validator::Numeric', | ||||||||||||||
| 94 | required => 'Dancer2::Plugin::FormValidator::Validator::Required', | ||||||||||||||
| 95 | required_with => 'Dancer2::Plugin::FormValidator::Validator::RequiredWith', | ||||||||||||||
| 96 | same => 'Dancer2::Plugin::FormValidator::Validator::Same', | ||||||||||||||
| 97 | }; | ||||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | 16 | 1; | |||||||||||||