Condition.pm 1.79 KB
Newer Older
1 2 3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
#
5 6
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
7 8

package Bugzilla::Search::Condition;
9 10

use 5.10.1;
11
use strict;
12
use warnings;
13

14
use base qw(Exporter);
15 16 17
our @EXPORT_OK = qw(condition);

sub new {
18 19 20 21
  my ($class, $params) = @_;
  my %self = %$params;
  bless \%self, $class;
  return \%self;
22 23
}

24 25
sub field { return $_[0]->{field} }
sub value { return $_[0]->{value} }
26

27
sub operator {
28 29 30 31 32
  my ($self, $value) = @_;
  if (@_ == 2) {
    $self->{operator} = $value;
  }
  return $self->{operator};
33 34
}

35
sub fov {
36 37
  my ($self) = @_;
  return ($self->field, $self->operator, $self->value);
38 39 40
}

sub translated {
41 42 43 44 45
  my ($self, $params) = @_;
  if (@_ == 2) {
    $self->{translated} = $params;
  }
  return $self->{translated};
46 47 48
}

sub as_string {
49 50 51 52
  my ($self) = @_;
  my $term = $self->translated->{term};
  $term = "NOT( $term )" if $term && $self->negate;
  return $term;
53 54
}

55
sub as_params {
56 57 58 59 60 61 62
  my ($self) = @_;
  return {
    f => $self->field,
    o => $self->operator,
    v => $self->value,
    n => scalar $self->negate
  };
63 64
}

65
sub negate {
66 67 68 69 70
  my ($self, $value) = @_;
  if (@_ == 2) {
    $self->{negate} = $value ? 1 : 0;
  }
  return $self->{negate};
71 72 73 74 75 76 77
}

###########################
# Convenience Subroutines #
###########################

sub condition {
78 79 80
  my ($field, $operator, $value) = @_;
  return __PACKAGE__->new(
    {field => $field, operator => $operator, value => $value});
81 82
}

83
1;
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

=head1 B<Methods in need of POD>

=over

=item as_string

=item fov

=item value

=item negate

=item translated

=item operator

=item as_params

=item condition

=item field

=back