Writer.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace Sabre\VObject;
  3. use Sabre\Xml;
  4. /**
  5. * iCalendar/vCard/jCal/jCard/xCal/xCard writer object.
  6. *
  7. * This object provides a few (static) convenience methods to quickly access
  8. * the serializers.
  9. *
  10. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  11. * @author Ivan Enderlin
  12. * @license http://sabre.io/license/ Modified BSD License
  13. */
  14. class Writer
  15. {
  16. /**
  17. * Serializes a vCard or iCalendar object.
  18. *
  19. * @return string
  20. */
  21. public static function write(Component $component)
  22. {
  23. return $component->serialize();
  24. }
  25. /**
  26. * Serializes a jCal or jCard object.
  27. *
  28. * @param int $options
  29. *
  30. * @return string
  31. */
  32. public static function writeJson(Component $component, $options = 0)
  33. {
  34. return json_encode($component, $options);
  35. }
  36. /**
  37. * Serializes a xCal or xCard object.
  38. *
  39. * @return string
  40. */
  41. public static function writeXml(Component $component)
  42. {
  43. $writer = new Xml\Writer();
  44. $writer->openMemory();
  45. $writer->setIndent(true);
  46. $writer->startDocument('1.0', 'utf-8');
  47. if ($component instanceof Component\VCalendar) {
  48. $writer->startElement('icalendar');
  49. $writer->writeAttribute('xmlns', Parser\XML::XCAL_NAMESPACE);
  50. } else {
  51. $writer->startElement('vcards');
  52. $writer->writeAttribute('xmlns', Parser\XML::XCARD_NAMESPACE);
  53. }
  54. $component->xmlSerialize($writer);
  55. $writer->endElement();
  56. return $writer->outputMemory();
  57. }
  58. }