[Pkg-owncloud-commits] [php-sabre-vobject] 11/30: add VCard::getByType()

David Prévot taffit at moszumanska.debian.org
Sun Mar 13 00:53:01 UTC 2016


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 01cf594e25fcbbfcd0db9d834b29e5a9245c21b4
Author: Kevin Bond <kevinbond at gmail.com>
Date:   Wed Feb 17 11:34:49 2016 -0500

    add VCard::getByType()
---
 lib/Component/VCard.php               | 19 +++++++++++++++++++
 tests/VObject/Component/VCardTest.php | 16 ++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/lib/Component/VCard.php b/lib/Component/VCard.php
index 66bbdd7..3c05191 100644
--- a/lib/Component/VCard.php
+++ b/lib/Component/VCard.php
@@ -411,6 +411,25 @@ class VCard extends VObject\Document {
     }
 
     /**
+     * Returns a property with a specific TYPE value (ADR, TEL, or EMAIL).
+     *
+     * This function will return null if the property does not exist. If there are
+     * multiple properties with the same TYPE value, only one will be returned.
+     *
+     * @param string $propertyName
+     * @param string $type
+     *
+     * @return VObject\Property|null
+     */
+    function getByType($propertyName, $type) {
+        foreach ($this->select($propertyName) as $field) {
+            if (isset($field['TYPE']) && $field['TYPE']->has($type)) {
+                return $field;
+            }
+        }
+    }
+
+    /**
      * This method should return a list of default property values.
      *
      * @return array
diff --git a/tests/VObject/Component/VCardTest.php b/tests/VObject/Component/VCardTest.php
index c16ff34..baa7f49 100644
--- a/tests/VObject/Component/VCardTest.php
+++ b/tests/VObject/Component/VCardTest.php
@@ -115,6 +115,22 @@ class VCardTest extends \PHPUnit_Framework_TestCase {
         $this->assertEquals(VCard::UNKNOWN, $vcard->getDocumentType());
     }
 
+    function testGetByType() {
+        $vcard = <<<VCF
+BEGIN:VCARD
+VERSION:3.0
+EMAIL;TYPE=home:1 at example.org
+EMAIL;TYPE=work:2 at example.org
+END:VCARD
+VCF;
+
+        $vcard = VObject\Reader::read($vcard);
+        $this->assertEquals('1 at example.org', $vcard->getByType('EMAIL', 'home')->getValue());
+        $this->assertEquals('2 at example.org', $vcard->getByType('EMAIL', 'work')->getValue());
+        $this->assertNull($vcard->getByType('EMAIL', 'non-existant'));
+        $this->assertNull($vcard->getByType('ADR', 'non-existant'));
+    }
+
     function testPreferredNoPref() {
 
         $vcard = <<<VCF

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



More information about the Pkg-owncloud-commits mailing list