summaryrefslogtreecommitdiff
path: root/lib/MON/Filter.pm
blob: d16d1c507ebb114b5c87e3ace2f87f3bb42caa37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package MON::Filter;

use strict;
use warnings;
use v5.10;
use autodie;

use Data::Dumper;

use MON::Display;
use MON::Config;
use MON::Utils;

our @ISA = ('MON::Display');

sub new {
  my ( $class, %opts ) = @_;

  my $self = bless \%opts, $class;

  $self->init();

  return $self;
}

sub init {
  my ($self) = @_;

  $self->{query_string} = '';
  $self->{filters}      = {};
  $self->{num_filters}  = 0;
  $self->{is_computed}  = 0;
  $self->{or}           = [];

  return undef;
}

# Create filters with params
sub compute {
  my ( $self, $params ) = @_;

  $self->debug( 'Computing filter using', $params );
  return undef if $self->{is_computed};

  my %likes;

  if ( defined $params and ref $params eq 'ARRAY' ) {
    while (@$params) {
      my $op_token = pop @$params;
      given ($op_token) {
        when (/^OP_LIKE$/) {
          my $arg2 = pop @$params;
          my $arg1 = pop @$params;

          if ( exists $likes{$arg1} ) {
            $self->error(
"Can not run multiple 'like's on '$arg1', since it is used for the API query_string"
            );
          }
          else {
            $likes{$arg1} = "$arg1=$arg2";
          }

        }
        when (/^OP_/) {
          $self->{filters}{$_} = [] unless exists $self->{filters}{$_};
          my $arg2 = pop @$params;
          my $arg1 = pop @$params;
          push @{ $self->{filters}{$_} }, [ $arg1, $arg2 ];
          $self->{num_filters}++;
        }
        default {
          $self->error("Inernal error: Operator expected instead of $_");
        }
      }
    }
  }

  $self->{query_string} = '?' . join( '&', values %likes );
  $self->{is_computed} = 1;

  $self->debug( 'Computed filter:', $self->{filters} );
  $self->verbose( "Computed query string is: " . $self->{query_string} );

  return undef;
}

sub filter {
  my ( $self, $objects ) = @_;

  my $config = $self->{config};
  my $json   = $self->{json};

  return $objects unless $self->{num_filters};

  my $num = sub {
    my $str = shift;
    $str =~ s/\D//g;
    $str = 0 if $str eq '';
    return int $str;
  };

  while ( my ( $op, $vals ) = each %{ $self->{filters} } ) {
    for my $val (@$vals) {
      my ( $key, $val ) = @$val;

      @$objects = grep {
        my $object = $_;

        if ( exists $object->{$key} ) {
          if ( $op eq 'OP_MATCHES' and $object->{$key} =~ /$val/ ) {
            1;

          }
          elsif ( $op eq 'OP_NMATCHES' and $object->{$key} !~ /$val/ ) {
            1;

          }
          elsif ( $op eq 'OP_EQ' and $object->{$key} eq $val ) {
            1;

          }
          elsif ( $op eq 'OP_NE' and $object->{$key} ne $val ) {
            1;

          }
          elsif ( $op eq 'OP_LT'
            and $num->( $object->{$key} ) < $num->($val) )
          {
            1;

          }
          elsif ( $op eq 'OP_LE'
            and $num->( $object->{$key} ) <= $num->($val) )
          {
            1;

          }
          elsif ( $op eq 'OP_GT'
            and $num->( $object->{$key} ) > $num->($val) )
          {
            1;

          }
          elsif ( $op eq 'OP_GE'
            and $num->( $object->{$key} ) >= $num->($val) )
          {
            1;

          }
          else {
            0;
          }
        }
        else {
          0;
        }
      } @$objects;
    }
  }

  return $objects;
}

1;