Serializer.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Part of XML_XRD
  4. *
  5. * PHP version 5
  6. *
  7. * @category XML
  8. * @package XML_XRD
  9. * @author Christian Weiske <cweiske@php.net>
  10. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  11. * @link http://pear.php.net/package/XML_XRD
  12. */
  13. require_once 'XML/XRD/Serializer/Exception.php';
  14. /**
  15. * Serialization dispatcher - loads the correct serializer for saving XRD data.
  16. *
  17. * @category XML
  18. * @package XML_XRD
  19. * @author Christian Weiske <cweiske@php.net>
  20. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  21. * @version Release: @package_version@
  22. * @link http://pear.php.net/package/XML_XRD
  23. */
  24. class XML_XRD_Serializer
  25. {
  26. /**
  27. * XRD data storage
  28. *
  29. * @var XML_XRD
  30. */
  31. protected $xrd;
  32. /**
  33. * Init object with xrd object
  34. *
  35. * @param XML_XRD $xrd Data storage the data are fetched from
  36. */
  37. public function __construct(XML_XRD $xrd)
  38. {
  39. $this->xrd = $xrd;
  40. }
  41. /**
  42. * Convert the XRD data into a string of the given type
  43. *
  44. * @param string $type File type: xml or json
  45. *
  46. * @return string Serialized data
  47. */
  48. public function to($type)
  49. {
  50. return (string)$this->getSerializer($type);
  51. }
  52. /**
  53. * Creates a XRD loader object for the given type
  54. *
  55. * @param string $type File type: xml or json
  56. *
  57. * @return XML_XRD_Loader
  58. */
  59. protected function getSerializer($type)
  60. {
  61. $class = 'XML_XRD_Serializer_' . strtoupper($type);
  62. $file = str_replace('_', '/', $class) . '.php';
  63. include_once $file;
  64. if (class_exists($class)) {
  65. return new $class($this->xrd);
  66. }
  67. throw new XML_XRD_Serializer_Exception(
  68. 'No serializer for type "' . $type . '"',
  69. XML_XRD_Loader_Exception::NO_LOADER
  70. );
  71. }
  72. }
  73. ?>