Can parse user information into User object

This commit is contained in:
Pierre 2018-04-15 13:55:51 +02:00
parent 86c1cf12c5
commit 1a2ab4f520
3 changed files with 211 additions and 71 deletions

View File

@ -30,10 +30,12 @@ class UserComponent {
* Get Single User Infos * Get Single User Infos
* *
* @param int $userID The user ID * @param int $userID The user ID
* @param bool $advanced Get advanced informations about user, for its page for example * @param bool $advanced Get advanced information about user, for its page for example
* @return Array The result of the function (user informations) (empty one if it fails) * @return User Information about the user (invalid object in case of failure)
* Notice : If advanced information are request, then the User object can be casted to
* AdvancedUser object.
*/ */
public function getUserInfos(int $userID, bool $advanced = false) : array { public function getUserInfos(int $userID, bool $advanced = false) : User {
//Prepare database request //Prepare database request
$tablesName = self::USER_TABLE; $tablesName = self::USER_TABLE;
$conditions = "WHERE utilisateurs.ID = ?"; $conditions = "WHERE utilisateurs.ID = ?";
@ -48,8 +50,11 @@ class UserComponent {
if(count($userInfos) == 0) if(count($userInfos) == 0)
return array(); //No result return array(); //No result
//Return result //Return parsed result
return $this->generateUserInfosArray($userInfos[0], $advanced); if(!$advanced)
return $this->parseDbToUser($userInfos[0]);
else
return $this->parseDbToAdvancedUser($userInfos[0]);
} }
/** /**
@ -86,57 +91,6 @@ class UserComponent {
return $result; return $result;
} }
/**
* Generate and return an array containing informations about a user
* given the database entry
*
* @param array $userInfos The user entry in the database
* @param bool $advanced Get advanced informations about user or not (to display its profile for example)
* @return array The informations ready to be returned
*/
private function generateUserInfosArray(array $userInfos, bool $advanced = false) : array{
//Prepare return
$return = array();
$return['userID'] = $userInfos['ID'];
$return['firstName'] = $userInfos['prenom'];
$return['lastName'] = $userInfos['nom'];
$return['publicPage'] = $userInfos['public'] == 1;
$return['openPage'] = $userInfos['pageouverte'] == 1;
$return['virtualDirectory'] = $userInfos['sous_repertoire'];
//Add account image url
$return['accountImage'] = CS::get()->components->accountImage->getPath($return['userID']);
//Check if we have to fetch advanced informations
if($advanced){
//Public friend list
$return['friend_list_public'] = $userInfos['liste_amis_publique'] == 1;
//Personnal website
$return['personnalWebsite'] = $userInfos['site_web'];
//Block comment his his page ?
$return['noCommentOnHisPage'] = $userInfos['bloquecommentaire'] == 1;
//Allow user to post informations on his page
$return['allowPostFromFriendOnHisPage'] = $userInfos['autoriser_post_amis'] == 1;
//Account creation date
$return['account_creation_time'] = strtotime($userInfos['date_creation']);
//Add background image url
$return['backgroundImage'] = CS::get()->components->backgroundImage->getPath($return['userID']);
//Get the number of likes of the page
$return['pageLikes'] = CS::get()->components->likes->count($return['userID'], Likes::LIKE_USER);
}
//Return result
return $return;
}
/** /**
* Update last user activity time on the network * Update last user activity time on the network
* *
@ -385,6 +339,107 @@ class UserComponent {
return $result[0]["liste_amis_publique"] == 1; return $result[0]["liste_amis_publique"] == 1;
} }
/**
* Generate and return an array containing informations about a user
* given the database entry
*
* @param array $userInfos The user entry in the database
* @param bool $advanced Get advanced informations about user or not (to display its profile for example)
* @return array The informations ready to be returned
*/
private function generateUserInfosArray(array $userInfos, bool $advanced = false) : array{
//Prepare return
$return = array();
$return['userID'] = $userInfos['ID'];
$return['firstName'] = $userInfos['prenom'];
$return['lastName'] = $userInfos['nom'];
$return['publicPage'] = $userInfos['public'] == 1;
$return['openPage'] = $userInfos['pageouverte'] == 1;
$return['virtualDirectory'] = $userInfos['sous_repertoire'];
//Add account image url
$return['accountImage'] = CS::get()->components->accountImage->getPath($return['userID']);
//Check if we have to fetch advanced informations
if($advanced){
//Public friend list
$return['friend_list_public'] = $userInfos['liste_amis_publique'] == 1;
//Personnal website
$return['personnalWebsite'] = $userInfos['site_web'];
//Block comment his his page ?
$return['noCommentOnHisPage'] = $userInfos['bloquecommentaire'] == 1;
//Allow user to post informations on his page
$return['allowPostFromFriendOnHisPage'] = $userInfos['autoriser_post_amis'] == 1;
//Account creation date
$return['account_creation_time'] = strtotime($userInfos['date_creation']);
//Add background image url
$return['backgroundImage'] = CS::get()->components->backgroundImage->getPath($return['userID']);
//Get the number of likes of the page
$return['pageLikes'] = CS::get()->components->likes->count($return['userID'], Likes::LIKE_USER);
}
//Return result
return $return;
}
/**
* Parse user database entry into user object
*
* @param array $entry The database entry, as an array
* @param User $user The user object to populate
* @return User Generated User object
*/
public function parseDbToUser(array $entry, User $user = null) : User {
//Create user object if required
if($user == null)
$user = new User;
$user->set_id($entry['ID']);
$user->set_firstName($entry['prenom']);
$user->set_lastName($entry['nom']);
$user->set_publicPage($entry['public'] == 1);
$user->set_virtualDirectory($entry['sous_repertoire']);
$user->set_accountImageURL(
CS::get()->components->accountImage->getPath($user->get_id()));
//Return generated user
return $user;
}
/**
* Parse user database entry into advanced user information object
*
* @param array $entry The database entry, as an array
* @return AdvancedUser Advanced information about the user
*/
private function parseDbToAdvancedUser(array $entry) : AdvancedUser {
//Parse general information about the user
$user = $this->parseDbToUser($entry, new Advanceduser());
$user->set_friendListPublic($entry['liste_amis_publique'] == 1);
$user->set_personnalWebsite($entry['site_web']);
$user->set_disallowComments($entry['bloquecommentaire'] == 1);
$user->set_allowPostFromFriends($entry['autoriser_post_amis'] == 1);
$user->set_creation_time(strtotime($entry['date_creation']));
$user->set_backgroundImage(
CS::get()->components->backgroundImage->getPath($user->get_id()));
$user->set_pageLikes(
CS::get()->components->likes->count($user->get_id(), Likes::LIKE_USER));
//Return result
return $user;
}
} }
//Register class //Register class

