View | Details | Raw Unified | Return to bug 23893
Collapse All | Expand All

(-)a/Koha/Object.pm (-16 / +87 lines)
Lines 368-395 sub to_api { Link Here
368
    my ( $self ) = @_;
368
    my ( $self ) = @_;
369
    my $json_object = $self->TO_JSON;
369
    my $json_object = $self->TO_JSON;
370
370
371
    my $to_api_mapping = $self->to_api_mapping;
372
371
    # Rename attributes if there's a mapping
373
    # Rename attributes if there's a mapping
372
    if ( $self->can('to_api_mapping') ) {
374
    foreach my $column ( keys %{$to_api_mapping} ) {
373
        foreach my $column ( keys %{$self->to_api_mapping} ) {
375
        my $mapped_column = $to_api_mapping->{$column};
374
            my $mapped_column = $self->to_api_mapping->{$column};
376
        if ( exists $json_object->{$column}
375
            if ( exists $json_object->{$column}
377
            && defined $mapped_column )
376
                && defined $mapped_column )
378
        {
377
            {
379
            # key != undef
378
                # key != undef
380
            $json_object->{$mapped_column} = delete $json_object->{$column};
379
                $json_object->{$mapped_column} = delete $json_object->{$column};
381
        }
380
            }
382
        elsif ( exists $json_object->{$column}
381
            elsif ( exists $json_object->{$column}
383
            && !defined $mapped_column )
382
                && !defined $mapped_column )
384
        {
383
            {
385
            # key == undef
384
                # key == undef
386
            delete $json_object->{$column};
385
                delete $json_object->{$column};
386
            }
387
        }
387
        }
388
    }
388
    }
389
389
390
    return $json_object;
390
    return $json_object;
391
}
391
}
392
392
393
=head3 to_api_mapping
394
395
    my $mapping = $object->to_api_mapping;
396
397
Generic method that returns the attribute name mappings required to
398
render the object on the API.
399
400
Note: this only returns an empty I<hashref>. Each class should have its
401
own mapping returned.
402
403
=cut
404
405
sub to_api_mapping {
406
    return {};
407
}
408
409
=head3 from_api_mapping
410
411
    my $mapping = $object->from_api_mapping;
412
413
Generic method that returns the attribute name mappings so the data that
414
comes from the API is correctly renamed to match what is required for the DB.
415
416
=cut
417
418
sub from_api_mapping {
419
    my ( $self ) = @_;
420
421
    my $to_api_mapping = $self->to_api_mapping;
422
423
    unless ( $self->{_from_api_mapping} ) {
424
        while (my ($key, $value) = each %{ $to_api_mapping } ) {
425
            $self->{_from_api_mapping}->{$value} = $key
426
                if defined $value;
427
        }
428
    }
429
430
    return $self->{_from_api_mapping};
431
}
432
393
=head3 $object->unblessed_all_relateds
433
=head3 $object->unblessed_all_relateds
394
434
395
my $everything_into_one_hashref = $object->unblessed_all_relateds
435
my $everything_into_one_hashref = $object->unblessed_all_relateds
Lines 502-507 sub AUTOLOAD { Link Here
502
    return $r;
542
    return $r;
503
}
543
}
504
544
545
=head3 attributes_from_api
546
547
    my $attributes = attributes_from_api( $params );
548
549
Returns the passed params, converted from API naming into the model.
550
551
=cut
552
553
sub attributes_from_api {
554
    my ( $self, $attributes ) = @_;
555
556
    my $mapping = $self->from_api_mapping;
557
558
    foreach my $attribute ( keys %{$mapping} ) {
559
        my $mapped_attribute = $mapping->{$attribute};
560
        if ( exists $attributes->{$attribute}
561
            && defined $mapped_attribute )
562
        {
563
            # key => !undef
564
            $attributes->{$mapped_attribute} = delete $attributes->{$attribute};
565
        }
566
        elsif ( exists $attributes->{$attribute}
567
            && !defined $mapped_attribute )
568
        {
569
            # key => undef / to be deleted
570
            delete $attributes->{$attribute};
571
        }
572
    }
573
574
    return $attributes;
575
}
576
505
=head3 _type
577
=head3 _type
506
578
507
This method must be defined in the child class. The value is the name of the DBIC resultset.
579
This method must be defined in the child class. The value is the name of the DBIC resultset.
508
- 

Return to bug 23893