summaryrefslogtreecommitdiff
path: root/LedgerSMB/RESTXML/Document/Base.pm
blob: 986b4148f8bb14d6f7db275b1a63e943114ebd58 (plain)
  1. package LedgerSMB::RESTXML::Document::Base;
  2. use strict;
  3. use warnings;
  4. use XML::Twig;
  5. use LedgerSMB::Log;
  6. use Carp;
  7. sub handle_post {
  8. my ( $self, $args ) = @_;
  9. return $args->{handler}->unsupported('the POST method is not implemented.');
  10. }
  11. sub handle_put {
  12. my ( $self, $args ) = @_;
  13. return $self->{handler}->unsupported('the PUT method is not implemented.');
  14. }
  15. sub handle_delete {
  16. my ( $self, $args ) = @_;
  17. return $self->{handler}
  18. ->unsupported('the DELETE method is not implemented.');
  19. }
  20. sub handle_get {
  21. my ( $self, $args ) = @_;
  22. return $self->{handler}->unsupported('the GET method is not implemented.');
  23. }
  24. =head3 hash_to_twig
  25. Convinenve function to convert a hashref to a XML::Twig structure.
  26. passed a hashref, required arguments:
  27. hash - the hash to convert
  28. name - the name of the root element.
  29. optional arguments:
  30. sort - by default, on set to 0 to disable. toggles whether or not hash keys are sorted
  31. in the resulting xml node created. Disabling this may save some performance if converting a lot of
  32. nodes at once.
  33. =cut
  34. sub hash_to_twig {
  35. my ( $self, $args ) = @_;
  36. my $hash = $args->{hash}
  37. || croak "Need a hash to convert to use hash_to_twig";
  38. my $name = $args->{name}
  39. || croak "Need a root element name to use hash_to_twig";
  40. my @keyorder = keys %$hash;
  41. @keyorder = sort @keyorder
  42. unless defined( $args->{sort} )
  43. and $args->{sort} == 0;
  44. return XML::Twig::Elt->new(
  45. $name,
  46. $args->{root_attr} || {},
  47. map { XML::Twig::Elt->new( $_, { '#CDATA' => 1 }, $hash->{$_} ) }
  48. @keyorder
  49. );
  50. }
  51. 1;