summaryrefslogtreecommitdiff
path: root/LedgerSMB/RESTXML/Document/Base.pm
blob: e8feaa96cf7d0fac6a8bdf3a689b158053ae4ce2 (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}->unsupported('the DELETE method is not implemented.');
  18. }
  19. sub handle_get {
  20. my ($self, $args) = @_;
  21. return $self->{handler}->unsupported('the GET method is not implemented.');
  22. }
  23. =head3 hash_to_twig
  24. Convinenve function to convert a hashref to a XML::Twig structure.
  25. passed a hashref, required arguments:
  26. hash - the hash to convert
  27. name - the name of the root element.
  28. optional arguments:
  29. sort - by default, on set to 0 to disable. toggles whether or not hash keys are sorted
  30. in the resulting xml node created. Disabling this may save some performance if converting a lot of
  31. nodes at once.
  32. =cut
  33. sub hash_to_twig {
  34. my ($self, $args) = @_;
  35. my $hash = $args->{hash} || croak "Need a hash to convert to use hash_to_twig";
  36. my $name = $args->{name} || croak "Need a root element name to use hash_to_twig";
  37. my @keyorder = keys %$hash;
  38. @keyorder = sort @keyorder unless defined($args->{sort}) and $args->{sort} == 0;
  39. return XML::Twig::Elt->new($name,$args->{root_attr}||{}, map {
  40. XML::Twig::Elt->new($_, {'#CDATA'=>1}, $hash->{$_})
  41. } @keyorder );
  42. }
  43. 1;