[Pkg-owncloud-commits] [php-sabre-vobject] 02/32: Support for converting ANNIVERSARY

David Prévot taffit at moszumanska.debian.org
Fri Nov 28 22:27:07 UTC 2014


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 86a6d9ba1d0fb37a3290f1f1f66f965c85d0d64a
Author: Evert Pot <evert at rooftopsolutions.nl>
Date:   Mon Nov 3 21:55:49 2014 -0300

    Support for converting ANNIVERSARY
    
    vcard 3 -> 4
    ------------
    
    Find all X-ABDATE and X-ANNIVERSARY properties, and merge the ones with identical values. Convert all of these to ANNIVERSARY.
    
    vcard 4 -> 3
    ------------
    
    Convert ANNIVERSARY to both X-ABDATE and X-ANNIVERSARY.
---
 lib/VCardConverter.php               | 135 ++++++++++++++++++++++-------------
 tests/VObject/VCardConverterTest.php |  52 +++++++++++++-
 2 files changed, 133 insertions(+), 54 deletions(-)

diff --git a/lib/VCardConverter.php b/lib/VCardConverter.php
index 6afa486..34160da 100644
--- a/lib/VCardConverter.php
+++ b/lib/VCardConverter.php
@@ -76,7 +76,6 @@ class VCardConverter {
         }
 
         $parameters = $property->parameters();
