Created
September 9, 2012 17:37
-
-
Save radutopala/3685963 to your computer and use it in GitHub Desktop.
User class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* @author Radu Topala <radu.topala@trisoft.ro> | |
*/ | |
class User | |
{ | |
protected $id; | |
/** | |
* @var string | |
*/ | |
protected $username; | |
/** | |
* @var string | |
*/ | |
protected $email; | |
/** | |
* @var string | |
*/ | |
protected $password; | |
/** | |
* @var string | |
*/ | |
protected $firstName; | |
/** | |
* @var string | |
*/ | |
protected $lastName; | |
/** | |
* @var \DateTime | |
*/ | |
protected $lastLogin; | |
/** | |
* @var array | |
*/ | |
protected $roles; | |
/** | |
* Some data array example for __get/__set | |
* | |
* @var array | |
*/ | |
private $data; | |
public function __construct() | |
{ | |
$this->roles = array(); | |
} | |
public function getFirstName($firstName) | |
{ | |
return $this->firstName; | |
} | |
public function getLastName($lastName) | |
{ | |
return $this->lastName; | |
} | |
public function setFirstName($firstName) | |
{ | |
$this->firstName = $firstName; | |
return $this; | |
} | |
public function setLastName($lastName) | |
{ | |
$this->lastName = $lastName; | |
return $this; | |
} | |
public static function isValidPassword($pwd) | |
{ | |
$valid = true; | |
if( strlen($pwd) < 8 ) { | |
$errors[]= "Password too short! <br />"; | |
} | |
if( strlen($pwd) > 20 ) { | |
$errors[]= "Password too long! <br />"; | |
} | |
if( !preg_match("#[0-9]+#", $pwd) ) { | |
$errors[]= "Password must include at least one number! <br />"; | |
} | |
if( !preg_match("#[a-z]+#", $pwd) ) { | |
$errors[]= "Password must include at least one letter! <br />"; | |
} | |
if( !preg_match("#[A-Z]+#", $pwd) ) { | |
$errors[]= "Password must include at least one CAPS! <br />"; | |
} | |
if( !preg_match("#\W+#", $pwd) ) { | |
$errors[]= "Password must include at least one symbol! <br />"; | |
} | |
$valid = isset($errors)?false:true; | |
return $valid; | |
} | |
/** | |
* Adds a role to the user. | |
* | |
* @param string $role | |
* | |
* @return User | |
*/ | |
public function addRole($role) | |
{ | |
$role = strtoupper($role); | |
if (!in_array($role, $this->roles, true)) { | |
$this->roles[] = $role; | |
} | |
return $this; | |
} | |
/** | |
* Returns the user unique id. | |
* | |
* @return mixed | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* @return string | |
*/ | |
public function getUsername() | |
{ | |
return $this->username; | |
} | |
/** | |
* Gets email. | |
* | |
* @return string | |
*/ | |
public function getEmail() | |
{ | |
return $this->email; | |
} | |
/** | |
* Gets the encrypted password. | |
* | |
* @return string | |
*/ | |
public function getPassword() | |
{ | |
return $this->password; | |
} | |
/** | |
* Gets the last login time. | |
* | |
* @return \DateTime | |
*/ | |
public function getLastLogin() | |
{ | |
return $this->lastLogin; | |
} | |
/** | |
* Returns the user roles | |
* | |
* @return array The roles | |
*/ | |
public function getRoles() | |
{ | |
return $this->roles; | |
} | |
/** | |
* @param string $role | |
* | |
* @return Boolean | |
*/ | |
public function hasRole($role) | |
{ | |
return in_array(strtoupper($role), $this->getRoles(), true); | |
} | |
/** | |
* Removes a role to the user. | |
* | |
* @param string $role | |
*/ | |
public function removeRole($role) | |
{ | |
if (false !== $key = array_search(strtoupper($role), $this->roles, true)) { | |
unset($this->roles[$key]); | |
$this->roles = array_values($this->roles); | |
} | |
} | |
/** | |
* Sets the username. | |
* | |
* @param string $username | |
* | |
* @return User | |
*/ | |
public function setUsername($username) | |
{ | |
$this->username = $username; | |
return $this; | |
} | |
/** | |
* Sets the email. | |
* | |
* @param string $email | |
* | |
* @return User | |
*/ | |
public function setEmail($email) | |
{ | |
$this->email = $email; | |
return $this; | |
} | |
/** | |
* Sets the hashed password. | |
* | |
* @param string $password | |
* | |
* @return User | |
*/ | |
public function setPassword($password) | |
{ | |
$this->password = $password; | |
return $this; | |
} | |
/** | |
* Sets the last login time | |
* | |
* @param \DateTime $time | |
* | |
* @return User | |
*/ | |
public function setLastLogin(\DateTime $time) | |
{ | |
$this->lastLogin = $time; | |
return $this; | |
} | |
/** | |
* Sets the roles of the user. | |
* | |
* This overwrites any previous roles. | |
* | |
* @param array $roles | |
* | |
* @return User | |
*/ | |
public function setRoles(array $roles) | |
{ | |
$this->roles = array(); | |
foreach ($roles as $role) { | |
$this->addRole($role); | |
} | |
return $this; | |
} | |
public function __toString() | |
{ | |
return (string) $this->getUsername(); | |
} | |
public function __set($name, $value) | |
{ | |
$this->data[$name] = $value; | |
} | |
public function __get($name) | |
{ | |
if (array_key_exists($name, $this->data)) { | |
return $this->data[$name]; | |
} | |
} | |
} | |
$test = new User(); | |
$test->setUsername('radutopala'); | |
$test->setFirstName('Radu'); | |
$test->setLastName('Topala'); | |
echo "Password xyz is ".(User::isValidPassword('xyz')?'valid':'invalid')."<br>"; | |
echo "Password xyz12 is ".(User::isValidPassword('xyz12')?'valid':'invalid')."<br>"; | |
echo "Password xyz12H$6 is ".(User::isValidPassword('xyz12H$6')?'valid':'invalid')."<br>"; | |
//Setting a private property through __set | |
$test->alfa = 'xyz'; | |
echo "Accessing a private property through __get: ".$test->alfa."<br>"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment