Line 0
Link Here
|
0 |
- |
1 |
package Koha::Object::JSONFields; |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it |
6 |
# under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 3 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use JSON; |
21 |
use Try::Tiny; |
22 |
|
23 |
use Koha::Exceptions; |
24 |
use Koha::Exceptions::Object; |
25 |
|
26 |
=head1 NAME |
27 |
|
28 |
Koha::Object::JSONFields - Class that adds JSON field manipulation helper methods |
29 |
for Koha::Object-derived classes |
30 |
|
31 |
=head1 SYNOPSIS |
32 |
|
33 |
use base qw(Koha::Object Koha::Object::JSONFields); |
34 |
my $object = Koha::Object->new({ property1 => $property1, property2 => $property2, etc... } ); |
35 |
my $field_name_hashref = $object->decode_json_field({ field => 'field_name' }); |
36 |
$object->encode_json_field({ field => 'field_name', data => $data }); |
37 |
|
38 |
=head1 API |
39 |
|
40 |
=head2 Class methods |
41 |
|
42 |
=head3 decode_json_field |
43 |
|
44 |
my $hashref = $object->decode_json_field({ field => 'field_name' }); |
45 |
|
46 |
=cut |
47 |
|
48 |
sub decode_json_field { |
49 |
my ( $self, $params ) = @_; |
50 |
|
51 |
Koha::Exceptions::MissingParameter->throw( parameter => 'field' ) |
52 |
unless defined $params->{field}; |
53 |
|
54 |
my $field = $params->{field}; |
55 |
|
56 |
return try { |
57 |
$self->$field ? $self->_json->decode( $self->$field ) : undef; |
58 |
} |
59 |
catch { |
60 |
Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_"); |
61 |
}; |
62 |
} |
63 |
|
64 |
=head3 set_encoded_json_field |
65 |
|
66 |
=cut |
67 |
|
68 |
sub set_encoded_json_field { |
69 |
my ( $self, $params ) = @_; |
70 |
|
71 |
Koha::Exceptions::MissingParameter->throw( parameter => 'field' ) |
72 |
unless defined $params->{field}; |
73 |
|
74 |
Koha::Exceptions::MissingParameter->throw( parameter => 'data' ) |
75 |
unless exists $params->{data}; |
76 |
|
77 |
my $field = $params->{field}; |
78 |
my $data = $params->{data}; |
79 |
|
80 |
return try { |
81 |
$self->$field( $data ? $self->_json->encode($data) : undef ); |
82 |
} |
83 |
catch { |
84 |
Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_"); |
85 |
}; |
86 |
} |
87 |
|
88 |
=head2 Internal methods |
89 |
|
90 |
=head3 _json |
91 |
|
92 |
my $JSON_object = $self->_json; |
93 |
|
94 |
Returns a JSON object with utf8 disabled. Encoding to UTF-8 should be |
95 |
done later. |
96 |
|
97 |
=cut |
98 |
|
99 |
sub _json { |
100 |
my ($self) = @_; |
101 |
$self->{_json} //= JSON->new->utf8(0); |
102 |
return $self->{_json}; |
103 |
} |
104 |
|
105 |
=head1 AUTHOR |
106 |
|
107 |
Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt> |
108 |
|
109 |
=cut |
110 |
|
111 |
1; |