Filters.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 109 CC 7 SCAR 18.8
CC
7
Cov
0%
intersect
20.0
new
6.0
match
6.0
CRAP 6.6
1 # Copyright (C) 2017 Koha-Suomi
2 #
3 # This file is part of Pootle-Client.
4  
5 package Pootle::Filters;
6  
7 use Modern::Perl '2015';
8 use utf8;
9 binmode STDOUT, ':encoding(UTF-8)';
10 binmode STDERR, ':encoding(UTF-8)';
11 use feature 'signatures'; no warnings "experimental::signatures";
12 use Carp::Always;
13 use Try::Tiny;
14 use Scalar::Util qw(blessed);
15 use Data::Dumper;
16  
17 =head2 Pootle::Client::Filters
18  
19 Wrapper object for multiple filters.
20 Use this to test a object against multiple filters conveniently.
21  
22 =head2 Synopsis
23  
24 For available language attributes to filter with, see.
25 https://pootle.readthedocs.io/en/stable-2.5.1/api/api_language.html#get-a-language
26 or Pootle::Resource::Language
27  
28 To pick only the Galician language from all the languages the Pootle server supports
29  
30 my $languages = $papi->findLanguages( Pootle::Filters->new({
31 fullname => qr/^Galician/,
32 code => qr/gl_?.*/,
33 }));
34  
35 To find all English dialects
36  
37 my $languages = $papi->findLanguages( Pootle::Filters->new({
38 code => qr/^en/, #All codes starting with 'en', such as en_GB, en_NZ, ...
39 }));
40  
41 Filters can filter all Pootle::Resource::* -objects
42  
43 =cut
44  
45 use Params::Validate qw(:all);
46  
47 use Pootle::Filters::Intersection;
48  
49 use Pootle::Logger;
50 my $l = bless({}, 'Pootle::Logger'); #Lazy load package logger this way to avoid circular dependency issues with logger includes from many packages
51  
52 =head2 new
53  
54 my $ok = Pootle::Client::Filter->new({
55 fullname => qr/^Finnish/,
56 code => qr/^fi/,
57 })->matches($objects);
58  
59 @PARAM1 ARRAYRef of {key => regexp}, {...} -pairs. Key selects the object's attribute to test against, and regexp matches against the attribute value
60 @RETURNS Pootle::Client::Filters
61  
62 =cut
63  
64 sub new($class, @params) {
65 $l->debug("Initializing ".__PACKAGE__." with parameters: ".$l->flatten(@params)) if $l->is_debug();
66 my %self = validate(@params, {
67 filters => { type => HASHREF, optional => 1 },
68 });
69 my $s = bless(\%self, $class);
70  
71 return $s;
72 }
73  
74 =head2 match
75  
76 Apply filters to the given object and return if object matches them
77  
78 @PARAM1 Pootle::Resource::*-object
79 @PARAM2 HASHRef of
80 @RETURNS Boolean, True when all filters match
81  
82 =cut
83  
84 sub match($s, $obj) {
85 return try {
86  
87 foreach my $key (keys %{$s->{filters}}) {
88 my $regexp = $s->{filters}->{$key};
89 unless ($obj->$key() =~ /$regexp/) {
90 return 0;
91 }
92 }
93 return 1;
94  
95 } catch { my $e = $_;
96 die $e;
97 };
98 }
99  
100 sub filter($s, $objects) {
101 my @matches;
102 foreach my $obj (@$objects) {
103 push(@matches, $obj) if $s->match($obj);
104 }
105 return \@matches;
106 }
107  
108 =head2 intersect
109  
110 Given two groups of Pootle::Resource::* -objects, finds an intersection of a given attribute.
111  
112 @RETURNS ARRAYRef of Pootle::Filters::Intersection-objects
113  
114 =cut
115  
116 sub intersect($s, $objects1, $objects2, $objects1Attribute, $objects2Attribute) {
117  
118 my @intersections; #Collect matches here
119  
120 foreach my $obj1 (@$objects1) { #Loop level 1
121 my $attr1s;
122 if (ref($obj1->$objects1Attribute()) eq 'ARRAY') {
123 $attr1s = $obj1->$objects1Attribute();
124 }
125 else {
126 $attr1s = [$obj1->$objects1Attribute()];
127 }
128 foreach my $attr1 (@$attr1s) { #Loop level 2, iterate object1 attributes
129 foreach my $obj2 (@$objects2) { #Loop level 3, iterate the comparable object2s
130 my $attr2s;
131 if (ref($obj2->$objects2Attribute()) eq 'ARRAY') {
132 $attr2s = $obj2->$objects2Attribute();
133 }
134 else {
135 $attr2s = [$obj2->$objects2Attribute()];
136 }
137 foreach my $attr2 (@$attr2s) { #Loop level 4, iterate object2 attributes
138 if ($attr1 eq $attr2) {
139 push(@intersections, new Pootle::Filters::Intersection({
140 obj1 => $obj1,
141 obj2 => $obj2,
142 obj1Attribute => $objects1Attribute,
143 obj2Attribute => $objects2Attribute,
144 attributeValue => $attr1 || $attr2,
145 }));
146 }
147 } #EO loop level 4
148 } #EO loop level 3
149 } #EO loop level 2
150 } #EO loop level 1
151  
152 return \@intersections;
153 }
154  
155 1;