use Carp;
use Mojo::JSON;
-use Scalar::Util qw( looks_like_number );
+use Scalar::Util qw( blessed looks_like_number );
use Try::Tiny;
use Koha::Database;
my @columns = @{$self->_columns()};
foreach my $p ( keys %$properties ) {
- unless ( grep {/^$p$/} @columns ) {
+ unless ( grep { $_ eq $p } @columns ) {
Koha::Exceptions::Object::PropertyNotFound->throw( "No property $p for " . ref($self) );
}
}
sub get_from_storage {
my ( $self, $attrs ) = @_;
my $stored_object = $self->_result->get_from_storage($attrs);
+ return unless $stored_object;
my $object_class = Koha::Object::_get_object_class( $self->_result->result_class );
return $object_class->_new_from_dbic($stored_object);
}
=head3 to_api
- my $object_for_api = $object->to_api;
+ my $object_for_api = $object->to_api(
+ {
+ [ embed => {
+ items => {
+ children => {
+ holds => {,
+ children => {
+ ...
+ }
+ }
+ }
+ },
+ library => {
+ ...
+ }
+ },
+ ...
+ ]
+ }
+ );
Returns a representation of the object, suitable for API output.
=cut
sub to_api {
- my ( $self ) = @_;
+ my ( $self, $params ) = @_;
my $json_object = $self->TO_JSON;
my $to_api_mapping = $self->to_api_mapping;
# Rename attributes if there's a mapping
- foreach my $column ( keys %{$to_api_mapping} ) {
- my $mapped_column = $to_api_mapping->{$column};
- if ( exists $json_object->{$column}
- && defined $mapped_column )
- {
- # key != undef
- $json_object->{$mapped_column} = delete $json_object->{$column};
+ if ( $self->can('to_api_mapping') ) {
+ foreach my $column ( keys %{ $self->to_api_mapping } ) {
+ my $mapped_column = $self->to_api_mapping->{$column};
+ if ( exists $json_object->{$column}
+ && defined $mapped_column )
+ {
+ # key != undef
+ $json_object->{$mapped_column} = delete $json_object->{$column};
+ }
+ elsif ( exists $json_object->{$column}
+ && !defined $mapped_column )
+ {
+ # key == undef
+ delete $json_object->{$column};
+ }
}
- elsif ( exists $json_object->{$column}
- && !defined $mapped_column )
- {
- # key == undef
- delete $json_object->{$column};
+ }
+
+ my $embeds = $params->{embed};
+
+ if ($embeds) {
+ foreach my $embed ( keys %{$embeds} ) {
+ if ( $embed =~ m/^(?<relation>.*)_count$/
+ and $embeds->{$embed}->{is_count} ) {
+
+ my $relation = $+{relation};
+ $json_object->{$embed} = $self->$relation->count;
+ }
+ else {
+ my $curr = $embed;
+ my $next = $embeds->{$curr}->{children};
+
+ my $children = $self->$curr;
+
+ if ( defined $children and ref($children) eq 'ARRAY' ) {
+ my @list = map {
+ $self->_handle_to_api_child(
+ { child => $_, next => $next, curr => $curr } )
+ } @{$children};
+ $json_object->{$curr} = \@list;
+ }
+ else {
+ $json_object->{$curr} = $self->_handle_to_api_child(
+ { child => $children, next => $next, curr => $curr } );
+ }
+ }
}
}
+
+
return $json_object;
}
my $from_api_mapping = $self->from_api_mapping;
my $params;
+ my $columns_info = $self->_result->result_source->columns_info;
while (my ($key, $value) = each %{ $from_api_params } ) {
- if ( exists $from_api_mapping->{$key} ) {
- $params->{$from_api_mapping->{$key}} = $value;
+ my $koha_field_name =
+ exists $from_api_mapping->{$key}
+ ? $from_api_mapping->{$key}
+ : $key;
+
+ if ( $columns_info->{$koha_field_name}->{is_boolean} ) {
+ # TODO: Remove when D8 is formally deprecated
+ # Handle booleans gracefully
+ $value = ( $value ) ? 1 : 0;
}
- else {
- $params->{$key} = $value;
+ elsif ( _date_or_datetime_column_type( $columns_info->{$koha_field_name}->{data_type} ) ) {
+ try {
+ $value = dt_from_string($value, 'rfc3339');
+ }
+ catch {
+ Koha::Exceptions::BadParameter->throw( parameter => $key );
+ };
}
+
+ $params->{$koha_field_name} = $value;
}
return $params;
my @columns = @{$self->_columns()};
# Using direct setter/getter like $item->barcode() or $item->barcode($barcode);
- if ( grep {/^$method$/} @columns ) {
+ if ( grep { $_ eq $method } @columns ) {
if ( @_ ) {
$self->_result()->set_column( $method, @_ );
return $self;
Koha::Exceptions::Object::MethodNotCoveredByTests->throw(
error => sprintf("The method %s->%s is not covered by tests!", ref($self), $method),
show_trace => 1
- ) unless grep { /^$method$/ } @known_methods;
+ ) unless grep { $_ eq $method } @known_methods;
my $r = eval { $self->_result->$method(@_) };
sub _type { }
+=head3 _handle_to_api_child
+
+=cut
+
+sub _handle_to_api_child {
+ my ($self, $args ) = @_;
+
+ my $child = $args->{child};
+ my $next = $args->{next};
+ my $curr = $args->{curr};
+
+ my $res;
+
+ if ( defined $child ) {
+
+ Koha::Exceptions::Exception->throw( "Asked to embed $curr but its return value doesn't implement to_api" )
+ if defined $next and blessed $child and !$child->can('to_api');
+
+ if ( blessed $child ) {
+ $res = $child->to_api({ embed => $next });
+ }
+ else {
+ $res = $child;
+ }
+ }
+
+ return $res;
+}
+
sub DESTROY { }
=head1 AUTHOR