Condition.pm 1.89 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 parent qw(Exporter);
15 16 17 18 19 20 21 22 23 24 25 26
our @EXPORT_OK = qw(condition);

sub new {
    my ($class, $params) = @_;
    my %self = %$params;
    bless \%self, $class;
    return \%self;
}

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

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

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
sub fov {
    my ($self) = @_;
    return ($self->field, $self->operator, $self->value);
}

sub translated {
    my ($self, $params) = @_;
    if (@_ == 2) {
        $self->{translated} = $params;
    }
    return $self->{translated};
}

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

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

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

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

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

79
1;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

=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