]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/libomb/profile.php
Merge branch '0.8.x' into 0.9.x
[quix0rs-gnu-social.git] / extlib / libomb / profile.php
1 <?php
2 require_once 'invalidparameterexception.php';
3 require_once 'Validate.php';
4 require_once 'helper.php';
5
6 /**
7  * OMB profile representation
8  *
9  * This class represents an OMB profile.
10  *
11  * Do not call the setters with null values. Instead, if you want to delete a
12  * field, pass an empty string. The getters will return null for empty fields.
13  *
14  * PHP version 5
15  *
16  * LICENSE: This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  * @package   OMB
30  * @author    Adrian Lang <mail@adrianlang.de>
31  * @copyright 2009 Adrian Lang
32  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
33  **/
34
35 class OMB_Profile {
36   protected $identifier_uri;
37   protected $profile_url;
38   protected $nickname;
39   protected $license_url;
40   protected $fullname;
41   protected $homepage;
42   protected $bio;
43   protected $location;
44   protected $avatar_url;
45
46   /* The profile as OMB param array. Cached and rebuild on usage.
47      false while outdated. */
48   protected $param_array;
49
50   /**
51    * Constructor for OMB_Profile
52    *
53    * Initializes the OMB_Profile object with an identifier uri.
54    *
55    * @param string $identifier_uri The profile URI as defined by the OMB. A unique
56    *                               and unchanging identifier for a profile.
57    *
58    * @access public
59    */
60   public function __construct($identifier_uri) {
61     if (!Validate::uri($identifier_uri)) {
62       throw new OMB_InvalidParameterException($identifier_uri, 'profile',
63                                                 'omb_listenee or omb_listener');
64     }
65     $this->identifier_uri = $identifier_uri;
66     $this->param_array = false;
67   }
68
69   /**
70    * Returns the profile as array
71    *
72    * The method returns an array which contains the whole profile as array. The
73    * array is cached and only rebuilt on changes of the profile.
74    *
75    * @param bool   $force_all Specifies whether empty fields should be added to
76    *                          the array as well. This is neccessary to clear
77    *                          fields via updateProfile.
78    *
79    * @param string $prefix    The common prefix to the key for all parameters.
80    *
81    * @access public
82    *
83    * @return array The profile as parameter array
84    */
85   public function asParameters($prefix, $force_all = false) {
86     if ($this->param_array === false) {
87       $this->param_array = array('' => $this->identifier_uri);
88
89       if ($force_all || !is_null($this->profile_url)) {
90         $this->param_array['_profile'] = $this->profile_url;
91       }
92
93       if ($force_all || !is_null($this->homepage)) {
94         $this->param_array['_homepage'] = $this->homepage;
95       }
96
97       if ($force_all || !is_null($this->nickname)) {
98         $this->param_array['_nickname'] = $this->nickname;
99       }
100
101       if ($force_all || !is_null($this->license_url)) {
102         $this->param_array['_license'] = $this->license_url;
103       }
104
105       if ($force_all || !is_null($this->fullname)) {
106         $this->param_array['_fullname'] = $this->fullname;
107       }
108
109       if ($force_all || !is_null($this->bio)) {
110         $this->param_array['_bio'] = $this->bio;
111       }
112
113       if ($force_all || !is_null($this->location)) {
114         $this->param_array['_location'] = $this->location;
115       }
116
117       if ($force_all || !is_null($this->avatar_url)) {
118         $this->param_array['_avatar'] = $this->avatar_url;
119       }
120
121     }
122     $ret = array();
123     foreach ($this->param_array as $k => $v) {
124       $ret[$prefix . $k] = $v;
125     }
126     return $ret;
127   }
128
129   /**
130    * Builds an OMB_Profile object from array
131    *
132    * The method builds an OMB_Profile object from the passed parameters array. The
133    * array MUST provide a profile URI. The array fields HAVE TO be named according
134    * to the OMB standard. The prefix (omb_listener or omb_listenee) is passed as a
135    * parameter.
136    *
137    * @param string $parameters An array containing the profile parameters.
138    * @param string $prefix     The common prefix of the profile parameter keys.
139    *
140    * @access public
141    *
142    * @returns OMB_Profile The built OMB_Profile.
143    */
144   public static function fromParameters($parameters, $prefix) {
145     if (!isset($parameters[$prefix])) {
146       throw new OMB_InvalidParameterException('', 'profile', $prefix);
147     }
148
149     $profile = new OMB_Profile($parameters[$prefix]);
150     $profile->updateFromParameters($parameters, $prefix);
151     return $profile;
152   }
153
154   /**
155    * Update from array
156    *
157    * Updates from the passed parameters array. The array does not have to
158    * provide a profile URI. The array fields HAVE TO be named according to the
159    * OMB standard. The prefix (omb_listener or omb_listenee) is passed as a
160    * parameter.
161    *
162    * @param string $parameters An array containing the profile parameters.
163    * @param string $prefix     The common prefix of the profile parameter keys.
164    *
165    * @access public
166    */
167   public function updateFromParameters($parameters, $prefix) {
168     if (isset($parameters[$prefix.'_profile'])) {
169       $this->setProfileURL($parameters[$prefix.'_profile']);
170     }
171
172     if (isset($parameters[$prefix.'_license'])) {
173       $this->setLicenseURL($parameters[$prefix.'_license']);
174     }
175
176     if (isset($parameters[$prefix.'_nickname'])) {
177       $this->setNickname($parameters[$prefix.'_nickname']);
178     }
179
180     if (isset($parameters[$prefix.'_fullname'])) {
181       $this->setFullname($parameters[$prefix.'_fullname']);
182     }
183
184     if (isset($parameters[$prefix.'_homepage'])) {
185       $this->setHomepage($parameters[$prefix.'_homepage']);
186     }
187
188     if (isset($parameters[$prefix.'_bio'])) {
189       $this->setBio($parameters[$prefix.'_bio']);
190     }
191
192     if (isset($parameters[$prefix.'_location'])) {
193       $this->setLocation($parameters[$prefix.'_location']);
194     }
195
196     if (isset($parameters[$prefix.'_avatar'])) {
197       $this->setAvatarURL($parameters[$prefix.'_avatar']);
198     }
199   }
200
201   public function getIdentifierURI() {
202     return $this->identifier_uri;
203   }
204
205   public function getProfileURL() {
206     return $this->profile_url;
207   }
208
209   public function getHomepage() {
210     return $this->homepage;
211   }
212
213   public function getNickname() {
214     return $this->nickname;
215   }
216
217   public function getLicenseURL() {
218     return $this->license_url;
219   }
220
221   public function getFullname() {
222     return $this->fullname;
223   }
224
225   public function getBio() {
226     return $this->bio;
227   }
228
229   public function getLocation() {
230     return $this->location;
231   }
232
233   public function getAvatarURL() {
234     return $this->avatar_url;
235   }
236
237   public function setProfileURL($profile_url) {
238     if (!OMB_Helper::validateURL($profile_url)) {
239       throw new OMB_InvalidParameterException($profile_url, 'profile',
240                                     'omb_listenee_profile or omb_listener_profile');
241     }
242     $this->profile_url = $profile_url;
243     $this->param_array = false;
244   }
245
246   public function setNickname($nickname) {
247     if (!Validate::string($nickname,
248                           array('min_length' => 1,
249                                 'max_length' => 64,
250                                 'format' => VALIDATE_NUM . VALIDATE_ALPHA))) {
251       throw new OMB_InvalidParameterException($nickname, 'profile', 'nickname');
252     }
253
254     $this->nickname = $nickname;
255     $this->param_array = false;
256   }
257
258   public function setLicenseURL($license_url) {
259     if (!OMB_Helper::validateURL($license_url)) {
260       throw new OMB_InvalidParameterException($license_url, 'profile',
261                                     'omb_listenee_license or omb_listener_license');
262     }
263     $this->license_url = $license_url;
264     $this->param_array = false;
265   }
266
267   public function setFullname($fullname) {
268     if ($fullname === '') {
269       $fullname = null;
270     } elseif (!Validate::string($fullname, array('max_length' => 255))) {
271       throw new OMB_InvalidParameterException($fullname, 'profile', 'fullname');
272     }
273     $this->fullname = $fullname;
274     $this->param_array = false;
275   }
276
277   public function setHomepage($homepage) {
278     if ($homepage === '') {
279       $homepage = null;
280     }
281     $this->homepage = $homepage;
282     $this->param_array = false;
283   }
284
285   public function setBio($bio) {
286     if ($bio === '') {
287       $bio = null;
288     } elseif (!Validate::string($bio, array('max_length' => 140))) {
289       throw new OMB_InvalidParameterException($bio, 'profile', 'fullname');
290     }
291     $this->bio = $bio;
292     $this->param_array = false;
293   }
294
295   public function setLocation($location) {
296     if ($location === '') {
297       $location = null;
298     } elseif (!Validate::string($location, array('max_length' => 255))) {
299       throw new OMB_InvalidParameterException($location, 'profile', 'fullname');
300     }
301     $this->location = $location;
302     $this->param_array = false;
303   }
304
305   public function setAvatarURL($avatar_url) {
306     if ($avatar_url === '') {
307       $avatar_url = null;
308     } elseif (!OMB_Helper::validateURL($avatar_url)) {
309       throw new OMB_InvalidParameterException($avatar_url, 'profile',
310                                       'omb_listenee_avatar or omb_listener_avatar');
311     }
312     $this->avatar_url = $avatar_url;
313     $this->param_array = false;
314   }
315
316 }
317 ?>