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.15.
2             # The POD got stripped from this file by OODoc version 3.06.
3             # For contributors see file ChangeLog.
4              
5             # This software is copyright (c) 2024-2026 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              
12             package Business::CAMT::Message;{
13             our $VERSION = '0.15';
14             }
15              
16              
17 1     1   700448 use strict;
  1         3  
  1         42  
18 1     1   7 use warnings;
  1         3  
  1         77  
19              
20 1     1   7 use Log::Report 'business-camt';
  1         2  
  1         14  
21 1     1   499 use Scalar::Util qw/weaken/;
  1         2  
  1         82  
22 1     1   7 use JSON ();
  1         5  
  1         367  
23              
24              
25             sub new
26 0     0 1   { my ($class, %args) = @_;
27 0 0         my $data = delete $args{data} or return undef;
28 0           (bless $data, $class)->init(\%args);
29             }
30              
31             sub init($) {
32 0     0 0   my ($self, $args) = @_;
33              
34 0           my %attrs;
35 0 0         $attrs{set} = $args->{set} or panic;
36 0 0         $attrs{version} = $args->{version} or panic;
37 0 0         $attrs{camt} = $args->{camt} or panic;
38 0           weaken $attrs{camt};
39 0           $self->{_attrs} = \%attrs;
40              
41 0           $self;
42             }
43              
44              
45             sub _loadSubclass($)
46 0     0     { my ($class, $set) = @_;
47 0 0         $class eq __PACKAGE__ or return $class;
48 0           my $super = 'Business::CAMT::CAMT'.($set =~ s/\..*//r);
49              
50             # Is there a special implementation for this type? Otherwise create
51             # an empty placeholder.
52 1     1   8 no strict 'refs';
  1         2  
  1         668  
53 0 0         eval "require $super" or @{"$super\::ISA"} = __PACKAGE__;
  0            
54 0           $super;
55             }
56              
57             sub fromData(%)
58 0     0 1   { my ($class, %args) = @_;
59 0 0         my $set = $args{set} or panic;
60 0           $class->_loadSubclass($set)->new(%args);
61             }
62              
63             #--------------------
64              
65 0     0 1   sub set { $_[0]->{_attrs}{set} }
66 0     0 1   sub version { $_[0]->{_attrs}{version} }
67 0     0 1   sub camt { $_[0]->{_attrs}{camt} }
68              
69             #--------------------
70              
71             sub write(%)
72 0     0 1   { my ($self, $file) = (shift, shift);
73 0           $self->camt->write($file, $self, @_);
74             }
75              
76              
77             sub toPerl()
78 0     0 1   { my $self = shift;
79 0           my $attrs = delete $self->{_attrs};
80              
81 0           my $d = Data::Dumper->new([$self], 'MESSAGE');
82 0           $d->Sortkeys(1)->Quotekeys(0)->Indent(1);
83 0           my $text = $d->Dump;
84              
85 0           $self->{_attrs} = $attrs;
86 0           $text;
87             }
88              
89              
90             sub toJSON(%)
91 0     0 1   { my ($self, %args) = @_;
92 0           my %data = %$self; # Shallow copy to remove blessing
93 0           delete $data{_attrs}; # remove object attributes
94              
95 0   0       my $settings = $args{settings} || {};
96 0           my %settings = (pretty => 1, canonical => 1, %$settings);
97              
98             # JSON parameters call methods, copied from to_json behavior
99 0           my $json = JSON->new;
100 0           while(my ($method, $value) = each %settings)
101 0           { $json->$method($value);
102             }
103              
104 0           $json->encode(\%data); # returns bytes
105             }
106              
107             1;