File Coverage

lib/Search/QS.pm
Criterion Covered Total %
statement 30 30 100.0
branch 3 4 75.0
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 43 44 97.7


line stmt bran cond sub pod time code
1             package Search::QS;
2             $Search::QS::VERSION = '0.03';
3 4     4   231763 use strict;
  4         30  
  4         96  
4 4     4   16 use warnings;
  4         6  
  4         85  
5              
6 4     4   1801 use Moose;
  4         1516808  
  4         24  
7              
8 4     4   26082 use Search::QS::Filters;
  4         11  
  4         132  
9 4     4   1530 use Search::QS::Options;
  4         13  
  4         742  
10              
11             # ABSTRACT: A converter between query string URI and search query
12              
13              
14             has filters => ( is => 'ro', isa => 'Search::QS::Filters',
15             default => sub {
16             return new Search::QS::Filters;
17             }
18             );
19              
20              
21             has options => ( is => 'ro', isa => 'Search::QS::Options',
22             default => sub {
23             return new Search::QS::Options;
24             }
25             );
26              
27             sub parse {
28 2     2 1 9 my $s = shift;
29 2         3 my $v = shift;
30              
31 2         47 $s->filters->parse($v);
32 2         115 $s->options->parse($v);
33             }
34              
35             sub to_qs {
36 2     2 1 78 my $s = shift;
37              
38 2         51 my $qs_filters = $s->filters->to_qs;
39 2         39 my $qs_options = $s->options->to_qs;
40              
41 2         3 my $ret = '';
42 2 50       8 $ret .= $qs_filters . '&' unless ($qs_filters eq '');
43 2 100       37 $ret .= $qs_options . '&' unless ($qs_options eq '');
44             # strip last &
45 2         5 chop($ret);
46              
47 2         49 return $ret;
48              
49             }
50              
51              
52 4     4   30 no Moose;
  4         8  
  4         23  
53             __PACKAGE__->meta->make_immutable;
54              
55             1;
56              
57             __END__
58              
59             =pod
60              
61             =encoding UTF-8
62              
63             =head1 NAME
64              
65             Search::QS - A converter between query string URI and search query
66              
67             =head1 VERSION
68              
69             version 0.03
70              
71             =head1 SYNOPSIS
72              
73             use Search::QS;
74              
75             my $qs = new Search::QS;
76             # parse query_string
77             $qs->parse_qs($qs);
78             # reconvert object to query_string
79             print $qs->to_qs;
80              
81             =head1 DESCRIPTION
82              
83             This module converts a query string like This
84              
85             http://www.example.com?flt[Name]=Foo
86              
87             into perl objects which rappresent a search.
88              
89             In L</"filters()"> there are all flt (filter) elements.
90              
91             In L</"options()"> there are query options like limit, start and sorting.
92              
93             =head1 METHODS
94              
95             =head2 filters()
96              
97             Return an instance of L<Search::QS::Filters>
98              
99             =head2 options()
100              
101             Return an instance of L<Search::QS::Options>
102              
103             =head2 parse($perl_struct)
104              
105             $perl_struct is an HASHREF which represents a query string like
106             the one returned by L<URI::Encode/"url_params_mixed">.
107             It parses the $perl_struct and fills related objects in
108             L</"filters()"> and L</"options()">
109              
110             =head2 to_qs()
111              
112             Return a query string which represents current state of L<filters()>
113             and L<options()> elements
114              
115             =head1 Examples
116              
117             Here some Examples.
118              
119             =over
120              
121             =item C<?flt[Name]=Foo>
122              
123             should be converted into
124              
125             Name = 'Foo'
126              
127             =item C<?flt[Name]=Foo%&flt[Name]=$op:like>
128              
129             should be converted into
130              
131             Name like 'Foo%'
132              
133             =item C<?flt[age]=5&flt[age]=9&flt[Name]=Foo>
134              
135             should be converted into
136              
137             (age = 5 OR age = 9) AND (Name = Foo)
138              
139             =item C<?flt[FirstName]=Foo&flt[FirstName]=$or:1&flt[LastName]=Bar&flt[LastName]=$or:1>
140              
141             should be converted into
142              
143             ( (FirstName = Foo) OR (LastName = Bar) )
144              
145             =item C<?flt[c:one]=1&flt[c:one]=$and:1&flt[d:one]=2&flt[d:one]=$and:1&flt[c:two]=2&flt[c:two]=$and:2&flt[d:two]=3&flt[d:two]=$op:!=&flt[d:two]=$and:2&flt[d:three]=10>
146              
147             should be converted into
148              
149             (d = 10) AND ( (c = 1) AND (d = 2) ) OR ( (c = 2) AND (d != 3) )
150              
151             =back
152              
153             =head1 SEE ALSO
154              
155             L<Search::QS::Filters>, L<Search::QS::Filter>, L<Search::QS::Options>
156              
157             =head1 AUTHOR
158              
159             Emiliano Bruni <info@ebruni.it>
160              
161             =head1 COPYRIGHT AND LICENSE
162              
163             This software is copyright (c) 2019 by Emiliano Bruni.
164              
165             This is free software; you can redistribute it and/or modify it under
166             the same terms as the Perl 5 programming language system itself.
167              
168             =cut