view Lib/IMPL/DOM/Transform/QueryToDOM.pm @ 250:129e48bb5afb

DOM refactoring ObjectToDOM methods are virtual QueryToDOM uses inflators Fixed transform for the complex values in the ObjectToDOM QueryToDOM doesn't allow to use complex values (HASHes) as values for nodes (overpost problem)
author sergey
date Wed, 07 Nov 2012 04:17:53 +0400
parents a02b110da931
children c9c2ec29793f
line wrap: on
line source

package IMPL::DOM::Transform::QueryToDOM;
use strict;

use IMPL::Const qw(:prop);
use IMPL::declare {
	base => [
	   'IMPL::DOM::Transform::ObjectToDOM' => '@_'
	],
	props => [
	   prefix => PROP_RO,
	   delimiter => PROP_RO
	]
};

sub CTOR {
	my ($this) = @_;
	
	$this->templates->{'CGI'} = \&TransformCGI;

	$this->delimiter('[.]');
	$this->prefix('');
}

# inflate simple properties
sub TransformPlain {
    my ($this,$data) = @_;
    
    $this->currentNode->nodeProperty( rawValue => $data );
    $this->currentNode->nodeValue( $this->inflateNodeValue($data) );
    return $this->currentNode;
}

# do not store complex data as node values
sub StoreObject {
    my ($this,$node,$data) = @_;
    
    return $node;
}

sub TransformCGI {
	my ($this,$query) = @_;

    my $data={};
    
    my $prefix = $this->prefix;
    my $delim = $this->delimiter;
    
    foreach my $param (grep index($_,$prefix) >= 0 , $query->param()) {
        
        my @value = grep length($_), $query->param($param) or next;
        
        my @parts = split /$delim/,$param;
        
        my $node = $data;
        while ( my $part = shift @parts ) {
            if (my ($name,$index) = ($part =~ m/^(\w+)(?:\[(\d+)\])?$/) ) {
                if (@parts) {
                    if(defined $index) {
                        $node = ($node->{$name}[$index] ||= {});
                    } else {
                        $node = ($node->{$name} ||= {});
                    }
                } else {
                    if(defined $index) {
                        $node->{$name}[$index] = (@value == 1 ? $value[0] : \@value);
                    } else {
                        $node->{$name} = (@value == 1 ? $value[0] : \@value);
                    }
                }
            }
        }  
    }
    
    return $this->Transform($data);
}

1;