PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/extlib/libomb/profile.php

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