Started coding the files that already have specifications. Most specifications pass...
[pivip.git] / project / library / Pivip / Auth / Identity / Property.php
blob3b150ba0f810869a03df921eda5a3da0ec55739d
1 <?php
3 /**
4 * Pivip
5 * Copyright (C) 2008 Vincent Tunru
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 * @license http://www.fsf.org/licensing/licenses/info/GPLv2.html GPL v.2
21 * @category Pivip
22 * @package Pivip
23 * @copyright (C) 2008 Vincent Tunru
24 * @author Vincent Tunru <email@vincentt.org>
27 /**
28 * Property of an identity
30 * @link http://www.ietf.org/rfc/rfc2426.txt RFC 2426, vCard MIME Directory
31 * profile.
32 * @see /library/Pivip/Auth/Identity/Abstract.php
34 class Pivip_Auth_Identity_Property implements SeekableIterator, Countable
36 /**
37 * @var array Property subproperties
39 private $_subProperties = array();
41 /**
42 * @var string Value of the property
44 public $value;
46 /**
47 * @var string Name of the property
49 public $name;
51 /**
52 * Set a property, as well as optional sub-properties
54 * @param string $property The property to set
55 * @param mixed $subProperties Provide an array to set sub-properties,
56 * provide a string to just set the value
58 public function __construct($property, $subProperties)
60 $this->name = strtolower($property);
61 $this->_subProperties = new ArrayObject();
63 if('email' == $property)
65 if(is_string($subProperties))
67 $this->value = $subProperties;
68 $this->_subProperties = array('type' => 'internet',
69 'value' => $subProperties);
70 } else if(is_array($subProperties)) {
71 if(empty($subProperties['type']))
73 $subProperties['type'] = 'internet';
75 $subProperties['type'] = strtolower($subProperties['type']);
76 if(empty($subProperties['value']))
78 $values = $subProperties;
79 unset($values['type']);
80 $subProperties['value'] = implode('', $values);
82 $this->_subProperties = $subProperties;
83 $this->value = $subProperties['value'];
84 } else {
85 throw new InvalidArgumentException();
87 return;
90 if('tel' == $property)
92 if(is_string($subProperties))
94 $this->value = $subProperties;
95 $this->_subProperties = array('type' => 'voice',
96 'value' => $subProperties);
97 } else if(is_array($subProperties))
99 if(empty($subProperties['type']))
101 $subProperties['type'] = 'voice';
103 $subProperties['type'] = strtolower($subProperties['type']);
104 if(empty($subProperties['value']))
106 $values = $subProperties;
107 unset($values['type']);
108 $subProperties['value'] = implode('', $values);
110 $this->_subProperties = $subProperties;
111 $this->value = $subProperties['value'];
112 } else {
113 throw new InvalidArgumentException();
115 return;
118 if('adr' == $property)
120 $components = array('post-office-box', 'extended-address',
121 'street-address', 'locality', 'region',
122 'postal-code', 'country-name', 'type');
123 if(is_string($subProperties))
125 $this->value = $subProperties;
126 $values = explode(';', $subProperties);
127 foreach($values as $i => $subProperty)
129 $this->_subProperties[$components[$i]] = $subProperty;
131 } else if(is_array($subProperties))
133 $value = '';
134 foreach($components as $component)
136 if(!empty($subProperties[$component]))
138 $value .= $subProperties[$component];
140 $value .= ';';
142 $this->value = $value;
143 $this->_subProperties = $subProperties;
144 } else {
145 throw new InvalidArgumentException();
147 return;
150 if('geo' == $property)
152 if(is_string($subProperties))
154 if(false === strpos($subProperties, ';'))
156 throw new InvalidArgumentException();
158 $values = explode(';', $subProperties);
159 $this->value = $subProperties;
160 $this->_subProperties = array('latitude' => $values[0],
161 'longitude' => $values[1]);
162 } else if(is_array($subProperties))
164 if(empty($subProperties['latitude']) ||
165 empty($subProperties['longitude']))
167 throw new InvalidArgumentException();
169 $this->value = $subProperties['latitude'] . ';' .
170 $subProperties['longitude'];
171 $this->_subProperties = $subProperties;
172 } else {
173 throw new InvalidArgumentException();
175 return;
178 if('org' == $property)
180 if(is_string($subProperties))
182 $this->value = $subProperties;
183 $this->_subProperties = array('organization-name' =>
184 $subProperties);
185 } else if(is_array($subProperties))
187 if(empty($subProperties['organization-name']))
189 $subProperties['organization-name'] = implode('',
190 $subProperties);
192 $this->_subProperties = $subProperties;
193 $this->value = $subProperties['organization-name'];
194 } else {
195 throw new InvalidArgumentException();
197 return;
200 if(is_string($subProperties))
202 $this->value = $subProperties;
203 } else if(is_array($subProperties)) {
204 $this->_subProperties = $subProperties;
205 $this->value = implode(';', $subProperties);
206 } else {
207 throw new InvalidArgumentException();
212 * Retrieve a sub-property
214 * @param string $subProperty Name of the sub-property to retrieve.
215 * @return string Value of the sub-property
217 public function __get($subProperty)
219 if(isset($this->_subProperties[$subProperty]))
221 return $this->_subProperties[$subProperty];
223 return '';
227 * Set a sub-property
229 * @param string $subProperty Name of the sub-property to set.
230 * @param string $value Value of the sub-property
232 public function __set($subProperty, $value)
234 if(!is_string($subProperty) || !is_string($value))
236 throw new InvalidArgumentException();
238 $this->_subProperties[$subProperty] = $value;
242 * Check whether a sub-property is set
244 * @param string $subProperty Name of the sub-property to check
245 * @return boolean Whether the sub-property is set.
247 public function __isset($subProperty)
249 return isset($this->_subProperties[$subProperty]);
253 * Unset a sub-property
255 * @param string $subProperty Name of the sub-property to unset
256 * @return void
258 public function __unset($subProperty)
260 unset($this->_subProperties[$subProperty]);
264 * The current value
266 * @link http://www.php.net/~helly/php/ext/spl/interfaceSeekableIterator.html
268 public function current()
270 return current($this->_subProperties);
274 * The current key
276 * @link http://www.php.net/~helly/php/ext/spl/interfaceSeekableIterator.html
278 public function key()
280 return key($this->_subProperties);
284 * Move the pointer forward
286 * @link http://www.php.net/~helly/php/ext/spl/interfaceSeekableIterator.html
288 public function next()
290 next($this->_subProperties);
294 * Rewind the pointer
296 * @link http://www.php.net/~helly/php/ext/spl/interfaceSeekableIterator.html
298 public function rewind()
300 reset($this->_subProperties);
304 * Get a specific value
306 * @link http://www.php.net/~helly/php/ext/spl/interfaceSeekableIterator.html
307 * @param mixed $index Index of the value to retrieve
309 public function seek($index)
311 return $this->__get($index);
315 * Check whether the current index is valid
317 * @link http://www.php.net/~helly/php/ext/spl/interfaceSeekableIterator.html
319 public function valid()
321 return (false !== current($this->_subProperties));
325 * Count the number of subproperties
327 * @link http://www.php.net/~helly/php/ext/spl/interfaceCountable.html
329 public function count()
331 return count($this->_subProperties);