]> git.mxchange.org Git - friendica-addons.git/blob - dav/sabre-vobject/lib/Sabre/VObject/Component/VCard.php
Second part of refactoring; should be runnable again, yet not thoroughly tested
[friendica-addons.git] / dav / sabre-vobject / lib / Sabre / VObject / Component / VCard.php
1 <?php
2
3 namespace Sabre\VObject\Component;
4
5 use Sabre\VObject;
6
7 /**
8  * The VCard component
9  *
10  * This component represents the BEGIN:VCARD and END:VCARD found in every
11  * vcard.
12  *
13  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
14  * @author Evert Pot (http://www.rooftopsolutions.nl/)
15  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
16  */
17 class VCard extends VObject\Component {
18
19     /**
20      * VCards with version 2.1, 3.0 and 4.0 are found.
21      *
22      * If the VCARD doesn't know its version, 4.0 is assumed.
23      */
24     const DEFAULT_VERSION = '4.0';
25
26     /**
27      * Validates the node for correctness.
28      *
29      * The following options are supported:
30      *   - Component::REPAIR - If something is broken, and automatic repair may
31      *                         be attempted.
32      *
33      * An array is returned with warnings.
34      *
35      * Every item in the array has the following properties:
36      *    * level - (number between 1 and 3 with severity information)
37      *    * message - (human readable message)
38      *    * node - (reference to the offending node)
39      *
40      * @param int $options
41      * @return array
42      */
43     public function validate($options = 0) {
44
45         $warnings = array();
46
47         $version = $this->select('VERSION');
48         if (count($version)!==1) {
49             $warnings[] = array(
50                 'level' => 1,
51                 'message' => 'The VERSION property must appear in the VCARD component exactly 1 time',
52                 'node' => $this,
53             );
54             if ($options & self::REPAIR) {
55                 $this->VERSION = self::DEFAULT_VERSION;
56             }
57         } else {
58             $version = (string)$this->VERSION;
59             if ($version!=='2.1' && $version!=='3.0' && $version!=='4.0') {
60                 $warnings[] = array(
61                     'level' => 1,
62                     'message' => 'Only vcard version 4.0 (RFC6350), version 3.0 (RFC2426) or version 2.1 (icm-vcard-2.1) are supported.',
63                     'node' => $this,
64                 );
65                 if ($options & self::REPAIR) {
66                     $this->VERSION = '4.0';
67                 }
68             }
69
70         }
71         $version = $this->select('FN');
72         if (count($version)!==1) {
73             $warnings[] = array(
74                 'level' => 1,
75                 'message' => 'The FN property must appear in the VCARD component exactly 1 time',
76                 'node' => $this,
77             );
78             if (($options & self::REPAIR) && count($version) === 0) {
79                 // We're going to try to see if we can use the contents of the
80                 // N property.
81                 if (isset($this->N)) {
82                     $value = explode(';', (string)$this->N);
83                     if (isset($value[1]) && $value[1]) {
84                         $this->FN = $value[1] . ' ' . $value[0];
85                     } else {
86                         $this->FN = $value[0];
87                     }
88
89                 // Otherwise, the ORG property may work
90                 } elseif (isset($this->ORG)) {
91                     $this->FN = (string)$this->ORG;
92                 }
93
94             }
95         }
96
97         return array_merge(
98             parent::validate($options),
99             $warnings
100         );
101
102     }
103
104 }
105