2 require_once 'invalidparameterexception.php';
3 require_once 'Validate.php';
4 require_once 'helper.php';
7 * OMB profile representation
9 * This class represents an OMB profile.
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.
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.
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.
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/>.
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
36 protected $identifier_uri;
37 protected $profile_url;
39 protected $license_url;
44 protected $avatar_url;
46 /* The profile as OMB param array. Cached and rebuild on usage.
47 false while outdated. */
48 protected $param_array;
51 * Constructor for OMB_Profile
53 * Initializes the OMB_Profile object with an identifier uri.
55 * @param string $identifier_uri The profile URI as defined by the OMB. A unique
56 * and unchanging identifier for a profile.
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');
65 $this->identifier_uri = $identifier_uri;
66 $this->param_array = false;
70 * Returns the profile as array
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.
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.
79 * @param string $prefix The common prefix to the key for all parameters.
83 * @return array The profile as parameter array
85 public function asParameters($prefix, $force_all = false) {
86 if ($this->param_array === false) {
87 $this->param_array = array('' => $this->identifier_uri);
89 if ($force_all || !is_null($this->profile_url)) {
90 $this->param_array['_profile'] = $this->profile_url;
93 if ($force_all || !is_null($this->homepage)) {
94 $this->param_array['_homepage'] = $this->homepage;
97 if ($force_all || !is_null($this->nickname)) {
98 $this->param_array['_nickname'] = $this->nickname;
101 if ($force_all || !is_null($this->license_url)) {
102 $this->param_array['_license'] = $this->license_url;
105 if ($force_all || !is_null($this->fullname)) {
106 $this->param_array['_fullname'] = $this->fullname;
109 if ($force_all || !is_null($this->bio)) {
110 $this->param_array['_bio'] = $this->bio;
113 if ($force_all || !is_null($this->location)) {
114 $this->param_array['_location'] = $this->location;
117 if ($force_all || !is_null($this->avatar_url)) {
118 $this->param_array['_avatar'] = $this->avatar_url;
123 foreach ($this->param_array as $k => $v) {
124 $ret[$prefix . $k] = $v;
130 * Builds an OMB_Profile object from array
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
137 * @param string $parameters An array containing the profile parameters.
138 * @param string $prefix The common prefix of the profile parameter keys.
142 * @returns OMB_Profile The built OMB_Profile.
144 public static function fromParameters($parameters, $prefix) {
145 if (!isset($parameters[$prefix])) {
146 throw new OMB_InvalidParameterException('', 'profile', $prefix);
149 $profile = new OMB_Profile($parameters[$prefix]);
150 $profile->updateFromParameters($parameters, $prefix);
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
162 * @param string $parameters An array containing the profile parameters.
163 * @param string $prefix The common prefix of the profile parameter keys.
167 public function updateFromParameters($parameters, $prefix) {
168 if (isset($parameters[$prefix.'_profile'])) {
169 $this->setProfileURL($parameters[$prefix.'_profile']);
172 if (isset($parameters[$prefix.'_license'])) {
173 $this->setLicenseURL($parameters[$prefix.'_license']);
176 if (isset($parameters[$prefix.'_nickname'])) {
177 $this->setNickname($parameters[$prefix.'_nickname']);
180 if (isset($parameters[$prefix.'_fullname'])) {
181 $this->setFullname($parameters[$prefix.'_fullname']);
184 if (isset($parameters[$prefix.'_homepage'])) {
185 $this->setHomepage($parameters[$prefix.'_homepage']);
188 if (isset($parameters[$prefix.'_bio'])) {
189 $this->setBio($parameters[$prefix.'_bio']);
192 if (isset($parameters[$prefix.'_location'])) {
193 $this->setLocation($parameters[$prefix.'_location']);
196 if (isset($parameters[$prefix.'_avatar'])) {
197 $this->setAvatarURL($parameters[$prefix.'_avatar']);
201 public function getIdentifierURI() {
202 return $this->identifier_uri;
205 public function getProfileURL() {
206 return $this->profile_url;
209 public function getHomepage() {
210 return $this->homepage;
213 public function getNickname() {
214 return $this->nickname;
217 public function getLicenseURL() {
218 return $this->license_url;
221 public function getFullname() {
222 return $this->fullname;
225 public function getBio() {
229 public function getLocation() {
230 return $this->location;
233 public function getAvatarURL() {
234 return $this->avatar_url;
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');
242 $this->profile_url = $profile_url;
243 $this->param_array = false;
246 public function setNickname($nickname) {
247 if (!Validate::string($nickname,
248 array('min_length' => 1,
250 'format' => VALIDATE_NUM . VALIDATE_ALPHA))) {
251 throw new OMB_InvalidParameterException($nickname, 'profile', 'nickname');
254 $this->nickname = $nickname;
255 $this->param_array = false;
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');
263 $this->license_url = $license_url;
264 $this->param_array = false;
267 public function setFullname($fullname) {
268 if ($fullname === '') {
270 } elseif (!Validate::string($fullname, array('max_length' => 255))) {
271 throw new OMB_InvalidParameterException($fullname, 'profile', 'fullname');
273 $this->fullname = $fullname;
274 $this->param_array = false;
277 public function setHomepage($homepage) {
278 if ($homepage === '') {
281 $this->homepage = $homepage;
282 $this->param_array = false;
285 public function setBio($bio) {
288 } elseif (!Validate::string($bio, array('max_length' => 140))) {
289 throw new OMB_InvalidParameterException($bio, 'profile', 'fullname');
292 $this->param_array = false;
295 public function setLocation($location) {
296 if ($location === '') {
298 } elseif (!Validate::string($location, array('max_length' => 255))) {
299 throw new OMB_InvalidParameterException($location, 'profile', 'fullname');
301 $this->location = $location;
302 $this->param_array = false;
305 public function setAvatarURL($avatar_url) {
306 if ($avatar_url === '') {
308 } elseif (!OMB_Helper::validateURL($avatar_url)) {
309 throw new OMB_InvalidParameterException($avatar_url, 'profile',
310 'omb_listenee_avatar or omb_listener_avatar');
312 $this->avatar_url = $avatar_url;
313 $this->param_array = false;