Bug 29746: Add Koha::Result::Boolean
[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 bool => \&as_bool;
24
25 use Koha::Object::Message;
26
27 =head1 NAME
28
29 Koha::Result::Boolean - Booleans, with extra Koha
30
31 =head1 API
32
33 =head2 Class methods
34
35 =head3 new
36
37     my $bool = Koha::Result::Boolean->new( $value );
38
39 Constructor method to generate a Koha::Result::Boolean object. I<value> is
40 a boolean expression.
41
42 =cut
43
44 sub new {
45     my ( $class, $value ) = @_;
46
47     $value //= 1; # default to true
48     $value = ($value) ? 1 : 0;
49
50     my $self = {
51         value     => $value,
52         _messages => [],
53     };
54
55     return bless ( $self, $class );
56 }
57
58 =head3 set_value
59
60     $bool->set_value(1);
61     $bool->set_value(0);
62
63 Set the boolean value for the object.
64
65 =cut
66
67 sub set_value {
68     my ( $self, $value ) = @_;
69
70     $self->{value} = ($value) ? 1 : 0;
71
72     return $self;
73 }
74
75 =head3 messages
76
77     my @messages = @{ $bool->messages };
78
79 Returns the I<Koha::Object::Message> objects that were recorded.
80
81 =cut
82
83 sub messages {
84     my ( $self ) = @_;
85
86     $self->{_messages} = []
87         unless defined $self->{_messages};
88
89     return $self->{_messages};
90 }
91
92 =head3 add_message
93
94     $bool->add_message(
95         {
96             message => $message,
97           [ type    => 'error',
98             payload => $payload ]
99         }
100     );
101
102 Adds a message.
103
104 =cut
105
106 sub add_message {
107     my ( $self, $params ) = @_;
108
109     push @{ $self->{_messages} }, Koha::Object::Message->new($params);
110
111     return $self;
112 }
113
114 =head2 Internal methods
115
116 =head3 as_bool
117
118 Internal method that exposes the boolean value of the object
119
120 =cut
121
122 sub as_bool {
123     my ($self) = @_;
124
125     return $self->{value};
126 }
127
128 =head1 AUTHORS
129
130 Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt>
131
132 =cut
133
134 1;