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