File Coverage

blib/lib/SOAP/Data/Builder/Element.pm
Criterion Covered Total %
statement 6 97 6.1
branch 0 30 0.0
condition 0 15 0.0
subroutine 2 14 14.2
pod 12 12 100.0
total 20 168 11.9


line stmt bran cond sub pod time code
1             package SOAP::Data::Builder::Element;
2 1     1   4 use strict;
  1         2  
  1         33  
3              
4             =head1 NAME
5              
6             SOAP::Data::Builder::Element - A simple wrapper SOAP::Data Elements
7              
8             =head1 DESCRIPTION
9              
10             This Module provides a quick and easy way to build complex SOAP data
11             and header structures for use with SOAP::Lite, managed by SOAP::Data::Builder.
12              
13             =cut
14              
15 1     1   984 use Data::Dumper;
  1         10238  
  1         1050  
16              
17             =head1 METHODS
18              
19             =head2 new(autotype=>0)
20              
21             Constructor method for this class, it instantiates and returns the element object,
22             taking value and attributes as named parameters
23              
24             my $element = SOAP::Data::Builder::Element->new( name=> 'anExample', VALUE=> 'foo', attributes => { 'ns1:foo' => 'bar'});
25              
26             optional parameters are : value, attributes, header, isMethod
27              
28             parent should be an element fetched using get_elem
29              
30             value should be a string, to add child nodes use add_elem(parent=>get_elem('name/of/parent'), .. )
31              
32             attributes should be a hashref : { 'ns:foo'=> bar, .. }
33              
34             header should be 1 or 0 specifying whether the element should be built using SOAP::Data or SOAP::Header
35              
36             =cut
37              
38             sub new {
39 0     0 1   my ($class,%args) = @_;
40 0           my $self = {};
41 0   0       bless ($self,ref $class || $class);
42 0           foreach my $key (keys %args) {
43 0   0       $self->{$key} = $args{$key} || 0;
44             }
45 0 0         if ($args{parent}) {
46 0 0         $self->{fullname} = (ref $args{parent}) ? $args{parent}->{fullname}: "$args{parent}/$args{name}";
47             }
48 0   0       $self->{fullname} ||= $args{name};
49 0           $self->{VALUE} = [ $args{value} ];
50 0           return $self;
51             }
52              
53             =head2 value()
54              
55             the value() method sets/gets the VALUE of the element
56              
57             =cut
58              
59             sub value {
60 0     0 1   my $self = shift;
61 0           my $value = shift;
62 0 0         if ($value) {
63 0 0         if (ref $value) {
64 0           $self->{VALUE} = $value;
65             } else {
66 0           $self->{VALUE} = [$value];
67             }
68             } else {
69 0           $value = $self->{VALUE};
70             }
71 0           return $value;
72             }
73              
74             =head2 name()
75              
76             the name() method gets/sets the name of the element
77              
78             =cut
79              
80             sub name {
81 0     0 1   my $self = shift;
82 0           my $value = shift;
83 0 0         if ($value) {
84 0           $self->{name} = $value;
85             } else {
86 0           $value = $self->{name};
87             }
88 0           return $value;
89             }
90              
91             =head2 fullname()
92              
93             the fullname() method returns the full '/' delimited name of the element
94              
95             'eb:foo/eb:name' would return the inner element on ..
96              
97             =cut
98              
99             sub fullname {
100 0     0 1   my $self = shift;
101 0   0       return $self->{fullname} || $self->{name};
102             }
103              
104             =head2 attributes()
105              
106             returns a hashref of the elements attributes
107              
108             =cut
109              
110             sub attributes {
111 0     0 1   my $self = shift;
112 0   0       return $self->{attributes} || {};
113             }
114              
115             =head2 remove_attribute($name)
116              
117             removes a named attribute - returns 1 if it existed , 0 if not
118              
119             =cut
120              
121             sub remove_attribute {
122 0     0 1   my ($self, $attribute) = @_;
123 0           my $success = 0;
124 0 0         if ($self->{attributes}{$attribute}) {
125 0           delete $self->{attributes}{$attribute};
126 0           $success++;
127             }
128 0           return $success;
129             }
130              
131             =head2 set_attribute($name,$value)
132              
133             sets a named attribute
134              
135             =cut
136              
137             sub set_attribute {
138 0     0 1   my ($self, $attribute, $value) = @_;
139 0           $self->{attributes}{$attribute} = $value;
140 0           return 1;
141             }
142              
143             =head2 get_attribute($name)
144              
145             gets a named attribute
146              
147             =cut
148              
149             sub get_attribute {
150 0     0 1   my ($self, $attribute) = @_;
151 0           return $self->{attributes}{$attribute};
152             }
153              
154             =head2 add_elem($elem)
155              
156             This method adds an element as a child to another element.
157              
158             Accepts either a SOAP::Data::Builder::Element object or a hash of arguments to create the object
159              
160             Returns the added element
161              
162             my $child = $parent->add_elem(name=>'foo',..);
163              
164             or
165              
166             $parent->add_elem($child);
167              
168             =cut
169              
170             sub add_elem {
171 0     0 1   my $self = shift;
172 0           my $elem;
173 0 0         if (ref $_[0] eq 'SOAP::Data::Builder::Element') {
174 0           $elem = $_[0];
175 0           push(@{$self->{VALUE}},$elem);
  0            
176             } else {
177 0           $elem = {};
178 0           bless ($elem,ref $self);
179 0           my %args = @_;
180 0           foreach my $key (keys %args) {
181 0   0       $elem->{$key} = $args{$key} || 0;
182             }
183 0           $elem->{fullname} = $self->{fullname}."/$args{name}";
184 0           $elem->{VALUE} = [ $args{value} ];
185 0           push(@{$self->{VALUE}},$elem);
  0            
186             }
187 0           return $elem;
188             }
189              
190             =head2 get_children()
191              
192             returns a list of the child nodes of an element
193              
194             =cut
195              
196             sub get_children {
197 0     0 1   my $self = shift;
198 0           my @children = shift;
199 0           foreach my $value (@{$self->value}) {
  0            
200 0 0         push (@children, $value ) if ref $value;
201             }
202 0 0         if (wantarray) {
203 0           return @children;
204             } else {
205 0           return \@children;
206             }
207             }
208              
209             =head2 remove_elem($name)
210              
211             removes the named node from the element, returns 1 if existed, 0 if not
212              
213             =cut
214              
215             sub remove_elem {
216 0     0 1   my ($self,$childname) = @_;
217 0           my @tmp_values = ();
218 0           my $success = 0;
219 0           foreach my $value (@{$self->value}) {
  0            
220 0 0         if (ref $value) {
221 0 0         push (@tmp_values, $value) unless ($value->fullname eq $childname);
222 0           $success++;
223             } else {
224 0           push (@tmp_values, $value);
225             }
226             }
227 0           $self->{VALUE} = [ @tmp_values ];
228 0           return $success;
229             }
230              
231             # soap data method
232              
233             =head2 get_as_data()
234              
235             returns the element and its sub-nodes in SOAP::Data objects.
236              
237             =cut
238              
239             sub get_as_data {
240 0     0 1   my $self = shift;
241 0           my @values;
242 0           foreach my $value ( @{$self->{VALUE}} ) {
  0            
243 0 0         if (ref $value) {
244 0           push(@values,$value->get_as_data())
245             } else {
246 0           push(@values,$value);
247             }
248             }
249              
250 0           my @data = ();
251              
252 0 0         if (ref $values[0]) {
253 0           $data[0] = \SOAP::Data->value( @values );
254             } else {
255 0           @data = @values;
256             }
257              
258 0 0         if ($self->{header}) {
259 0           $data[0] = SOAP::Header->name($self->{name} => $data[0])->attr($self->{attributes});
260             } else {
261 0 0         if ($self->{isMethod}) {
262 0           @data = ( SOAP::Data->name($self->{name} )->attr($self->{attributes}) => SOAP::Data->value( @values ) );
263             } else {
264 0           $data[0] = SOAP::Data->name($self->{name} => $data[0])->attr($self->{attributes});
265             }
266             }
267              
268 0           return @data;
269             }
270              
271              
272             1;