File Coverage

blib/lib/Map/Tube/Plugin/Formatter/Utils.pm
Criterion Covered Total %
statement 12 14 85.7
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 17 19 89.4


line stmt bran cond sub pod time code
1             package Map::Tube::Plugin::Formatter::Utils;
2              
3             $Map::Tube::Plugin::Formatter::Utils::VERSION = '0.14';
4             $Map::Tube::Plugin::Formatter::Utils::AUTHORITY = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             Map::Tube::Plugin::Formatter::Utils - Helper package for Map::Tube::Plugin::Formatter.
9              
10             =head1 VERSION
11              
12             Version 0.14
13              
14             =cut
15              
16 1     1   13 use 5.006;
  1         3  
17 1     1   4 use strict; use warnings;
  1     1   1  
  1         16  
  1         4  
  1         2  
  1         20  
18 1     1   311 use Data::Dumper;
  1         4550  
  1         48  
19 1     1   1447 use XML::Twig;
  0            
  0            
20             use Map::Tube::Exception::MissingSupportedObject;
21             use Map::Tube::Exception::InvalidSupportedObject;
22             use Map::Tube::Exception::FoundUnsupportedObject;
23             use parent 'Exporter';
24              
25             our @EXPORT_OK = qw(xml get_data validate_object);
26              
27             =head1 DESCRIPTION
28              
29             B
30              
31             =cut
32              
33             sub xml {
34             my ($data) = @_;
35              
36             my $twig = XML::Twig
37             ->new()
38             ->set_xml_version("1.0")
39             ->set_encoding('utf-8');
40              
41             my $map = XML::Twig::Elt->new('map');
42             $twig->set_root($map);
43              
44             foreach my $i (keys %$data) {
45             my $e = $map->insert_new_elt($i, $data->{$i}->{attributes});
46             foreach my $j (keys %{$data->{$i}->{children}}) {
47             my $m = $e->insert_new_elt($j.'s');
48             foreach (@{$data->{$i}->{children}->{$j}}) {
49             $m->insert_new_elt($j, $_);
50             }
51             }
52             }
53              
54             $twig->set_pretty_print('indented');
55             return $twig->sprint;
56             }
57              
58             sub get_data {
59             my ($self, $object) = @_;
60              
61             validate_object($object);
62              
63             my $data = {};
64             if (ref($object) eq 'Map::Tube::Node') {
65             $data = {
66             id => $object->id,
67             name => $object->name,
68             links => [ map {{ id => $_, name => $self->get_node_by_id($_)->name }} (split /\,/,$object->link) ],
69             lines => [ map {{ id => $_->id, name => $_->name }} (@{$object->line}) ],
70             };
71             }
72             elsif (ref($object) eq 'Map::Tube::Line') {
73             $data = {
74             id => $object->id,
75             name => $object->name,
76             color => $object->color || 'undef',
77             stations => [ map {{ id => $_->id, name => $_->name }} (@{$object->get_stations}) ],
78             };
79             }
80             elsif (ref($object) eq 'Map::Tube::Route') {
81             my $children = [];
82             my $nodes = $object->nodes;
83             my $size = $#$nodes;
84             foreach my $i (1..($size-1)) {
85             push @{$children}, { name => $nodes->[$i]->as_string, order => $i };
86             }
87              
88             $data = {
89             from => $object->from->as_string,
90             to => $object->to->as_string,
91             nodes => $children,
92             };
93             }
94              
95             return $data;
96             }
97              
98             sub validate_object {
99             my ($object) = @_;
100              
101             my @caller = caller(0);
102             @caller = caller(2) if $caller[3] eq '(eval)';
103              
104             Map::Tube::Exception::MissingSupportedObject->throw({
105             method => __PACKAGE__."::validate_object",
106             message => "ERROR: No object received.",
107             filename => $caller[1],
108             line_number => $caller[2] })
109             unless defined $object;
110              
111             Map::Tube::Exception::InvalidSupportedObject->throw({
112             method => __PACKAGE__."::validate_object",
113             message => "ERROR: Invalid object received.",
114             filename => $caller[1],
115             line_number => $caller[2] })
116             unless ref($object);
117              
118             Map::Tube::Exception::FoundUnsupportedObject->throw({
119             method => __PACKAGE__."::validate_object",
120             message => "ERROR: Unsupported object received.",
121             filename => $caller[1],
122             line_number => $caller[2] })
123             unless ((ref($object) eq 'Map::Tube::Node')
124             || (ref($object) eq 'Map::Tube::Line')
125             || (ref($object) eq 'Map::Tube::Route'));
126             }
127              
128             =head1 AUTHOR
129              
130             Mohammad S Anwar, C<< >>
131              
132             =head1 REPOSITORY
133              
134             L
135              
136             =head1 BUGS
137              
138             Please report any bugs or feature requests to C,
139             or through the web interface at L.
140             I will be notified and then you'll automatically be notified of progress on your
141             bug as I make changes.
142              
143             =head1 SUPPORT
144              
145             You can find documentation for this module with the perldoc command.
146              
147             perldoc Map::Tube::Plugin::Formatter::Utils
148              
149             You can also look for information at:
150              
151             =over 4
152              
153             =item * RT: CPAN's request tracker (report bugs here)
154              
155             L
156              
157             =item * AnnoCPAN: Annotated CPAN documentation
158              
159             L
160              
161             =item * CPAN Ratings
162              
163             L
164              
165             =item * Search CPAN
166              
167             L
168              
169             =back
170              
171             =head1 LICENSE AND COPYRIGHT
172              
173             Copyright (C) 2015 - 2016 Mohammad S Anwar.
174              
175             This program is free software; you can redistribute it and / or modify it under
176             the terms of the the Artistic License (2.0). You may obtain a copy of the full
177             license at:
178              
179             L
180              
181             Any use, modification, and distribution of the Standard or Modified Versions is
182             governed by this Artistic License.By using, modifying or distributing the Package,
183             you accept this license. Do not use, modify, or distribute the Package, if you do
184             not accept this license.
185              
186             If your Modified Version has been derived from a Modified Version made by someone
187             other than you,you are nevertheless required to ensure that your Modified Version
188             complies with the requirements of this license.
189              
190             This license does not grant you the right to use any trademark, service mark,
191             tradename, or logo of the Copyright Holder.
192              
193             This license includes the non-exclusive, worldwide, free-of-charge patent license
194             to make, have made, use, offer to sell, sell, import and otherwise transfer the
195             Package with respect to any patent claims licensable by the Copyright Holder that
196             are necessarily infringed by the Package. If you institute patent litigation
197             (including a cross-claim or counterclaim) against any party alleging that the
198             Package constitutes direct or contributory patent infringement,then this Artistic
199             License to you shall terminate on the date that such litigation is filed.
200              
201             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
202             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
203             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
204             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
205             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
206             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
207             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
208              
209             =cut
210              
211             1; # End of Map::Tube::Plugin::Formatter::Utils