File Coverage

lib/Search/QS/Options.pm
Criterion Covered Total %
statement 50 50 100.0
branch 9 12 75.0
condition n/a
subroutine 11 11 100.0
pod 3 3 100.0
total 73 76 96.0


line stmt bran cond sub pod time code
1             package Search::QS::Options;
2             $Search::QS::Options::VERSION = '0.03';
3 4     4   44 use v5.14;
  4         12  
4 4     4   16 use strict;
  4         5  
  4         71  
5 4     4   15 use warnings;
  4         6  
  4         78  
6              
7 4     4   16 use Moose;
  4         5  
  4         21  
8 4     4   22917 use Set::Array;
  4         8  
  4         114  
9 4     4   1409 use Search::QS::Options::Sort;
  4         10  
  4         2267  
10              
11             # ABSTRACT: Options query search like limits, start and sort
12              
13              
14             has start => ( is => 'rw', isa => 'Int', default => 0);
15             has limit => ( is => 'rw', isa => 'Int|Undef');
16             has sort => ( is => 'rw', isa => 'Set::Array', default => sub {
17             return new Set::Array;
18             }
19             );
20              
21              
22             sub parse() {
23 6     6 1 9 my $s = shift;
24 6         9 my $struct = shift;
25              
26 6         16 $s->reset();
27              
28 6         23 while (my ($k,$v) = each %$struct) {
29 20         354 given($k) {
30 20         39 when ('start') { $s->start($v) }
  5         165  
31 15         20 when ('limit') { $s->limit($v) }
  4         86  
32 11         33 when (/^sort\[(.*?)\]/) { $s->_parse_sort($1, $v) }
  5         14  
33             }
34             }
35             }
36              
37             sub _parse_sort() {
38 5     5   8 my $s = shift;
39 5         8 my $key = shift;
40 5         8 my $val = shift;
41              
42 5 50       14 $val = 'asc' if ($val eq 1);
43 5 50       18 $val = 'desc' if ($val eq -1);
44              
45 5 50       20 return unless ($val =~ /^(asc|desc)$/);
46              
47 5         114 $s->sort->push(new Search::QS::Options::Sort(
48             name => $key,
49             direction => $val
50             ));
51             }
52              
53              
54             sub to_qs() {
55 7     7 1 10 my $s = shift;
56 7         166 my $sort = join('&', map($_->to_qs, $s->sort->compact() ));
57              
58 7         284 my $ret = '';
59 7 100       154 $ret.= 'start=' . $s->start . '&' unless ($s->start == 0);
60 7 100       140 $ret.= 'limit=' . $s->limit . '&' if ($s->limit);
61 7 100       21 $ret.= $sort . '&' if ($sort);
62              
63 7         17 chop($ret);
64              
65 7         25 return $ret;
66             }
67              
68             sub reset() {
69 6     6 1 9 my $s = shift;
70 6         121 $s->sort->clear;
71 6         455 $s->limit(undef);
72 6         129 $s->start(0);
73             }
74              
75              
76 4     4   30 no Moose;
  4         5  
  4         34  
77             __PACKAGE__->meta->make_immutable;
78              
79             1;
80              
81             __END__
82              
83             =pod
84              
85             =encoding UTF-8
86              
87             =head1 NAME
88              
89             Search::QS::Options - Options query search like limits, start and sort
90              
91             =head1 VERSION
92              
93             version 0.03
94              
95             =head1 SYNOPSIS
96              
97             use Search::QS::Options;
98              
99             my $opt = new Search::QS::Options;
100             # parse query_string
101             $opt->parse_qs($qs);
102             # reconvert object to query_string
103             print $opt->to_qs;
104              
105             =head1 DESCRIPTION
106              
107             This object incapsulate the options of a query.
108              
109             =head1 METHODS
110              
111             =head2 start()
112              
113             Set/Get the first record to show
114              
115             =head2 limit()
116              
117             Set/Get the max number of elements to show
118              
119             =head2 sort()
120              
121             An array (L<Set::Array>) of L<Search::QS::Options::Sort> with sort informations
122              
123             =head2 parse($perl_struct)
124              
125             $perl_struct is an HASHREF which represents a query string like
126             the one returned by L<URI::Encode/"url_params_mixed">.
127             It parses the struct and extract filter informations
128              
129             =head2 to_qs()
130              
131             Return a query string of the internal rappresentation of the object
132              
133             =head2 reset()
134              
135             Initialize the object with default values
136              
137             =head1 SEE ALSO
138              
139             L<Seach::QS::Options::Sort>
140              
141             =head1 AUTHOR
142              
143             Emiliano Bruni <info@ebruni.it>
144              
145             =head1 COPYRIGHT AND LICENSE
146              
147             This software is copyright (c) 2019 by Emiliano Bruni.
148              
149             This is free software; you can redistribute it and/or modify it under
150             the same terms as the Perl 5 programming language system itself.
151              
152             =cut