Profile/TypeConstraints.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Profile/Video.pm ↑ Summary Profile.pm »
1 package Net::PMP::Profile::TypeConstraints;
2 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 our $VERSION = '0.102';
6  
7 # The Net::PMP::Type::* prefix is used for all our type constraints
8 # to avoid stepping on anyone's toes
9  
10 # locales
11 0 ▶use Locale::Language;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 my %all_langs = map { $_ => $_ } all_language_codes();
13 subtype 'Net::PMP::Type::ISO6391' => as 'Str' =>
14 where { length($_) == 2 and exists $all_langs{$_} } =>
15 message {"The provided hreflang ($_) is not a valid ISO639-1 value."};
16  
17 # datetimes
18 use DateTime::Format::Strptime;
19 use DateTime::Format::DateParse;
20 my $coerce_datetime = sub {
21 my $thing = shift;
22 my $iso8601_formatter
23 = DateTime::Format::Strptime->new( pattern => '%FT%T.%3NZ' );
24 if ( blessed $thing) {
25 if ( $thing->isa('DateTime') ) {
26  
27 # enforce UTC
28 $thing->set_time_zone('UTC');
29 $thing->set_formatter($iso8601_formatter);
30 return $thing;
31 }
32 confess "$thing is not a DateTime object";
33 }
34 else {
35 my $dt = DateTime::Format::DateParse->parse_datetime($thing);
36 if ( !$dt ) {
37 confess "Invalid date format: $thing";
38 }
39  
40 # enforce UTC
41 $dt->set_time_zone('UTC');
42 $dt->set_formatter($iso8601_formatter);
43 return $dt;
44 }
45 };
46 subtype 'Net::PMP::Type::DateTimeOrStr' => as class_type('DateTime');
47 coerce 'Net::PMP::Type::DateTimeOrStr' => from 'Object' =>
48 via { $coerce_datetime->($_) } => from 'Str' =>
49 via { $coerce_datetime->($_) };
50 subtype 'Net::PMP::Type::ValidDates' => as
51 'HashRef[Net::PMP::Type::DateTimeOrStr]';
52 coerce 'Net::PMP::Type::ValidDates' => from 'HashRef' => via {
53 if ( !exists $_->{to} or !exists $_->{from} ) {
54 confess "ValidDates must contain 'to' and 'from' keys";
55 }
56 $_->{to} = $coerce_datetime->( $_->{to} );
57 $_->{from} = $coerce_datetime->( $_->{from} );
58 $_;
59 };
60  
61 # Content types
62 use Media::Type::Simple qw(is_type);
63  
64 #confess "MediaType defined!";
65 subtype 'Net::PMP::Type::MediaType' => as 'Str' => where {
66 is_type($_);
67 } => message {
68 "The value ($_) does not appear to be a valid media type.";
69 };
70  
71 # MediaEnclosure
72 my $coerce_enclosure = sub {
73  
74 # defer till runtime to avoid circular dependency
75 require Net::PMP::Profile::MediaEnclosure;
76  
77 if ( ref( $_[0] ) eq 'HASH' ) {
78 return Net::PMP::Profile::MediaEnclosure->new( $_[0] );
79 }
80 else { return $_[0]; }
81 };
82 subtype 'Net::PMP::Type::MediaEnclosure' =>
83 as class_type('Net::PMP::Profile::MediaEnclosure');
84 coerce 'Net::PMP::Type::MediaEnclosure' => from 'Any' =>
85 via { $coerce_enclosure->($_) };
86 subtype 'Net::PMP::Type::MediaEnclosures' => as
87 'ArrayRef[Net::PMP::Type::MediaEnclosure]';
88 coerce 'Net::PMP::Type::MediaEnclosures' => from 'ArrayRef[HashRef]' => via {
89 [ map { $coerce_enclosure->($_) } @$_ ];
90 } => from 'HashRef' => via { [ $coerce_enclosure->($_) ] };
91  
92 no Moose::Util::TypeConstraints;
93  
94 __PACKAGE__->meta->make_immutable();
95  
96 1;
97  
98 __END__
« Profile/Video.pm ↑ Summary Profile.pm »