Bug 30477: Add new UNIMARC installer translation files
[koha.git] / Koha / Result / Boolean.pm
1 package Koha::Result::Boolean;
2
3 # Copyright ByWater Solutions 2021
4 # Copyright Theke Solutions   2021
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22
23 use overload
24    bool => \&as_bool,
25    '==' => \&equals;
26
27 use Koha::Object::Message;
28
29 =head1 NAME
30
31 Koha::Result::Boolean - Booleans, with extra Koha
32
33 =head1 API
34
35 =head2 Class methods
36
37 =head3 new
38
39     my $bool = Koha::Result::Boolean->new( $value );
40
41 Constructor method to generate a Koha::Result::Boolean object. I<value> is
42 a boolean expression.
43
44 =cut
45
46 sub new {
47     my ( $class, $value ) = @_;
48
49     $value //= 1; # default to true
50     $value = ($value) ? 1 : 0;
51
52     my $self = {
53         value     => $value,
54         _messages => [],
55     };
56
57     return bless ( $self, $class );
58 }
59
60 =head3 set_value
61
62     $bool->set_value(1);
63     $bool->set_value(0);
64
65 Set the boolean value for the object.
66
67 =cut
68
69 sub set_value {
70     my ( $self, $value ) = @_;
71
72     $self->{value} = ($value) ? 1 : 0;
73
74     return $self;
75 }
76
77 =head3 messages
78
79     my @messages = @{ $bool->messages };
80
81 Returns the I<Koha::Object::Message> objects that were recorded.
82
83 =cut
84
85 sub messages {
86     my ( $self ) = @_;
87
88     $self->{_messages} = []
89         unless defined $self->{_messages};
90
91     return $self->{_messages};
92 }
93
94 =head3 add_message
95
96     $bool->add_message(
97         {
98             message => $message,
99           [ type    => 'error',
100             payload => $payload ]
101         }
102     );
103
104 Adds a message.
105
106 =cut
107
108 sub add_message {
109     my ( $self, $params ) = @_;
110
111     push @{ $self->{_messages} }, Koha::Object::Message->new($params);
112
113     return $self;
114 }
115
116 =head2 Internal methods
117
118 =head3 as_bool
119
120 Internal method that exposes the boolean value of the object as a scalar.
121
122 =cut
123
124 sub as_bool {
125     my ($self) = @_;
126
127     return $self->{value} + 0;
128 }
129
130 =head3 equals
131
132 Internal method implementing equality comparison in scalar context.
133
134 =cut
135
136 sub equals {
137     my ( $first, $second, $flipped ) = @_;
138
139     return ($flipped)
140       ? $first == $second->as_bool
141       : $first->as_bool == $second;
142 }
143
144 =head1 AUTHORS
145
146 Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt>
147
148 =cut
149
150 1;