DBRIS/Formation/Carriage.pm untested

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 273 CC 31 SCAR 49.9
CC
31
Cov
0%
new
272.0
parse_type
156.0
is_first_class
12.0
CRAP 146.8
1 package Travel::Status::DE::DBRIS::Formation::Carriage;
2  
3 use strict;
4 use warnings;
5 use 5.020;
6 use utf8;
7  
8 use parent 'Class::Accessor';
9 use Carp qw(cluck);
10  
11 our $VERSION = '0.40';
12 Travel::Status::DE::DBRIS::Formation::Carriage->mk_ro_accessors(
13 qw(class_type is_closed is_dosto is_locomotive is_powercar
14 number model model4 section uic_id country type
15 start_meters end_meters length_meters start_percent end_percent length_percent
16 has_priority_seats has_ac has_quiet_zone has_bahn_comfort has_wheelchair_space
17 has_wheelchair_toilet has_family_zone has_infant_cabin has_info has_bistro
18 has_first_class has_second_class
19 )
20 );
21  
22 my %type_map = (
23 SEATS_SEVERELY_DISABLE => 'priority_seats',
24 AIR_CONDITION => 'ac',
25 ZONE_QUIET => 'quiet_zone',
26 SEATS_BAHN_COMFORT => 'bahn_comfort',
27 INFO => 'info',
28 TOILET_WHEELCHAIR => 'wheelchair_toilet',
29 WHEELCHAIR_SPACE => 'wheelchair_space',
30 ZONE_FAMILY => 'family_zone',
31 CABIN_INFANT => 'infant_cabin',
32 );
33  
34 sub new {
35 my ( $obj, %opt ) = @_;
36 my $ref = {};
37  
38 my %json = %{ $opt{json} };
39 my $platform = $opt{platform};
40  
41 $ref->{class_type} = 0;
42 $ref->{has_bistro} = 0;
43 $ref->{is_locomotive} = 0;
44 $ref->{is_powercar} = 0;
45 $ref->{is_closed} = 0;
46 $ref->{number} = $json{wagonIdentificationNumber};
47 $ref->{model} = $json{vehicleID};
48 $ref->{uic_id} = $json{vehicleID};
49 $ref->{section} = $json{platformPosition}{sector};
50 $ref->{type} = $json{type}{constructionType};
51  
52 if ( defined $ref->{uic_id} and length( $ref->{uic_id} ) >= 12 ) {
53 $ref->{uic_id} =~ s{ ^ \d{11} \K - }{}x;
54 $ref->{country} = substr( $ref->{uic_id}, 2, 2 );
55 $ref->{model4} = substr( $ref->{uic_id}, 4, 4 );
56 }
57 if ( defined $ref->{model} ) {
58 $ref->{model} =~ s{^.....(...)...-?.(?:-.)?$}{$1}
59 or $ref->{model} = undef;
60 }
61  
62 my $self = bless( $ref, $obj );
63  
64 $self->parse_type;
65  
66 for my $amenity ( @{ $json{amenities} // [] } ) {
67 my $type = $amenity->{type};
68 if ( $type_map{$type} ) {
69 my $key = 'has_' . $type_map{$type};
70 $self->{$key} = 1;
71 }
72 }
73  
74 if ( $json{status} and $json{status} eq 'CLOSED' ) {
75 $ref->{is_closed} = 1;
76 }
77  
78 if ( $json{type}{category} =~ m{DININGCAR} ) {
79 $ref->{has_bistro} = 1;
80 }
81 elsif ( $json{type}{category} eq 'LOCOMOTIVE' ) {
82 $ref->{is_locomotive} = 1;
83 }
84 elsif ( $json{type}{category} eq 'POWERCAR' ) {
85 $ref->{is_powercar} = 1;
86 }
87  
88 $ref->{has_first_class} = $json{type}{hasFirstClass};
89 $ref->{has_second_class} = $json{type}{hasEconomyClass};
90  
91 if ( $ref->{type} ) {
92 if ( $ref->{type} =~ m{AB} ) {
93 $ref->{class_type} = 12;
94 }
95 elsif ( $ref->{type} =~ m{A} ) {
96 $ref->{class_type} = 1;
97 }
98 elsif ( $ref->{type} =~ m{B|WR} ) {
99 $ref->{class_type} = 2;
100 }
101 }
102  
103 my $pos = $json{platformPosition};
104 my $platform_length = $platform->{end} - $platform->{start};
105  
106 $ref->{start_meters} = $pos->{start};
107 $ref->{end_meters} = $pos->{end};
108 if ( defined $pos->{start}
109 and defined $platform->{start}
110 and $platform_length )
111 {
112 $ref->{start_percent}
113 = ( $pos->{start} - $platform->{start} ) * 100 / $platform_length;
114 }
115 if ( defined $pos->{end}
116 and defined $platform->{start}
117 and $platform_length )
118 {
119 $ref->{end_percent}
120 = ( $pos->{end} - $platform->{start} ) * 100 / $platform_length;
121 }
122  
123 if ( not defined $pos->{start}
124 or not defined $pos->{end}
125 or $pos->{start} eq ''
126 or $pos->{end} eq '' )
127 {
128 $ref->{position}{valid} = 0;
129 }
130 else {
131 $ref->{position}{valid} = 1;
132 $ref->{length_meters} = $pos->{start} - $pos->{end};
133 }
134  
135 if ( defined $ref->{start_percent} and defined $ref->{end_percent} ) {
136 $ref->{length_percent} = $ref->{end_percent} - $ref->{start_percent};
137 }
138  
139 return $self;
140 }
141  
142 sub attributes {
143 my ($self) = @_;
144  
145 return @{ $self->{attributes} // [] };
146 }
147  
148 # See also:
149 # https://de.wikipedia.org/wiki/UIC-Bauart-Bezeichnungssystem_f%C3%BCr_Reisezugwagen#Kennbuchstaben
150 # https://www.deutsche-reisezugwagen.de/lexikon/erklarung-der-gattungszeichen/
151 sub parse_type {
152 my ($self) = @_;
153  
154 my $type = $self->{type};
155 my @desc;
156  
157 if ( not $type ) {
158 $self->{attributes} = [];
159 return;
160 }
161  
162 if ( $type =~ m{^D} ) {
163 $self->{is_dosto} = 1;
164 push( @desc, 'Doppelstock' );
165 }
166  
167 if ( $type =~ m{b} ) {
168 $self->{has_accessibility} = 1;
169 push( @desc, 'Behindertengerechte Ausstattung' );
170 }
171  
172 if ( $type =~ m{d} ) {
173 $self->{multipurpose} = 1;
174 push( @desc, 'Mehrzweck' );
175 }
176  
177 if ( $type =~ m{f} ) {
178 push( @desc, 'Steuerabteil' );
179 }
180  
181 if ( $type =~ m{i} ) {
182 push( @desc, 'Interregio' );
183 }
184  
185 if ( $type =~ m{mm} ) {
186 push( @desc, 'modernisiert' );
187 }
188  
189 if ( $type =~ m{p} ) {
190 $self->{has_ac} = 1;
191 push( @desc, 'Großraum' );
192 }
193  
194 if ( $type =~ m{s} ) {
195 push( @desc, 'Sonderabteil' );
196 }
197  
198 if ( $type =~ m{v} ) {
199 $self->{has_ac} = 1;
200 $self->{has_compartments} = 1;
201 push( @desc, 'Abteil' );
202 }
203  
204 if ( $type =~ m{w} ) {
205 $self->{has_ac} = 1;
206 $self->{has_compartments} = 1;
207 push( @desc, 'Abteil' );
208 }
209  
210 $self->{attributes} = \@desc;
211 }
212  
213 sub is_first_class {
214 my ($self) = @_;
215  
216 if ( not defined $self->{type} ) {
217 return;
218 }
219 if ( $self->{type} =~ m{^D?A} ) {
220 return 1;
221 }
222 return 0;
223 }
224  
225 sub is_second_class {
226 my ($self) = @_;
227  
228 if ( not defined $self->{type} ) {
229 return;
230 }
231 if ( $self->{type} =~ m{^D?A?B} ) {
232 return 1;
233 }
234 return 0;
235 }
236  
237 sub sections {
238 my ($self) = @_;
239  
240 return @{ $self->{sections} };
241 }
242  
243 sub TO_JSON {
244 my ($self) = @_;
245  
246 my %copy = %{$self};
247  
248 return {%copy};
249 }
250  
251 1;
252