[Pkg-owncloud-commits] [php-sabre-vobject] 08/16: Converting X-APPLE-OMIT-YEAR.

David Prévot taffit at moszumanska.debian.org
Mon Jul 21 19:19:14 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 655a5ddbb1a059b87a10f863a2c1377d9f7eed69
Author: Evert Pot <me at evertpot.com>
Date:   Mon Jul 14 12:14:08 2014 -0400

    Converting X-APPLE-OMIT-YEAR.
    
    Fixes #98.
---
 ChangeLog.md                               |  2 ++
 lib/Sabre/VObject/VCardConverter.php       | 42 ++++++++++++++++++++++++
 tests/Sabre/VObject/VCardConverterTest.php | 51 ++++++++++++++++++++++++++++++
 tests/phpcs/ruleset.xml                    |  3 +-
 4 files changed, 96 insertions(+), 2 deletions(-)

diff --git a/ChangeLog.md b/ChangeLog.md
index 1d080ef..21e0461 100644
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -4,6 +4,8 @@ ChangeLog
 3.2.4 (2014-??-??)
 ------------------
 
+* Added: The VCardConverter now takes `X-APPLE-OMIT-YEAR` into consideration
+  when converting between vCard 3 and 4.
 * Fixed: PHP 5.3 support was broken in the cli tool.
 
 
diff --git a/lib/Sabre/VObject/VCardConverter.php b/lib/Sabre/VObject/VCardConverter.php
index 618cef3..6afa486 100644
--- a/lib/Sabre/VObject/VCardConverter.php
+++ b/lib/Sabre/VObject/VCardConverter.php
@@ -94,6 +94,29 @@ class VCardConverter {
 
                 $newProperty = $this->convertUriToBinary($output, $property, $parameters);
 
+            } elseif ($property instanceof Property\VCard\DateAndOrTime) {
+
+                // In vCard 4, the birth year may be optional. This is not the
+                // case for vCard 3. Apple has a workaround for this that
+                // allows applications that support Apple's extension still
+                // omit birthyears in vCard 3, but applications that do not
+                // support this, will just use a random birthyear. We're
+                // choosing 1604 for the birthyear, because that's what apple
+                // uses.
+                $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
+                    );
+
+                }
+
             } elseif ($property->name === 'KIND') {
 
                 switch(strtolower($property->getValue())) {
@@ -125,6 +148,25 @@ class VCardConverter {
 
                 $newProperty = $this->convertBinaryToUri($output, $property, $parameters);
 
+            } elseif ($property instanceof Property\VCard\DateAndOrTime && isset($parameters['X-APPLE-OMIT-YEAR'])) {
+
+                // If a property such as BDAY contained 'X-APPLE-OMIT-YEAR',
+                // then we're stripping the year from the vcard 4 value.
+                $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
+                    );
+                }
+
+                // 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' :
diff --git a/tests/Sabre/VObject/VCardConverterTest.php b/tests/Sabre/VObject/VCardConverterTest.php
index 25b907e..941d36c 100644
--- a/tests/Sabre/VObject/VCardConverterTest.php
+++ b/tests/Sabre/VObject/VCardConverterTest.php
@@ -271,4 +271,55 @@ OUT;
         );
 
     }
+
+    function testBDAYConversion() {
+
+        $version = Version::VERSION;
+
+        $input = <<<IN
+BEGIN:VCARD
+VERSION:3.0
+PRODID:foo
+BDAY;X-APPLE-OMIT-YEAR=1604:1604-04-16
+END:VCARD
+
+IN;
+
+        $output = <<<OUT
+BEGIN:VCARD
+VERSION:4.0
+PRODID:-//Sabre//Sabre VObject {$version}//EN
+BDAY:--04-16
+END:VCARD
+
+OUT;
+
+        $vcard = \Sabre\VObject\Reader::read($input);
+        $vcard = $vcard->convert(\Sabre\VObject\Document::VCARD40);
+
+        $this->assertEquals(
+            $output,
+            str_replace("\r", "", $vcard->serialize())
+        );
+
+        $input = $output;
+        $output = <<<OUT
+BEGIN:VCARD
+VERSION:3.0
+PRODID:-//Sabre//Sabre VObject {$version}//EN
+BDAY;X-APPLE-OMIT-YEAR=1604:1604-04-16
+END:VCARD
+
+OUT;
+
+        $vcard = \Sabre\VObject\Reader::read($input);
+        $vcard = $vcard->convert(\Sabre\VObject\Document::VCARD30);
+
+        $this->assertEquals(
+            $output,
+            str_replace("\r", "", $vcard->serialize())
+        );
+
+    }
+
 }
diff --git a/tests/phpcs/ruleset.xml b/tests/phpcs/ruleset.xml
index f28539a..ec2c4c8 100644
--- a/tests/phpcs/ruleset.xml
+++ b/tests/phpcs/ruleset.xml
@@ -38,7 +38,7 @@
    <!-- The PHP constants true, false, and null MUST be in lower case. -->
    <rule ref="Generic.PHP.LowerCaseConstant"/>
 
-   <rule ref="Squiz.Scope.MethodScope"/>
+   <!-- <rule ref="Squiz.Scope.MethodScope"/> -->
    <rule ref="Squiz.WhiteSpace.ScopeKeywordSpacing"/>
 
    <!-- In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma. -->
@@ -52,7 +52,6 @@
     <severity>0</severity>
    </rule>
     -->
-   <rule ref="PEAR.Functions.ValidDefaultValue"/>
    <rule ref="PEAR.WhiteSpace.ScopeClosingBrace"/>
 
 </ruleset>

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