File Coverage

lib/Search/QS/Options.pm
Criterion Covered Total %
statement 59 59 100.0
branch 5 8 62.5
condition n/a
subroutine 16 16 100.0
pod 3 3 100.0
total 83 86 96.5


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