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

(-)a/Koha/Object.pm (-33 / +58 lines)
Lines 445-450 sub from_api_mapping { Link Here
445
    return $self->{_from_api_mapping};
445
    return $self->{_from_api_mapping};
446
}
446
}
447
447
448
=head3 new_from_api
449
450
    my $object = Koha::Object->new_from_api;
451
    my $object = Koha::Object->new_from_api( $attrs );
452
453
Creates a new object, mapping the API attribute names to the ones on the DB schema.
454
455
=cut
456
457
sub new_from_api {
458
    my ( $class, $params ) = @_;
459
460
    my $self = $class->new;
461
    return $self->set_from_api( $params );
462
}
463
464
=head3 set_from_api
465
466
    my $object = Koha::Object->new(...);
467
    $object->set_from_api( $attrs )
468
469
Sets the object's attributes mapping API attribute names to the ones on the DB schema.
470
471
=cut
472
473
sub set_from_api {
474
    my ( $self, $from_api_params ) = @_;
475
476
    return $self->set( $self->attributes_from_api( $from_api_params ) );
477
}
478
479
=head3 attributes_from_api
480
481
    my $attributes = attributes_from_api( $params );
482
483
Returns the passed params, converted from API naming into the model.
484
485
=cut
486
487
sub attributes_from_api {
488
    my ( $self, $from_api_params ) = @_;
489
490
    my $from_api_mapping = $self->from_api_mapping;
491
492
    my $params;
493
494
    while (my ($key, $value) = each %{ $from_api_params } ) {
495
        if ( exists $from_api_mapping->{$key} ) {
496
            $params->{$from_api_mapping->{$key}} = $value;
497
        }
498
        else {
499
            $params->{$key} = $value;
500
        }
501
    }
502
503
    return $params;
504
}
505
448
=head3 $object->unblessed_all_relateds
506
=head3 $object->unblessed_all_relateds
449
507
450
my $everything_into_one_hashref = $object->unblessed_all_relateds
508
my $everything_into_one_hashref = $object->unblessed_all_relateds
Lines 557-594 sub AUTOLOAD { Link Here
557
    return $r;
615
    return $r;
558
}
616
}
559
617
560
=head3 attributes_from_api
561
562
    my $attributes = attributes_from_api( $params );
563
564
Returns the passed params, converted from API naming into the model.
565
566
=cut
567
568
sub attributes_from_api {
569
    my ( $self, $attributes ) = @_;
570
571
    my $mapping = $self->from_api_mapping;
572
573
    foreach my $attribute ( keys %{$mapping} ) {
574
        my $mapped_attribute = $mapping->{$attribute};
575
        if ( exists $attributes->{$attribute}
576
            && defined $mapped_attribute )
577
        {
578
            # key => !undef
579
            $attributes->{$mapped_attribute} = delete $attributes->{$attribute};
580
        }
581
        elsif ( exists $attributes->{$attribute}
582
            && !defined $mapped_attribute )
583
        {
584
            # key => undef / to be deleted
585
            delete $attributes->{$attribute};
586
        }
587
    }
588
589
    return $attributes;
590
}
591
592
=head3 _type
618
=head3 _type
593
619
594
This method must be defined in the child class. The value is the name of the DBIC resultset.
620
This method must be defined in the child class. The value is the name of the DBIC resultset.
595
- 

Return to bug 23893