View File

@ -0,0 +1,85 @@
<?php
/**
* Advanced user information
*
* @author Pierre HUBERT
*/
//User class must be loaded
require_once __DIR__."/User.php";
class AdvancedUser extends User {
//Private fields
private $friendListPublic;
private $personnalWebsite;
private $disallowComments;
private $allowPostFromFriends;
private $creation_time;
private $backgroundImage;
private $pageLikes;
//Get and set friend list visibility level
public function set_friendListPublic(bool $friendListPublic){
$this->friendListPublic = $friendListPublic;
}
public function is_friendListPublic() : bool {
return $this->friendListPublic;
}
//Get and set the personnal website of the user
public function set_personnalWebsite(string $personnalWebsite){
$this->personnalWebsite = $personnalWebsite == "" ? null : $personnalWebsite;
}
public function get_personnalWebsite() : string {
return $this->personnalWebsite != null ? $this->personnalWebsite : "null";
}
//Get and set comment status on user page
public function set_disallowComments(bool $disallowComments){
$this->disallowComments = $disallowComments;
}
public function is_disallowComments() : bool {
return $this->disallowComments;
}
//Get and set the allow posts on user page from friend status
public function set_allowPostFromFriends(bool $allowPostFromFriends){
$this->allowPostFromFriends = $allowPostFromFriends;
}
public function is_allowPostFromFriends() : bool {
return $this->allowPostFromFriends;
}
//Set and get the account creation time
public function set_creation_time(int $creation_time){
$this->creation_time = $creation_time;
}
public function get_creation_time() : int {
return $this->creation_time;
}
//Set and get the background image of the user
public function set_backgroundImage(string $backgroundImage){
$this->backgroundImage = $backgroundImage == "" ? null : $backgroundImage;
}
public function get_backgroundImage() : string {
return $this->backgroundImage != null ? $this->backgroundImage : "null";
}
//Get and set the number of likes on user page
public function set_pageLikes(int $pageLikes){
$this->pageLikes = $pageLikes;
}
public function get_pageLikes() : int {
return $this->pageLikes;
}
}

View File

@ -27,69 +27,69 @@ class User {
} }
//Set and get user ID //Set and get user ID
private function set_id(int $id){ public function set_id(int $id){
$this->id = $id; $this->id = $id;
} }
private function get_id() : int { public function get_id() : int {
return $this->id; return $this->id;
} }
//Set and get the first name of the user //Set and get the first name of the user
private function set_firstName(string $firstName){ public function set_firstName(string $firstName){
$this->firstName = $firstName; $this->firstName = $firstName;
} }
private function get_firstName() : string { public function get_firstName() : string {
return $this->firstName; return $this->firstName;
} }
//Set and get the last name of the user //Set and get the last name of the user
private function set_lastName(string $lastName){ public function set_lastName(string $lastName){
$this->lastName = $lastName; $this->lastName = $lastName;
} }
private function get_lastName() : string { public function get_lastName() : string {
return $this->lastName; return $this->lastName;
} }
//Set and get the public status of the user page //Set and get the public status of the user page
private function set_publicPage(bool $publicPage){ public function set_publicPage(bool $publicPage){
$this->publicPage = $publicPage; $this->publicPage = $publicPage;
} }
private function is_publicPage() : bool { public function is_publicPage() : bool {
return $this->publicPage; return $this->publicPage;
} }
//Set and get the open status of the user page //Set and get the open status of the user page
private function set_openPage(bool $openPage){ public function set_openPage(bool $openPage){
$this->openPage = $openPage; $this->openPage = $openPage;
} }
private function is_openPage() : bool { public function is_openPage() : bool {
return $this->openPage; return $this->openPage;
} }
//Set and get the virtual directory of the user //Set and get the virtual directory of the user
private function set_virtualDirectory(string $virtualDirectory){ public function set_virtualDirectory(string $virtualDirectory){
$this->virtualDirectory = $virtualDirectory; $this->virtualDirectory = $virtualDirectory == "" ? null : $virtualDirectory;
} }
private function get_virtualDirectory() : string { public function get_virtualDirectory() : string {
return $this->virtualDirectory != null ? $this->virtualDirectory : "null"; return $this->virtualDirectory != null ? $this->virtualDirectory : "null";
} }
//Set and get the URL pointing of the user account image //Set and get the URL pointing of the user account image
private function set_accountImageURL(string $accountImageURL){ public function set_accountImageURL(string $accountImageURL){
$this->accountImageURL = $accountImageURL; $this->accountImageURL = $accountImageURL;
} }
private function get_accountImageURL() : string { public function get_accountImageURL() : string {
return $this->accountImageURL; return $this->accountImageURL;
} }