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