PRANG/XMLSchema/Types.pm

filter: Statementstmt 56.4% 44 / 78 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 9.6% 3 / 31 Podpod n/a totaltotal 33.5% 47 / 140 CC 15 SCAR 18.9
CC
15
Cov
41%
__ANON__
30.0
__ANON__
6.0
__ANON__
6.0
CRAP 6.6
« PRANG/Util.pm ↑ Summary PRANG/Cookbook/Role/Time.pm »
1  
2 package PRANG::XMLSchema::Types;
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 subtype "PRANG::XMLSchema::normalizedString"
8 => as "Str"
9 0 ▶ => where { !m{[\n\r\t]} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
10  
11 subtype "PRANG::XMLSchema::token"
12 => as "Str"
13 => where {
14 0 ▶ !m{[\t\r\n]|^\s|\s$|\s\s};
Subroutine __ANON__: not called CC=1 SCAR=6.9
15 1 };
16  
17 # automatically trim tokens if passed them.
18 coerce "PRANG::XMLSchema::token"
19 => from "Str",
20 => via {
21 0 ▶ my ($x) = m/\A\s*(.*?)\s*\Z/;
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 0 $x =~ s{\s+}{ }g;
23 0 $x;
24 },
25 1 ;
26  
27 # See https://rt.cpan.org/Ticket/Display.html?id=52309
28 # use Regexp::Common qw/URI/;
29 subtype "PRANG::XMLSchema::anyURI"
30 => as "Str"
31 => where {
32 0 ▶ m{^\w+:\S+$}; # validate using this instead
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 1 };
34  
35 1 ▶use I18N::LangTags qw(is_language_tag);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 subtype "PRANG::XMLSchema::language"
37 => as "Str"
38 => where {
39 0 ▶ is_language_tag($_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 1 };
41  
42 subtype "PRANG::XMLSchema::dateTime"
43 => as "Str"
44 => where {
45 # from the XMLSchema spec... it'll do for now ;)
46 # how on earth is one supposed to encode Pacific/Guam
47 # or Pacific/Saipan dates before 1845 with this regex?
48 0 ▶ m{
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 ^
50 -?([1-9][0-9]{3,}|0[0-9]{3})
51 -(0[1-9]|1[0-2])
52 -(0[1-9]|[12][0-9]|3[01])
53 T(([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?|(24:00:00(\.0+)?))
54 (?:Z|(?:\+|-)(?:(?:0[0-9]|1[0-3]):[0-5][0-9]|14:00))?
55 $
56 }x;
57 1 };
58  
59 subtype "PRANG::XMLSchema::time"
60 => as "Str"
61 => where {
62 # from the XMLSchema spec... it'll do for now ;)
63 0 ▶ m{
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 ^
65 (([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?|(24:00:00(\.0+)?))
66 (?:Z|(?:\+|-)(?:(?:0[0-9]|1[0-3]):[0-5][0-9]|14:00))?
67 $
68 }x;
69 1 };
70  
71 subtype "PRANG::XMLSchema::date"
72 => as "Str"
73 => where {
74 # from the XMLSchema spec... it'll do for now ;)
75 # XXX: Note, since the XML Spec bizarrely has Timezones on Dates,
76 # we have chosen to ignore it (since it is optional anyway)
77 0 ▶ m{
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 ^
79 -?([1-9][0-9]{3,}|0[0-9]{3})
80 -(0[1-9]|1[0-2])
81 -(0[1-9]|[12][0-9]|3[01])
82 $
83 }x;
84 1 };
85  
86 subtype "PRANG::XMLSchema::duration"
87 => as "Str"
88 => where {
89 0 ▶ m{^\s* (?: [pP]? \s* )?
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 (?: C \s* \d+)?
91 (?: Y \s* \d+)?
92 (?: M \s* \d+)?
93 (?: D \s* \d+)?
94 (?: h \s* \d+)?
95 (?: m \s* \d+)?
96 (?: s \s* \d+(?:\.\d+) )? \s* $}x;
97 1 };
98  
99 # other built-in primitive datatypes.
100 1 subtype "PRANG::XMLSchema::string"
101 => as "Str";
102 subtype "PRANG::XMLSchema::boolean"
103 => as "Str"
104 => where {
105 0 ▶ m/^(?:0|1|true|false)$/;
Subroutine __ANON__: not called CC=1 SCAR=6.9
106 1 };
107  
108 coerce "Bool"
109 => from 'PRANG::XMLSchema::boolean'
110 0 ▶ => via { m{1|true} ? 1 : 0 };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
/1|true/ ? : 0 0 0
111  
112 coerce "PRANG::XMLSchema::boolean"
113 => from 'Bool'
114 0 ▶ => via { (!!$_) ? "true" : "false" };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
!!$_ ? : 0 0 0
115  
116 1 subtype "PRANG::XMLSchema::decimal"
117 => as "Num";
118  
119 # floating point stuff...
120 subtype "PRANG::XMLSchema::float"
121 => as "Str"
122 => where {
123 0 ▶ m{^(?:[\-+]?(?:\d+(?:\.\d*)?(?:e[\-+]?(\d+))?|inf)|NaN)$}i;
Subroutine __ANON__: not called CC=1 SCAR=6.9
124 1 };
125 1 our $inf = exp(~0 >> 1);
126 1 our $nan = $inf / $inf;
127 1 our $neg_inf = -$inf;
128 coerce "Num"
129 => from 'PRANG::XMLSchema::float'
130 => via {
131 0 ▶ m{^(?:([\-+])?inf|(nan)|(.))};
Subroutine __ANON__: not called CC=5 SCAR=34.0
132 0 ▶ return eval $_ if defined $3;
Branch: 1 branch
BranchTrueFalseTotal
if defined $3 0 0 0
133 0 ▶ return $nan if $2;
Branch: 1 branch
BranchTrueFalseTotal
if $2 0 0 0
134 0 ▶ return $neg_inf if $1 and $1 eq "-";
Branch: 1 branch
BranchTrueFalseTotal
if $1 and $1 eq "-" 0 0 0
Condition0%0/3
$1 and $1 eq "-"
!ll&&!rl&&r
000
MC/DC0%0/2
$1 and $1 eq "-"
$1 $1 eq "-"
Truth table0%0/3
A and B
A= $1B= $1 eq "-"
ABresult
0X0
100
111
135 0 return $inf;
136 1 };
137  
138 1 if ( 0.1 == 0.100000000000000006 ) {
139 1 subtype "PRANG::XMLSchema::double"
140 => as "PRANG::XMLSchema::float";
141 }
142 else {
143 subtype "PRANG::XMLSchema::double"
144 => as "PRANG::XMLSchema::float"
145 => where {
146 0 ▶ unpack('d',pack('d',$_))==$_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 };
148  
149 coerce "PRANG::XMLSchema::double"
150 => from "PRANG::XMLSchema::float"
151 => via {
152 0 ▶ unpack('d',pack('d',$_));
Subroutine __ANON__: not called CC=1 SCAR=6.9
153 };
154 }
155  
156 # built-in derived types.
157 # this sub-typing might seem unnecessarily deep, but that's what the
158 # spec says... see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/datatypes.html#built-in-derived
159 1 subtype "PRANG::XMLSchema::integer"
160 => as "Int";
161 subtype "PRANG::XMLSchema::nonPositiveInteger"
162 => as "PRANG::XMLSchema::integer"
163 => where {
164 0 ▶ $_ <= 0;
Subroutine __ANON__: not called CC=1 SCAR=6.9
165 1 };
166 subtype "PRANG::XMLSchema::negativeInteger"
167 => as "PRANG::XMLSchema::nonPositiveInteger"
168 => where {
169 0 ▶ $_ <= -1;
Subroutine __ANON__: not called CC=1 SCAR=6.9
170 1 };
171 subtype "PRANG::XMLSchema::nonNegativeInteger"
172 => as "PRANG::XMLSchema::integer"
173 => where {
174 0 ▶ $_ >= 0;
Subroutine __ANON__: not called CC=1 SCAR=6.9
175 1 };
176 subtype "PRANG::XMLSchema::positiveInteger"
177 => as "PRANG::XMLSchema::nonNegativeInteger"
178 => where {
179 0 ▶ $_ >= 1;
Subroutine __ANON__: not called CC=1 SCAR=6.9
180 1 };
181 subtype "PRANG::XMLSchema::long"
182 => as "PRANG::XMLSchema::integer"
183 => where {
184 0 ▶ $_ >= -9223372036854775808 and $_ <= 9223372036854775807;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_ >= '-9223372036854775808' and $_ <= '9223372036854775807' 0 0 0
185 1 };
186 subtype "PRANG::XMLSchema::int"
187 => as "PRANG::XMLSchema::long"
188 => where {
189 0 ▶ $_ >= -2147483648 and $_ <= 2147483647;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_ >= '-2147483648' and $_ <= 2147483647 0 0 0
190 1 };
191 subtype "PRANG::XMLSchema::short"
192 => as "PRANG::XMLSchema::int"
193 => where {
194 0 ▶ $_ >= -32768 and $_ <= 32767;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_ >= -32768 and $_ <= 32767 0 0 0
195 1 };
196 subtype "PRANG::XMLSchema::byte"
197 => as "PRANG::XMLSchema::short"
198 => where {
199 0 ▶ $_ >= -128 and $_ <= 127;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_ >= -128 and $_ <= 127 0 0 0
200 1 };
201 subtype "PRANG::XMLSchema::unsignedLong"
202 => as "PRANG::XMLSchema::nonNegativeInteger"
203 => where {
204 0 ▶ $_ >= 0 and $_ < 18446744073709551615;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_ >= 0 and $_ < '18446744073709551615' 0 0 0
205 1 };
206 subtype "PRANG::XMLSchema::unsignedInt"
207 => as "PRANG::XMLSchema::unsignedLong"
208 => where {
209 0 ▶ $_ >= 0 and $_ < 2147483647;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_ >= 0 and $_ < 2147483647 0 0 0
210 1 };
211 subtype "PRANG::XMLSchema::unsignedShort"
212 => as "PRANG::XMLSchema::unsignedInt"
213 => where {
214 0 ▶ $_ >= 0 and $_ < 65536;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_ >= 0 and $_ < 65536 0 0 0
215 1 };
216 subtype "PRANG::XMLSchema::unsignedByte"
217 => as "PRANG::XMLSchema::unsignedShort"
218 => where {
219 0 ▶ $_ >= 0 and $_ < 256;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_ >= 0 and $_ < 256 0 0 0
220 1 };
221  
222  
223 1 1;
224  
225 =head1 NAME
226  
227 PRANG::XMLSchema::Types - type registry for XML Schema-related types
228  
229 =head1 SYNOPSIS
230  
231 package My::Class;
232 use Moose;
233 use PRANG::Graph;
234 use PRANG::XMLSchema::Types;
235  
236 has_attr 'foo' =>
237 is => "ro",
238 isa => "PRANG::XMLSchema::unsignedShort",
239 ;
240  
241 =head1 DESCRIPTION
242  
243 This module is a collection of types which make working with XML
244 Schema specifications easier. See the source for the complete list.
245  
246 These might be moved into a separate namespace, but if you include
247 this module you will get aliases for wherever these XML Schema types
248 end up.
249  
250 =head1 SEE ALSO
251  
252 L<PRANG>, L<PRANG::Graph::Meta::Attr>, L<PRANG::Graph::Meta::Element>
253  
254 =head1 AUTHOR AND LICENCE
255  
256 Development commissioned by NZ Registry Services, and carried out by
257 Catalyst IT - L<http://www.catalyst.net.nz/>
258  
259 Copyright 2009, 2010, NZ Registry Services. This module is licensed
260 under the Artistic License v2.0, which permits relicensing under other
261 Free Software licenses.
262  
263 =cut
264  
« PRANG/Util.pm ↑ Summary PRANG/Cookbook/Role/Time.pm »