source: pmb4.1/trunk/fuentes/pmb/admin/connecteurs/out/webdav/vendor/sabre/vobject/lib/Sabre/VObject/Property/Compound.php @ 478

Last change on this file since 478 was 478, checked in by mabarracus, 4 years ago

copy trusty code 4.1

  • Property svn:executable set to *
File size: 3.3 KB
Line 
1<?php
2
3namespace Sabre\VObject\Property;
4
5use Sabre\VObject;
6
7/**
8* Compound property.
9*
10* This class adds (de)serialization of compound properties to/from arrays.
11*
12* Currently the following properties from RFC 6350 are mapped to use this
13* class:
14*
15*  N:          Section 6.2.2
16*  ADR:        Section 6.3.1
17*  ORG:        Section 6.6.4
18*  CATEGORIES: Section 6.7.1
19*
20* In order to use this correctly, you must call setParts and getParts to
21* retrieve and modify dates respectively.
22*
23* @author Thomas Tanghus (http://tanghus.net/)
24* @author Lars Kneschke
25* @author Evert Pot (http://www.rooftopsolutions.nl/)
26* @copyright Copyright (C) 2007-2013 Rooftop Solutions. All rights reserved.
27* @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
28*/
29
30/**
31* This class represents a compound property in a vCard.
32*/
33class Compound extends VObject\Property {
34
35    /**
36    * If property names are added to this map, they will be (de)serialised as arrays
37    * using the getParts() and setParts() methods.
38    * The keys are the property names, values are delimiter chars.
39    *
40    * @var array
41    */
42    static public $delimiterMap = array(
43        'N'                             =>      ';',
44        'ADR'                   =>      ';',
45        'ORG'                   =>      ';',
46        'CATEGORIES'    =>      ',',
47    );
48
49    /**
50     * The currently used delimiter.
51     *
52     * @var string
53     */
54    protected $delimiter = null;
55
56    /**
57    * Get a compound value as an array.
58    *
59    * @param $name string
60    * @return array
61    */
62    public function getParts() {
63
64        if (is_null($this->value)) {
65            return array();
66        }
67
68        $delimiter = $this->getDelimiter();
69
70        // split by any $delimiter which is NOT prefixed by a slash.
71        // Note that this is not a a perfect solution. If a value is prefixed
72        // by two slashes, it should actually be split anyway.
73        //
74        // Hopefully we can fix this better in a future version, where we can
75        // break compatibility a bit.
76        $compoundValues = preg_split("/(?<!\\\)$delimiter/", $this->value);
77
78        // remove slashes from any semicolon and comma left escaped in the single values
79        $compoundValues = array_map(
80            function($val) {
81                return strtr($val, array('\,' => ',', '\;' => ';'));
82        }, $compoundValues);
83
84        return $compoundValues;
85
86    }
87
88    /**
89     * Returns the delimiter for this property.
90     *
91     * @return string
92     */
93    public function getDelimiter() {
94
95        if (!$this->delimiter) {
96            if (isset(self::$delimiterMap[$this->name])) {
97                $this->delimiter = self::$delimiterMap[$this->name];
98            } else {
99                // To be a bit future proof, we are going to default the
100                // delimiter to ;
101                $this->delimiter = ';';
102            }
103        }
104        return $this->delimiter;
105
106    }
107
108    /**
109     * Set a compound value as an array.
110     *
111    *
112    * @param $name string
113    * @return array
114    */
115    public function setParts(array $values) {
116
117        // add slashes to all semicolons and commas in the single values
118        $values = array_map(
119            function($val) {
120                return strtr($val, array(',' => '\,', ';' => '\;'));
121            }, $values);
122
123        $this->setValue(
124            implode($this->getDelimiter(), $values)
125        );
126
127    }
128
129}
Note: See TracBrowser for help on using the repository browser.