X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=extlib%2Flibomb%2Fprofile.php;h=d732e10d7dfc71156b9866ee58c39bdd6f6a0d8a;hb=382756a6ada508131ab1408a94daf7617a8fa0fc;hp=13314d3e80bcebcbb440e40570fddaa405deb9b2;hpb=81aada21447b7561e759886b327aa7798a490bb2;p=quix0rs-gnu-social.git diff --git a/extlib/libomb/profile.php b/extlib/libomb/profile.php index 13314d3e80..d732e10d7d 100755 --- a/extlib/libomb/profile.php +++ b/extlib/libomb/profile.php @@ -1,15 +1,6 @@ . * - * @package OMB - * @author Adrian Lang - * @copyright 2009 Adrian Lang - * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0 - **/ - -class OMB_Profile { - protected $identifier_uri; - protected $profile_url; - protected $nickname; - protected $license_url; - protected $fullname; - protected $homepage; - protected $bio; - protected $location; - protected $avatar_url; - - /* The profile as OMB param array. Cached and rebuild on usage. - false while outdated. */ - protected $param_array; - - /** - * Constructor for OMB_Profile - * - * Initializes the OMB_Profile object with an identifier uri. - * - * @param string $identifier_uri The profile URI as defined by the OMB. A unique - * and unchanging identifier for a profile. - * - * @access public - */ - public function __construct($identifier_uri) { - if (!Validate::uri($identifier_uri)) { - throw new OMB_InvalidParameterException($identifier_uri, 'profile', + * @package OMB + * @author Adrian Lang + * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0 + * @version 0.1a-20090828 + * @link http://adrianlang.de/libomb + */ + +require_once 'invalidparameterexception.php'; +require_once 'Validate.php'; +require_once 'helper.php'; + +/** + * OMB profile representation + * + * This class represents an OMB profile. + * + * Do not call the setters with null values. Instead, if you want to delete a + * field, pass an empty string. The getters will return null for empty fields. + */ +class OMB_Profile +{ + protected $identifier_uri; + protected $profile_url; + protected $nickname; + protected $license_url; + protected $fullname; + protected $homepage; + protected $bio; + protected $location; + protected $avatar_url; + + /* The profile as OMB param array. Cached and rebuild on usage. + false while outdated. */ + protected $param_array; + + /** + * Constructor for OMB_Profile + * + * Initializes the OMB_Profile object with an identifier uri. + * + * @param string $identifier_uri The profile URI as defined by the OMB; + * A unique and never changing identifier for + * a profile + * + * @access public + */ + public function __construct($identifier_uri) + { + if (!Validate::uri($identifier_uri)) { + throw new OMB_InvalidParameterException($identifier_uri, 'profile', 'omb_listenee or omb_listener'); + } + $this->identifier_uri = $identifier_uri; + $this->param_array = false; } - $this->identifier_uri = $identifier_uri; - $this->param_array = false; - } - - /** - * Returns the profile as array - * - * The method returns an array which contains the whole profile as array. The - * array is cached and only rebuilt on changes of the profile. - * - * @param bool $force_all Specifies whether empty fields should be added to - * the array as well. This is neccessary to clear - * fields via updateProfile. - * - * @param string $prefix The common prefix to the key for all parameters. - * - * @access public - * - * @return array The profile as parameter array - */ - public function asParameters($prefix, $force_all = false) { - if ($this->param_array === false) { - $this->param_array = array('' => $this->identifier_uri); - - if ($force_all || !is_null($this->profile_url)) { - $this->param_array['_profile'] = $this->profile_url; - } - - if ($force_all || !is_null($this->homepage)) { - $this->param_array['_homepage'] = $this->homepage; - } - - if ($force_all || !is_null($this->nickname)) { - $this->param_array['_nickname'] = $this->nickname; - } - - if ($force_all || !is_null($this->license_url)) { - $this->param_array['_license'] = $this->license_url; - } - - if ($force_all || !is_null($this->fullname)) { - $this->param_array['_fullname'] = $this->fullname; - } - - if ($force_all || !is_null($this->bio)) { - $this->param_array['_bio'] = $this->bio; - } - - if ($force_all || !is_null($this->location)) { - $this->param_array['_location'] = $this->location; - } - - if ($force_all || !is_null($this->avatar_url)) { - $this->param_array['_avatar'] = $this->avatar_url; - } - } - $ret = array(); - foreach ($this->param_array as $k => $v) { - $ret[$prefix . $k] = $v; - } - return $ret; - } - - /** - * Builds an OMB_Profile object from array - * - * The method builds an OMB_Profile object from the passed parameters array. The - * array MUST provide a profile URI. The array fields HAVE TO be named according - * to the OMB standard. The prefix (omb_listener or omb_listenee) is passed as a - * parameter. - * - * @param string $parameters An array containing the profile parameters. - * @param string $prefix The common prefix of the profile parameter keys. - * - * @access public - * - * @returns OMB_Profile The built OMB_Profile. - */ - public static function fromParameters($parameters, $prefix) { - if (!isset($parameters[$prefix])) { - throw new OMB_InvalidParameterException('', 'profile', $prefix); + /** + * Return the profile as array + * + * Returns an array which contains the whole profile as array. + * The array is cached and only rebuilt on changes of the profile. + * + * @param string $prefix The common prefix to the key for all parameters + * @param bool $force_all Specifies whether empty fields should be added + * to the array as well; This is necessary to + * clear fields via updateProfile + * + * @access public + * + * @return array The profile as parameter array + */ + public function asParameters($prefix, $force_all = false) + { + if ($this->param_array === false) { + $this->param_array = array('' => $this->identifier_uri); + + if ($force_all || !is_null($this->profile_url)) { + $this->param_array['_profile'] = $this->profile_url; + } + + if ($force_all || !is_null($this->homepage)) { + $this->param_array['_homepage'] = $this->homepage; + } + + if ($force_all || !is_null($this->nickname)) { + $this->param_array['_nickname'] = $this->nickname; + } + + if ($force_all || !is_null($this->license_url)) { + $this->param_array['_license'] = $this->license_url; + } + + if ($force_all || !is_null($this->fullname)) { + $this->param_array['_fullname'] = $this->fullname; + } + + if ($force_all || !is_null($this->bio)) { + $this->param_array['_bio'] = $this->bio; + } + + if ($force_all || !is_null($this->location)) { + $this->param_array['_location'] = $this->location; + } + + if ($force_all || !is_null($this->avatar_url)) { + $this->param_array['_avatar'] = $this->avatar_url; + } + + } + $ret = array(); + foreach ($this->param_array as $k => $v) { + $ret[$prefix . $k] = $v; + } + return $ret; } - $profile = new OMB_Profile($parameters[$prefix]); - $profile->updateFromParameters($parameters, $prefix); - return $profile; - } - - /** - * Update from array - * - * Updates from the passed parameters array. The array does not have to - * provide a profile URI. The array fields HAVE TO be named according to the - * OMB standard. The prefix (omb_listener or omb_listenee) is passed as a - * parameter. - * - * @param string $parameters An array containing the profile parameters. - * @param string $prefix The common prefix of the profile parameter keys. - * - * @access public - */ - public function updateFromParameters($parameters, $prefix) { - if (isset($parameters[$prefix.'_profile'])) { - $this->setProfileURL($parameters[$prefix.'_profile']); + /** + * Build an OMB_Profile object from array + * + * Builds an OMB_Profile object from the passed parameters array. The + * array MUST provide a profile URI. The array fields HAVE TO be named + * according to the OMB standard. The prefix (omb_listener or omb_listenee) + * is passed as a parameter. + * + * @param string $parameters An array containing the profile parameters + * @param string $prefix The common prefix of the profile parameter keys + * + * @access public + * + * @returns OMB_Profile The built OMB_Profile + */ + public static function fromParameters($parameters, $prefix) + { + if (!isset($parameters[$prefix])) { + throw new OMB_InvalidParameterException('', 'profile', $prefix); + } + + $profile = new OMB_Profile($parameters[$prefix]); + $profile->updateFromParameters($parameters, $prefix); + return $profile; } - if (isset($parameters[$prefix.'_license'])) { - $this->setLicenseURL($parameters[$prefix.'_license']); + /** + * Update from array + * + * Updates from the passed parameters array. The array does not have to + * provide a profile URI. The array fields HAVE TO be named according to the + * OMB standard. The prefix (omb_listener or omb_listenee) is passed as a + * parameter. + * + * @param string $parameters An array containing the profile parameters + * @param string $prefix The common prefix of the profile parameter keys + * + * @access public + */ + public function updateFromParameters($parameters, $prefix) + { + if (isset($parameters[$prefix.'_profile'])) { + $this->setProfileURL($parameters[$prefix.'_profile']); + } + + if (isset($parameters[$prefix.'_license'])) { + $this->setLicenseURL($parameters[$prefix.'_license']); + } + + if (isset($parameters[$prefix.'_nickname'])) { + $this->setNickname($parameters[$prefix.'_nickname']); + } + + if (isset($parameters[$prefix.'_fullname'])) { + $this->setFullname($parameters[$prefix.'_fullname']); + } + + if (isset($parameters[$prefix.'_homepage'])) { + $this->setHomepage($parameters[$prefix.'_homepage']); + } + + if (isset($parameters[$prefix.'_bio'])) { + $this->setBio($parameters[$prefix.'_bio']); + } + + if (isset($parameters[$prefix.'_location'])) { + $this->setLocation($parameters[$prefix.'_location']); + } + + if (isset($parameters[$prefix.'_avatar'])) { + $this->setAvatarURL($parameters[$prefix.'_avatar']); + } } - if (isset($parameters[$prefix.'_nickname'])) { - $this->setNickname($parameters[$prefix.'_nickname']); + public function getIdentifierURI() + { + return $this->identifier_uri; } - if (isset($parameters[$prefix.'_fullname'])) { - $this->setFullname($parameters[$prefix.'_fullname']); + public function getProfileURL() + { + return $this->profile_url; } - if (isset($parameters[$prefix.'_homepage'])) { - $this->setHomepage($parameters[$prefix.'_homepage']); + public function getHomepage() + { + return $this->homepage; } - if (isset($parameters[$prefix.'_bio'])) { - $this->setBio($parameters[$prefix.'_bio']); + public function getNickname() + { + return $this->nickname; } - if (isset($parameters[$prefix.'_location'])) { - $this->setLocation($parameters[$prefix.'_location']); + public function getLicenseURL() + { + return $this->license_url; } - if (isset($parameters[$prefix.'_avatar'])) { - $this->setAvatarURL($parameters[$prefix.'_avatar']); + public function getFullname() + { + return $this->fullname; } - } - - public function getIdentifierURI() { - return $this->identifier_uri; - } - public function getProfileURL() { - return $this->profile_url; - } - - public function getHomepage() { - return $this->homepage; - } - - public function getNickname() { - return $this->nickname; - } - - public function getLicenseURL() { - return $this->license_url; - } + public function getBio() + { + return $this->bio; + } - public function getFullname() { - return $this->fullname; - } + public function getLocation() + { + return $this->location; + } - public function getBio() { - return $this->bio; - } + public function getAvatarURL() + { + return $this->avatar_url; + } - public function getLocation() { - return $this->location; - } + public function setProfileURL($profile_url) + { + $this->setVal('profile', $profile_url, 'OMB_Helper::validateURL', + 'profile_url'); + } - public function getAvatarURL() { - return $this->avatar_url; - } + public function setNickname($nickname) + { + $this->setVal('nickname', $nickname, 'OMB_Profile::validateNickname', + 'nickname', true); + } - public function setProfileURL($profile_url) { - if (!OMB_Helper::validateURL($profile_url)) { - throw new OMB_InvalidParameterException($profile_url, 'profile', - 'omb_listenee_profile or omb_listener_profile'); + public function setLicenseURL($license_url) + { + $this->setVal('license', $license_url, 'OMB_Helper::validateURL', + 'license_url'); } - $this->profile_url = $profile_url; - $this->param_array = false; - } - - public function setNickname($nickname) { - if (!Validate::string($nickname, - array('min_length' => 1, - 'max_length' => 64, - 'format' => VALIDATE_NUM . VALIDATE_ALPHA))) { - throw new OMB_InvalidParameterException($nickname, 'profile', 'nickname'); + + public function setFullname($fullname) + { + $this->setVal('fullname', $fullname, 'OMB_Profile::validate255'); } - $this->nickname = $nickname; - $this->param_array = false; - } + public function setHomepage($homepage) + { + $this->setVal('homepage', $homepage, 'OMB_Helper::validateURL'); + } - public function setLicenseURL($license_url) { - if (!OMB_Helper::validateURL($license_url)) { - throw new OMB_InvalidParameterException($license_url, 'profile', - 'omb_listenee_license or omb_listener_license'); + public function setBio($bio) + { + $this->setVal('bio', $bio, 'OMB_Profile::validate140'); } - $this->license_url = $license_url; - $this->param_array = false; - } - - public function setFullname($fullname) { - if ($fullname === '') { - $fullname = null; - } elseif (!Validate::string($fullname, array('max_length' => 255))) { - throw new OMB_InvalidParameterException($fullname, 'profile', 'fullname'); + + public function setLocation($location) + { + $this->setVal('location', $location, 'OMB_Profile::validate255'); } - $this->fullname = $fullname; - $this->param_array = false; - } - public function setHomepage($homepage) { - if ($homepage === '') { - $homepage = null; + public function setAvatarURL($avatar_url) + { + $this->setVal('avatar', $avatar_url, 'OMB_Helper::validateURL', + 'avatar_url'); } - $this->homepage = $homepage; - $this->param_array = false; - } - - public function setBio($bio) { - if ($bio === '') { - $bio = null; - } elseif (!Validate::string($bio, array('max_length' => 140))) { - throw new OMB_InvalidParameterException($bio, 'profile', 'fullname'); + + protected static function validate255($str) + { + return Validate::string($str, array('max_length' => 255)); } - $this->bio = $bio; - $this->param_array = false; - } - - public function setLocation($location) { - if ($location === '') { - $location = null; - } elseif (!Validate::string($location, array('max_length' => 255))) { - throw new OMB_InvalidParameterException($location, 'profile', 'fullname'); + + protected static function validate140($str) + { + return Validate::string($str, array('max_length' => 140)); } - $this->location = $location; - $this->param_array = false; - } - - public function setAvatarURL($avatar_url) { - if ($avatar_url === '') { - $avatar_url = null; - } elseif (!OMB_Helper::validateURL($avatar_url)) { - throw new OMB_InvalidParameterException($avatar_url, 'profile', - 'omb_listenee_avatar or omb_listener_avatar'); + + protected static function validateNickname($str) + { + return Validate::string($str, + array('min_length' => 1, + 'max_length' => 64, + 'format' => VALIDATE_NUM . VALIDATE_ALPHA)); } - $this->avatar_url = $avatar_url; - $this->param_array = false; - } + /** + * Set a value + * + * Updates a value specified by a parameter name and the new value. + * + * @param string $param The parameter name according to OMB + * @param string $value The new value + * @param callback $validator A validator function for the parameter + * @param string $field The name of the field in OMB_Profile + * @param bool $force Whether null values should be checked as well + */ + protected function setVal($param, $value, $validator, $field = null, + $force = false) + { + if (is_null($field)) { + $field = $param; + } + if ($value === '' && !$force) { + $value = null; + } elseif (!call_user_func($validator, $value)) { + throw new OMB_InvalidParameterException($value, 'profile', $param); + } + if ($this->$field !== $value) { + $this->$field = $value; + $this->param_array = false; + } + } } ?>