[Pkg-owncloud-commits] [php-sabre-vobject] 187/341: Writer for MimeDir and XML support generators.

David Prévot taffit at moszumanska.debian.org
Tue Aug 11 13:35:46 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository php-sabre-vobject.

commit e6a904eac290c167bdc1cd1bbf7f25e50045daa9
Author: Ivan Enderlin <ivan.enderlin at hoa-project.net>
Date:   Thu Jan 22 11:26:06 2015 +0100

    Writer for MimeDir and XML support generators.
---
 lib/Writer.php                   | 74 +++++++++++++++++++++++++++++++---------
 tests/VObject/Parser/XmlTest.php | 31 +++++++++++++++++
 2 files changed, 88 insertions(+), 17 deletions(-)

diff --git a/lib/Writer.php b/lib/Writer.php
index db1a14b..08c85ad 100644
--- a/lib/Writer.php
+++ b/lib/Writer.php
@@ -19,12 +19,27 @@ class Writer {
     /**
      * Serializes a vCard or iCalendar object.
      *
-     * @param Component $component
+     * @param Component|Iterator $component
      * @return string
      */
-    static function write(Component $component) {
+    static function write($component) {
+
+        if($component instanceof Component) {
+            return $component->serialize();
+        } elseif($component instanceof \Iterator) {
+
+            $out = null;
+            $iterator = $component;
 
-        return $component->serialize();
+            foreach ($iterator as $component) {
+                $out .= $component->serialize();
+            }
+
+            return $out;
+
+        } else {
+            throw new \InvalidArgumentException('Need a ' . __NAMESPACE__ .  '\Component object or an iterator');
+        }
 
     }
 
@@ -44,31 +59,56 @@ class Writer {
     /**
      * Serializes a xCal or xCard object.
      *
-     * @param Component $component
+     * @param Component|Iterator $component
      * @return string
      */
-    static public function writeXml(Component $component) {
+    static public function writeXml($component) {
 
         $writer = new Xml\Writer();
         $writer->openMemory();
-        $writer->startDocument('1.0', 'utf-8');
+        $writer->setIndent(true);
 
-        if($component instanceof Component\VCalendar) {
+        $startDocument = function($component) use($writer) {
 
-            $writer->startElement('icalendar');
-            $writer->writeAttribute('xmlns', Parser\Xml::XCAL_NAMESPACE);
-        }
-        else {
+            $writer->startDocument('1.0', 'utf-8');
 
-            $writer->startElement('vcards');
-            $writer->writeAttribute('xmlns', Parser\Xml::XCARD_NAMESPACE);
-        }
+            if ($component instanceof Component\VCalendar) {
 
-        $writer->setIndent(true);
+                $writer->startElement('icalendar');
+                $writer->writeAttribute('xmlns', Parser\Xml::XCAL_NAMESPACE);
 
-        $component->xmlSerialize($writer);
+            } else {
 
-        $writer->endElement();
+                $writer->startElement('vcards');
+                $writer->writeAttribute('xmlns', Parser\Xml::XCARD_NAMESPACE);
+
+            }
+
+        };
+
+        $endDocument = function() use($writer) {
+            $writer->endElement();
+        };
+
+        if($component instanceof Component) {
+
+            $startDocument($component);
+            $component->xmlSerialize($writer);
+            $endDocument();
+
+        } elseif($component instanceof \Iterator) {
+
+            $iterator = $component;
+
+            foreach ($iterator as $component) {
+                $startDocument($component);
+                $component->xmlSerialize($writer);
+                $endDocument();
+            }
+
+        } else {
+            throw new \InvalidArgumentException('Need a ' . __NAMESPACE__ .  '\Component object or an iterator');
+        }
 
         return $writer->outputMemory();
 
diff --git a/tests/VObject/Parser/XmlTest.php b/tests/VObject/Parser/XmlTest.php
index 870c373..6f5b9bd 100644
--- a/tests/VObject/Parser/XmlTest.php
+++ b/tests/VObject/Parser/XmlTest.php
@@ -1151,6 +1151,37 @@ XML
 
     }
 
+    function testRFC6351MultipleVCard() {
+
+        $this->assertXMLEqualsToMimeDir(
+<<<XML
+<?xml version="1.0" encoding="UTF-8"?>
+<vcards xmlns="urn:ietf:params:xml:ns:vcard-4.0">
+ <vcard>
+  <fn>
+   <text>J. Doe</text>
+  </fn>
+ </vcard>
+ <vcard>
+  <fn>
+   <text>G. Freeman</text>
+  </fn>
+ </vcard>
+</vcards>
+XML
+,
+            'BEGIN:VCARD' . CRLF .
+            'VERSION:4.0' . CRLF .
+            'FN:J. Doe' . CRLF .
+            'END:VCARD' . CRLF .
+            'BEGIN:VCARD' . CRLF .
+            'VERSION:4.0' . CRLF .
+            'FN:G. Freeman' . CRLF .
+            'END:VCARD' . CRLF
+        );
+
+    }
+
     /**
      * Check this equality:
      *     XML -> object model -> MIME Dir.

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabre-vobject.git



More information about the Pkg-owncloud-commits mailing list