[Pkg-owncloud-commits] [php-sabre-vobject] 196/341: Remove generators. Assume we compute only 1 vcard.

David Prévot taffit at moszumanska.debian.org
Tue Aug 11 13:35:47 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 792f46951525d2be48e8afdd0218eb1f5ad312dd
Author: Ivan Enderlin <ivan.enderlin at hoa-project.net>
Date:   Mon Jan 26 11:01:35 2015 +0100

    Remove generators. Assume we compute only 1 vcard.
---
 lib/Parser/XML.php               |  9 +++---
 lib/Writer.php                   | 69 +++++++++-------------------------------
 tests/VObject/Parser/XmlTest.php | 31 ------------------
 tests/VObject/ReaderTest.php     |  4 +--
 tests/VObject/WriterTest.php     | 36 ---------------------
 5 files changed, 21 insertions(+), 128 deletions(-)

diff --git a/lib/Parser/XML.php b/lib/Parser/XML.php
index 687c15d..10805f5 100644
--- a/lib/Parser/XML.php
+++ b/lib/Parser/XML.php
@@ -70,7 +70,7 @@ class XML extends Parser {
      * @param resource|string $input
      * @param int $options
      * @throws \Exception
-     * @return Generator of Sabre\VObject\Document
+     * @return Sabre\VObject\Document
      */
     public function parse($input = null, $options = 0) {
 
@@ -92,8 +92,6 @@ class XML extends Parser {
                 $this->root = new VCalendar([], false);
                 $this->pointer = &$this->input['value'][0];
                 $this->parseVCalendarComponents($this->root);
-
-                yield $this->root;
                 break;
 
             case '{' . self::XCARD_NAMESPACE . '}vcards':
@@ -103,7 +101,8 @@ class XML extends Parser {
                     $this->pointer  = &$vCard;
                     $this->parseVCardComponents($this->root);
 
-                    yield $this->root;
+                    // We just parse the first <vcard /> element.
+                    break;
 
                 }
                 break;
@@ -113,7 +112,7 @@ class XML extends Parser {
 
         }
 
-        return;
+        return $this->root;
     }
 
     /**
diff --git a/lib/Writer.php b/lib/Writer.php
index 08c85ad..6e9006c 100644
--- a/lib/Writer.php
+++ b/lib/Writer.php
@@ -19,27 +19,12 @@ class Writer {
     /**
      * Serializes a vCard or iCalendar object.
      *
-     * @param Component|Iterator $component
+     * @param Component $component
      * @return string
      */
-    static function write($component) {
-
-        if($component instanceof Component) {
-            return $component->serialize();
-        } elseif($component instanceof \Iterator) {
-
-            $out = null;
-            $iterator = $component;
-
-            foreach ($iterator as $component) {
-                $out .= $component->serialize();
-            }
-
-            return $out;
+    static function write(Component $component) {
 
-        } else {
-            throw new \InvalidArgumentException('Need a ' . __NAMESPACE__ .  '\Component object or an iterator');
-        }
+        return $component->serialize();
 
     }
 
@@ -59,56 +44,32 @@ class Writer {
     /**
      * Serializes a xCal or xCard object.
      *
-     * @param Component|Iterator $component
+     * @param Component $component
      * @return string
      */
-    static public function writeXml($component) {
+    static public function writeXml(Component $component) {
 
         $writer = new Xml\Writer();
         $writer->openMemory();
         $writer->setIndent(true);
 
-        $startDocument = function($component) use($writer) {
-
-            $writer->startDocument('1.0', 'utf-8');
-
-            if ($component instanceof Component\VCalendar) {
+        $writer->startDocument('1.0', 'utf-8');
 
-                $writer->startElement('icalendar');
-                $writer->writeAttribute('xmlns', Parser\Xml::XCAL_NAMESPACE);
+        if ($component instanceof Component\VCalendar) {
 
-            } else {
+            $writer->startElement('icalendar');
+            $writer->writeAttribute('xmlns', Parser\Xml::XCAL_NAMESPACE);
 
-                $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();
+        } else {
 
-        } elseif($component instanceof \Iterator) {
+            $writer->startElement('vcards');
+            $writer->writeAttribute('xmlns', Parser\Xml::XCARD_NAMESPACE);
 
-            $iterator = $component;
+        }
 
-            foreach ($iterator as $component) {
-                $startDocument($component);
-                $component->xmlSerialize($writer);
-                $endDocument();
-            }
+        $component->xmlSerialize($writer);
 
-        } else {
-            throw new \InvalidArgumentException('Need a ' . __NAMESPACE__ .  '\Component object or an iterator');
-        }
+        $writer->endElement();
 
         return $writer->outputMemory();
 
diff --git a/tests/VObject/Parser/XmlTest.php b/tests/VObject/Parser/XmlTest.php
index 6f5b9bd..870c373 100644
--- a/tests/VObject/Parser/XmlTest.php
+++ b/tests/VObject/Parser/XmlTest.php
@@ -1151,37 +1151,6 @@ 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.
diff --git a/tests/VObject/ReaderTest.php b/tests/VObject/ReaderTest.php
index 3588e83..3bdda95 100644
--- a/tests/VObject/ReaderTest.php
+++ b/tests/VObject/ReaderTest.php
@@ -458,7 +458,7 @@ ICS;
 </icalendar>
 XML;
 
-        $result = Reader::readXML($data)->current();
+        $result = Reader::readXML($data);
 
         $this->assertInstanceOf('Sabre\\VObject\\Component', $result);
         $this->assertEquals('VCALENDAR', $result->name);
@@ -480,7 +480,7 @@ XML;
         fwrite($stream, $data);
         rewind($stream);
 
-        $result = Reader::readXML($stream)->current();
+        $result = Reader::readXML($stream);
 
         $this->assertInstanceOf('Sabre\\VObject\\Component', $result);
         $this->assertEquals('VCALENDAR', $result->name);
diff --git a/tests/VObject/WriterTest.php b/tests/VObject/WriterTest.php
index 043a9f8..800e13d 100644
--- a/tests/VObject/WriterTest.php
+++ b/tests/VObject/WriterTest.php
@@ -18,33 +18,6 @@ class WriterTest extends \PHPUnit_Framework_TestCase {
 
     }
 
-    function testWriteToMimeDirWithIterator() {
-
-        $iterator = function() {
-            for($i = 0; $i < 3; ++$i) {
-                yield $this->getComponent();
-            }
-        };
-
-        $result = Writer::write($iterator());
-        $this->assertEquals(
-            "BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n" .
-            "BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n" .
-            "BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n",
-            $result
-        );
-
-    }
-
-    /**
-     * @expectedException InvalidArgumentException
-     */
-    function testWriteToMimeDirWithUnexpectedArgument() {
-
-        $result = Writer::write('foo');
-
-    }
-
     function testWriteToJson() {
 
         $result = Writer::writeJson($this->getComponent());
@@ -65,13 +38,4 @@ class WriterTest extends \PHPUnit_Framework_TestCase {
 
     }
 
-    /**
-     * @expectedException InvalidArgumentException
-     */
-    function testWriteToXmlWithUnexpectedArgument() {
-
-        $result = Writer::writeXml('foo');
-
-    }
-
 }

-- 
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