From b9af3d71b130ed72db899a9191249d9bc9505ec6 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 11 Nov 2019 15:03:28 -0300 Subject: [PATCH] Bug 23893: Implement Koha::Object->from_api_mapping This patch implements the from_api_mapping method, that calculates (and caches) the reserve mapping from the to_api_mapping method. A generic to_api_mapping is added to simplify things a bit in the generic to_api method and so the mappings are reusable in the way they are in from_api_mapping. To test: 1. Apply this patches 2. Run: $ kshell k$ prove t/db_dependent/Koha/Object.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Josef Moravec Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall Signed-off-by: Martin Renvoize --- Koha/Object.pm | 102 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 87 insertions(+), 15 deletions(-) diff --git a/Koha/Object.pm b/Koha/Object.pm index 589ea8c5d8..617c034150 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -384,28 +384,68 @@ sub to_api { my ( $self ) = @_; my $json_object = $self->TO_JSON; + my $to_api_mapping = $self->to_api_mapping; + # Rename attributes if there's a mapping - 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}; - } + 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}; + } + elsif ( exists $json_object->{$column} + && !defined $mapped_column ) + { + # key == undef + delete $json_object->{$column}; } } return $json_object; } +=head3 to_api_mapping + + my $mapping = $object->to_api_mapping; + +Generic method that returns the attribute name mappings required to +render the object on the API. + +Note: this only returns an empty I. Each class should have its +own mapping returned. + +=cut + +sub to_api_mapping { + return {}; +} + +=head3 from_api_mapping + + my $mapping = $object->from_api_mapping; + +Generic method that returns the attribute name mappings so the data that +comes from the API is correctly renamed to match what is required for the DB. + +=cut + +sub from_api_mapping { + my ( $self ) = @_; + + my $to_api_mapping = $self->to_api_mapping; + + unless ( $self->{_from_api_mapping} ) { + while (my ($key, $value) = each %{ $to_api_mapping } ) { + $self->{_from_api_mapping}->{$value} = $key + if defined $value; + } + } + + return $self->{_from_api_mapping}; +} + =head3 $object->unblessed_all_relateds my $everything_into_one_hashref = $object->unblessed_all_relateds @@ -518,6 +558,38 @@ sub AUTOLOAD { return $r; } +=head3 attributes_from_api + + my $attributes = attributes_from_api( $params ); + +Returns the passed params, converted from API naming into the model. + +=cut + +sub attributes_from_api { + my ( $self, $attributes ) = @_; + + my $mapping = $self->from_api_mapping; + + foreach my $attribute ( keys %{$mapping} ) { + my $mapped_attribute = $mapping->{$attribute}; + if ( exists $attributes->{$attribute} + && defined $mapped_attribute ) + { + # key => !undef + $attributes->{$mapped_attribute} = delete $attributes->{$attribute}; + } + elsif ( exists $attributes->{$attribute} + && !defined $mapped_attribute ) + { + # key => undef / to be deleted + delete $attributes->{$attribute}; + } + } + + return $attributes; +} + =head3 _type This method must be defined in the child class. The value is the name of the DBIC resultset. -- 2.39.5