-
         $valueType = null;
         if (isset($parameters['VALUE'])) {
             $valueType = $parameters['VALUE']->getValue();
@@ -85,14 +84,19 @@ class VCardConverter {
         if (!$valueType) {
             $valueType = $property->getValueType();
         }
+        $newProperty = $output->createProperty(
+            $property->name,
+            $property->getParts(),
+            array(), // parameters will get added a bit later.
+            $valueType
+        );
 
-        $newProperty = null;
 
         if ($targetVersion===Document::VCARD30) {
 
             if ($property instanceof Property\Uri && in_array($property->name, array('PHOTO','LOGO','SOUND'))) {
 
-                $newProperty = $this->convertUriToBinary($output, $property, $parameters);
+                $newProperty = $this->convertUriToBinary($output, $newProperty, $parameters);
 
             } elseif ($property instanceof Property\VCard\DateAndOrTime) {
 
@@ -106,26 +110,38 @@ class VCardConverter {
                 $parts = DateTimeParser::parseVCardDateTime($property->getValue());
                 if (is_null($parts['year'])) {
                     $newValue = '1604-' . $parts['month'] . '-' . $parts['date'];
-                    $newProperty = $output->createProperty(
-                        $property->name,
-                        $newValue,
-                        array(
-                            'X-APPLE-OMIT-YEAR' => '1604'
-                        ),
-                        $valueType
-                    );
+                    $newProperty->setValue($newValue);
+                    $newProperty['X-APPLE-OMIT-YEAR'] = '1604';
+                }
 
+                if ($newProperty->name == 'ANNIVERSARY') {
+                    // Microsoft non-standard anniversary
+                    $newProperty->name = 'X-ANNIVERSARY';
+
+                    // We also need to add a new apple property for the same
+                    // purpose. This apple property needs a 'label' in the same
+                    // group, so we first need to find a groupname that doesn't
+                    // exist yet.
+                    $x = 1;
+                    while($output->select('ITEM' . $x . '.')) {
+                       $x++;
+                    }
+                    $output->add('ITEM' . $x . '.X-ABDATE', $newProperty->getValue(), array('VALUE' => 'DATE-AND-OR-TIME'));
+                    $output->add('ITEM' . $x . '.X-ABLABEL', '_$!<Anniversary>!$_');
                 }
 
             } elseif ($property->name === 'KIND') {
 
                 switch(strtolower($property->getValue())) {
                     case 'org' :
-                        // OS X addressbook property.
+                        // vCard 3.0 does not have an equivalent to KIND:ORG,
+                        // but apple has an extension that means the same
+                        // thing.
                         $newProperty = $output->createProperty('X-ABSHOWAS','COMPANY');
                         break;
+
                     case 'individual' :
-                        // Individual is implied, so we can just skip it.
+                        // Individual is implicit, so we skip it.
                         return;
 
                     case 'group' :
@@ -146,7 +162,7 @@ class VCardConverter {
 
             if ($property instanceof Property\Binary) {
 
-                $newProperty = $this->convertBinaryToUri($output, $property, $parameters);
+                $newProperty = $this->convertBinaryToUri($output, $newProperty, $parameters);
 
             } elseif ($property instanceof Property\VCard\DateAndOrTime && isset($parameters['X-APPLE-OMIT-YEAR'])) {
 
@@ -155,45 +171,62 @@ class VCardConverter {
                 $parts = DateTimeParser::parseVCardDateTime($property->getValue());
                 if ($parts['year']===$property['X-APPLE-OMIT-YEAR']->getValue()) {
                     $newValue = '--' . $parts['month'] . '-' . $parts['date'];
-                    $newProperty = $output->createProperty(
-                        $property->name,
-                        $newValue,
-                        array(),
-                        $valueType
-                    );
+                    $newProperty->setValue($newValue);
                 }
 
                 // Regardless if the year matched or not, we do need to strip
                 // X-APPLE-OMIT-YEAR.
                 unset($parameters['X-APPLE-OMIT-YEAR']);
 
-            } else {
-                switch($property->name) {
-                    case 'X-ABSHOWAS' :
-                        if (strtoupper($property->getValue()) === 'COMPANY') {
-                            $newProperty = $output->createProperty('KIND','org');
-                        }
-                        break;
-                    case 'X-ADDRESSBOOKSERVER-KIND' :
-                        if (strtoupper($property->getValue()) === 'GROUP') {
-                            $newProperty = $output->createProperty('KIND','group');
-                        }
-                        break;
-                }
-
             }
+            switch($property->name) {
+                case 'X-ABSHOWAS' :
+                    if (strtoupper($property->getValue()) === 'COMPANY') {
+                        $newProperty = $output->createProperty('KIND','ORG');
+                    }
+                    break;
+                case 'X-ADDRESSBOOKSERVER-KIND' :
+                    if (strtoupper($property->getValue()) === 'GROUP') {
+                        $newProperty = $output->createProperty('KIND','GROUP');
+                    }
+                    break;
+                case 'X-ANNIVERSARY' :
+                    $newProperty->name = 'ANNIVERSARY';
+                    // If we already have an anniversary property with the same
+                    // value, ignore.
+                    if (isset($output->ANNIVERSARY) && $output->ANNIVERSARY->getValue() === $newProperty->getValue()) {
+                        return;
+                    }
+                    break;
+                case 'X-ABDATE' :
+                    // Find out what the label was, if it exists.
+                    if (!$property->group) {
+                        break;
+                    }
+                    $label = $input->{$property->group . '.X-ABLABEL'};
 
-        }
-
+                    // We only support converting anniversaries.
+                    if ($label->getValue()!=='_$!<Anniversary>!$_') {
+                        break;
+                    }
 
-        if (is_null($newProperty)) {
+                    // If we already have an anniversary property with the same
+                    // value, ignore.
+                    if (isset($output->ANNIVERSARY) && $output->ANNIVERSARY->getValue() === $newProperty->getValue()) {
+                        return;
+                    }
+                    $newProperty->name = 'ANNIVERSARY';
+                    break;
+                // Apple's per-property label system.
+                case 'X-ABLABEL' :
+                    if($newProperty->getValue() === '_$!<Anniversary>!$_') {
+                        // We can safely remove these, as they are converted to
+                        // ANNIVERSARY properties.
+                        return;
+                    }
+                    break;
 
-            $newProperty = $output->createProperty(
-                $property->name,
-                $property->getParts(),
-                array(), // no parameters yet
-                $valueType
-            );
+            }
 
         }
 
@@ -231,10 +264,11 @@ class VCardConverter {
      *                    the new property.
      * @return Property\Uri
      */
-    protected function convertBinaryToUri(Component\VCard $output, Property\Binary $property, array &$parameters) {
+    protected function convertBinaryToUri(Component\VCard $output, Property\Binary $newProperty, array &$parameters) {
 
+        $value = $newProperty->getValue();
         $newProperty = $output->createProperty(
-            $property->name,
+            $newProperty->name,
             null, // no value
             array(), // no parameters yet
             'URI' // Forcing the BINARY type
@@ -267,8 +301,7 @@ class VCardConverter {
 
         }
 
-        $newProperty->setValue('data:' . $mimeType . ';base64,' . base64_encode($property->getValue()));
-
+        $newProperty->setValue('data:' . $mimeType . ';base64,' . base64_encode($value));
         return $newProperty;
 
     }
@@ -286,17 +319,17 @@ class VCardConverter {
      *                    the new property.
      * @return Property\Binary|null
      */
-    protected function convertUriToBinary(Component\VCard $output, Property\Uri $property, array &$parameters) {
+    protected function convertUriToBinary(Component\VCard $output, Property\Uri $newProperty, array &$parameters) {
 
-        $value = $property->getValue();
+        $value = $newProperty->getValue();
 
         // Only converting data: uris
         if (substr($value, 0, 5)!=='data:') {
-            return;
+            return $newProperty;
         }
 
         $newProperty = $output->createProperty(
-            $property->name,
+            $newProperty->name,
             null, // no value
             array(), // no parameters yet
             'BINARY'
diff --git a/tests/VObject/VCardConverterTest.php b/tests/VObject/VCardConverterTest.php
index 614efd0..bab6a56 100644
--- a/tests/VObject/VCardConverterTest.php
+++ b/tests/VObject/VCardConverterTest.php
@@ -33,7 +33,7 @@ PHOTO;TYPE=HOME:data:image/jpeg;base64,Zm9v
 PHOTO:data:image/gif;base64,Zm9v
 PHOTO;X-PARAM=FOO:data:image/png;base64,Zm9v
 PHOTO:http://example.org/foo.png
-KIND:org
+KIND:ORG
 END:VCARD
 OUT;
 
@@ -176,7 +176,7 @@ PHOTO:data:image/jpeg;base64,Zm9v
 PHOTO:data:image/gif,foo
 PHOTO;X-PARAM=FOO:data:image/png;base64,Zm9v
 PHOTO:http://example.org/foo.png
-KIND:org
+KIND:ORG
 END:VCARD
 
 IN;
@@ -219,7 +219,7 @@ IN;
         $output = <<<OUT
 BEGIN:VCARD
 VERSION:4.0
-KIND:group
+KIND:GROUP
 END:VCARD
 
 OUT;
@@ -386,4 +386,50 @@ OUT;
 
     }
 
+    function testAnniversary() {
+
+        $input = <<<IN
+BEGIN:VCARD
+VERSION:4.0
+ITEM1.ANNIVERSARY:20081210
+END:VCARD
+
+IN;
+
+        $output = <<<'OUT'
+BEGIN:VCARD
+VERSION:3.0
+ITEM1.X-ABDATE;VALUE=DATE-AND-OR-TIME:20081210
+ITEM1.X-ABLABEL:_$!<Anniversary>!$_
+ITEM1.X-ANNIVERSARY;VALUE=DATE-AND-OR-TIME:20081210
+END:VCARD
+
+OUT;
+
+        $vcard = Reader::read($input);
+        $vcard = $vcard->convert(Document::VCARD30);
+
+        $this->assertVObjEquals(
+            $output,
+            $vcard
+        );
+
+        // Swapping input and output
+        list(
+            $input,
+            $output
+        ) = [
+            $output,
+            $input
+        ];
+
+        $vcard = Reader::read($input);
+        $vcard = $vcard->convert(Document::VCARD40);
+
+        $this->assertVObjEquals(
+            $output,
+            $vcard
+        );
+
+    }
 }

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