view Lib/IMPL/DOM/Transform/QueryToDOM.pm @ 237:61db68166c37

refactoring QueryToDOM
author sergey
date Mon, 15 Oct 2012 17:39:12 +0400
parents
children b8c724f6de36
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,
	   delimier => PROP_RO
	]
};

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

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

    my $data={};
    
    my $prefix = $this->prefix;
    my $delim = $this->delimiter;
    
    foreach my $param (grep index($_,$prefix) >= 0 , $query->param()) {
        length (my $value = $query->param($param)) or next;
        
        my @parts = split /\//,$param;
        
        my $node = $data;
        while ( my $part = shift @parts ) {
            if (@parts) {
                $node = ($node->{$part} ||= {});
            } else {            
                $node->{$part} = $value;
            }
        }  
    }
    
    if (keys %$data > 1) {
        $data = { document => $data };
    }
    
    return $this->Transform($data);
}