File Coverage

blib/lib/Business/CAMT/Message.pm
Criterion Covered Total %
statement 18 59 30.5
branch 0 14 0.0
condition 0 2 0.0
subroutine 6 16 37.5
pod 8 9 88.8
total 32 100 32.0


line stmt bran cond sub pod time code
1             # This code is part of Perl distribution Business-CAMT version 0.14.
2             # The POD got stripped from this file by OODoc version 3.05.
3             # For contributors see file ChangeLog.
4              
5             # This software is copyright (c) 2024-2025 by Mark Overmeer.
6              
7             # This is free software; you can redistribute it and/or modify it under
8             # the same terms as the Perl 5 programming language system itself.
9             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10              
11             #oodist: *** DO NOT USE THIS VERSION FOR PRODUCTION ***
12             #oodist: This file contains OODoc-style documentation which will get stripped
13             #oodist: during its release in the distribution. You can use this file for
14             #oodist: testing, however the code of this development version may be broken!
15              
16             package Business::CAMT::Message;{
17             our $VERSION = '0.14';
18             }
19              
20              
21 1     1   749732 use strict;
  1         2  
  1         35  
22 1     1   17 use warnings;
  1         2  
  1         65  
23              
24 1     1   4 use Log::Report 'business-camt';
  1         2  
  1         11  
25 1     1   334 use Scalar::Util qw/weaken/;
  1         2  
  1         43  
26 1     1   4 use JSON ();
  1         1  
  1         255  
27              
28              
29             sub new
30 0     0 1   { my ($class, %args) = @_;
31 0 0         my $data = delete $args{data} or return undef;
32 0           (bless $data, $class)->init(\%args);
33             }
34              
35             sub init($) {
36 0     0 0   my ($self, $args) = @_;
37              
38 0           my %attrs;
39 0 0         $attrs{set} = $args->{set} or panic;
40 0 0         $attrs{version} = $args->{version} or panic;
41 0 0         $attrs{camt} = $args->{camt} or panic;
42 0           weaken $attrs{camt};
43 0           $self->{_attrs} = \%attrs;
44              
45 0           $self;
46             }
47              
48              
49             sub _loadSubclass($)
50 0     0     { my ($class, $set) = @_;
51 0 0         $class eq __PACKAGE__ or return $class;
52 0           my $super = 'Business::CAMT::CAMT'.($set =~ s/\..*//r);
53              
54             # Is there a special implementation for this type? Otherwise create
55             # an empty placeholder.
56 1     1   6 no strict 'refs';
  1         2  
  1         385  
57 0 0         eval "require $super" or @{"$super\::ISA"} = __PACKAGE__;
  0            
58 0           $super;
59             }
60              
61             sub fromData(%)
62 0     0 1   { my ($class, %args) = @_;
63 0 0         my $set = $args{set} or panic;
64 0           $class->_loadSubclass($set)->new(%args);
65             }
66              
67             #--------------------
68              
69 0     0 1   sub set { $_[0]->{_attrs}{set} }
70 0     0 1   sub version { $_[0]->{_attrs}{version} }
71 0     0 1   sub camt { $_[0]->{_attrs}{camt} }
72              
73             #--------------------
74              
75             sub write(%)
76 0     0 1   { my ($self, $file) = (shift, shift);
77 0           $self->camt->write($file, $self, @_);
78             }
79              
80              
81             sub toPerl()
82 0     0 1   { my $self = shift;
83 0           my $attrs = delete $self->{_attrs};
84              
85 0           my $d = Data::Dumper->new([$self], 'MESSAGE');
86 0           $d->Sortkeys(1)->Quotekeys(0)->Indent(1);
87 0           my $text = $d->Dump;
88              
89 0           $self->{_attrs} = $attrs;
90 0           $text;
91             }
92              
93              
94             sub toJSON(%)
95 0     0 1   { my ($self, %args) = @_;
96 0           my %data = %$self; # Shallow copy to remove blessing
97 0           delete $data{_attrs}; # remove object attributes
98              
99 0   0       my $settings = $args{settings} || {};
100 0           my %settings = (pretty => 1, canonical => 1, %$settings);
101              
102             # JSON parameters call methods, copied from to_json behavior
103 0           my $json = JSON->new;
104 0           while(my ($method, $value) = each %settings)
105 0           { $json->$method($value);
106             }
107              
108 0           $json->encode(\%data); # returns bytes
109             }
110              
111             1;