annotate Lib/IMPL/DOM/Transform/QueryToDOM.pm @ 244:a02b110da931

refactoring fixed binding to CGI query parameters with multiple values
author sergey
date Mon, 22 Oct 2012 04:09:27 +0400
parents b8c724f6de36
children 129e48bb5afb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
1 package IMPL::DOM::Transform::QueryToDOM;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
2 use strict;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
3
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
4 use IMPL::Const qw(:prop);
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
5 use IMPL::declare {
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
6 base => [
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
7 'IMPL::DOM::Transform::ObjectToDOM' => '@_'
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
8 ],
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
9 props => [
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
10 prefix => PROP_RO,
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
11 delimiter => PROP_RO
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
12 ]
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
13 };
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
14
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
15 sub CTOR {
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
16 my ($this) = @_;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
17
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
18 $this->templates->{'CGI'} = \&TransformCGI;
244
a02b110da931 refactoring
sergey
parents: 238
diff changeset
19
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
20 $this->delimiter('[.]');
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
21 $this->prefix('');
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
22 }
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
23
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
24 sub TransformCGI {
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
25 my ($this,$query) = @_;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
26
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
27 my $data={};
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
28
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
29 my $prefix = $this->prefix;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
30 my $delim = $this->delimiter;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
31
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
32 foreach my $param (grep index($_,$prefix) >= 0 , $query->param()) {
244
a02b110da931 refactoring
sergey
parents: 238
diff changeset
33
a02b110da931 refactoring
sergey
parents: 238
diff changeset
34 my @value = grep length($_), $query->param($param) or next;
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
35
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
36 my @parts = split /$delim/,$param;
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
37
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
38 my $node = $data;
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
39 while ( my $part = shift @parts ) {
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
40 if (my ($name,$index) = ($part =~ m/^(\w+)(?:\[(\d+)\])?$/) ) {
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
41 if (@parts) {
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
42 if(defined $index) {
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
43 $node = ($node->{$name}[$index] ||= {});
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
44 } else {
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
45 $node = ($node->{$name} ||= {});
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
46 }
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
47 } else {
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
48 if(defined $index) {
244
a02b110da931 refactoring
sergey
parents: 238
diff changeset
49 $node->{$name}[$index] = (@value == 1 ? $value[0] : \@value);
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
50 } else {
244
a02b110da931 refactoring
sergey
parents: 238
diff changeset
51 $node->{$name} = (@value == 1 ? $value[0] : \@value);
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
52 }
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
53 }
237
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
54 }
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
55 }
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
56 }
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
57
61db68166c37 refactoring QueryToDOM
sergey
parents:
diff changeset
58 return $this->Transform($data);
238
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
59 }
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
60
b8c724f6de36 DOM model refactoring
sergey
parents: 237
diff changeset
61 1;