blib/lib/XML/Loy/GeoRSS.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 49 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 198 CC 18 SCAR 27.2
CC
18
Cov
0%
geo_polygon
30.0
geo_point
20.0
geo_line
20.0
CRAP 15.2
1 package XML::Loy::GeoRSS;
2 use strict;
3 use warnings;
4  
5 use XML::Loy with => (
6 prefix => 'georss',
7 namespace => 'http://www.georss.org/georss'
8 );
9  
10 use Carp qw/carp/;
11 use Scalar::Util qw/looks_like_number/;
12  
13 # No constructor
14 sub new {
15 carp 'Only use ' . __PACKAGE__ . ' as an extension';
16 return;
17 };
18  
19  
20 # Add 'point' element
21 sub geo_point {
22 my $self = shift;
23  
24 # Get
25 if (@_ <= 1) {
26 # Get point object
27 my $point = $self->find('point') or return;
28 $point = $point->[ shift // 0 ] or return;
29  
30 # Wrong namespace
31 return if $point->namespace ne __PACKAGE__->_namespace;
32  
33 # Return point
34 return [ split /\s+/, $point->text ];
35 }
36  
37 # Set
38 elsif (@_ == 2 && looks_like_number($_[0]) && looks_like_number($_[1])) {
39 return $self->add(point => $_[0] . ' ' . $_[1]);
40 }
41  
42 # Parameterlist has wrong length
43 return;
44 };
45  
46  
47 # Add 'line' element
48 sub geo_line {
49 my $self = shift;
50  
51 # Get
52 if (@_ <= 1) {
53 my $line = $self->find('line') or return;
54 $line = $line->[ shift // 0 ] or return;
55  
56 # Wrong namespace
57 return if $line->namespace ne __PACKAGE__->_namespace;
58  
59 # Return line
60 my @points;
61 my @v = split /\s+/, $line->text;
62 push @points, [ shift(@v), shift(@v) // 0 ] while @v;
63 return \@points;
64 };
65  
66 # Parameterlist not even or too small
67 return if @_ % 2 || @_ < 4;
68  
69 # Set
70 return $self->add(line => join(' ',@_) );
71 };
72  
73  
74 # Add 'polygon' element
75 sub geo_polygon {
76 my $self = shift;
77  
78 # Get
79 if (@_ <= 1) {
80 my $poly = $self->find('polygon') or return;
81 $poly = $poly->[ shift // 0 ] or return;
82  
83 # Wrong namespace
84 return if $poly->namespace ne __PACKAGE__->_namespace;
85  
86 # Return polygon
87 my @points;
88 my @v = split /\s+/, $poly->text;
89 push @points, [ shift(@v), shift(@v) // 0 ] while @v;
90 return \@points;
91 };
92  
93 # Parameterlist not even or too small
94 return if @_ % 2 || @_ < 6;
95  
96 # Last pair is not identical to first pair
97 if ($_[0] != $_[$#_ - 1] && $_[1] != $_[$#_]) {
98 push(@_, @_[0..1]);
99 };
100  
101 # Add polygon
102 return $self->add(polygon => join(' ', @_));
103 };
104  
105  
106 # Add properties
107 sub geo_property {
108 my $self = shift;
109  
110 my %properties = @_;
111  
112 # Add all available properties
113 foreach my $tag (grep(/^(?:(?:relationship|featuretype)tag|featurename)$/i,
114 keys %properties)) {
115  
116 my $val = $properties{$tag};
117  
118 # Add as an array, if it is one
119 foreach (ref $val ? @$val : ($val)) {
120 $self->add( lc($tag) => $_ );
121 };
122 };
123  
124 return $self;
125 };
126  
127  
128 # Add 'floor' element
129 sub geo_floor {
130 shift->add(floor => shift);
131 };
132  
133  
134 # Add 'elev' element
135 sub geo_elev {
136 shift->add(elev => shift);
137 };
138  
139  
140 # Add 'radius' element
141 sub geo_radius {
142 shift->add(radius => shift);
143 };
144  
145  
146 # Add 'where' element
147 sub geo_where {
148 shift->add('where');
149 };
150  
151  
152 # Add 'box' element
153 sub geo_box {
154 my $self = shift;
155  
156 if (@_ <= 1) {
157 my $box = $self->find('box') or return;
158 $box = $box->[ shift // 0 ] or return;
159  
160 # Wrong namespace
161 return if $box->namespace ne __PACKAGE__->_namespace;
162  
163 # Return box
164 my @points;
165 my @v = split /\s+/, $box->text;
166 return [
167 [$v[0], $v[1]],
168 [$v[2], $v[3]]
169 ];
170 };
171  
172 # Parameterlist has wrong length
173 return unless @_ == 4;
174  
175 return $self->add(box => join(' ',@_));
176 };
177  
178  
179 # Add 'circle' element
180 sub geo_circle {
181 my $self = shift;
182  
183 if (@_ <= 1) {
184 my $circle = $self->find('circle') or return;
185 $circle = $circle->[ shift // 0 ] or return;
186  
187 # Wrong namespace
188 return if $circle->namespace ne __PACKAGE__->_namespace;
189  
190 # Return point
191 my @v = split /\s+/, $circle->text;
192 return [ [ $v[0], $v[1] ], $v[2] ];
193 };
194  
195 # Parameterlist has wrong length
196 return unless @_ == 3;
197  
198 return $self->add(circle => join(' ',@_));
199 };
200  
201  
202 1;
203  
204  
205 __END__