Me gustaría escuchar algunas opiniones referentes al diseño como tal de las siguientes clases, que simplemente buscan abstraer de todo proceso que haga dependiente al objeto usuario con características definidas en la base de datos.
Para ello se resume en una clase esclavo (que a fin de cuentas es un intermediario) donde una propiedad de esta misma es el objeto sobre el cual recaen sus funciones:
Transaction.php
Código PHP:
interface Transaction {
public function setConnection(Connection $connection);
public function toSave();
public function toEdit();
public function toDelete();
public function isExist();
}
Profile.php
Código PHP:
<?php
class Profile {
private $id;
private $email;
private $password;
private $nickname;
private $firstname;
private $lastname;
private $sex;
private $birthday;
private $date;
private $about;
private $state;
private $session;
public function __construct($id){
$this->id = $id;
$this->email = NULL;
$this->password = NULL;
$this->nickname = null;
$this->firstname = NULL;
$this->lastname = NULL;
$this->sex = NULL;
$this->birthday = NULL;
$this->date = NULL;
$this->about = NULL;
$this->state = TRUE;
$this->address = NULL;
$this->now = NULL;
}
public function getId() {
return $this->id;
}
public function getEmail() {
return $this->email;
}
public function setEmail($email) {
$this->email = $email;
return $this;
}
public function getPassword() {
return $this->password;
}
public function setPassword($password) {
$this->password = $password;
return $this;
}
public function getNickname() {
return $this->nickname;
}
public function setNickname($nickname) {
$this->nickname = $nickname;
return $this;
}
public function getFirstname() {
return $this->firstname;
}
public function setFirstname($firstname) {
$this->firstname = $firstname;
return $this;
}
public function getLastname() {
return $this->lastname;
}
public function setLastname($lastname) {
$this->lastname = $lastname;
return $this;
}
public function getSex() {
return $this->sex;
}
public function setSex($sex) {
$this->sex = $sex;
return $this;
}
public function getBirthday() {
return $this->birthday;
}
public function setBirthday(Array $birthday) {
$this->birthday = $birthday;
return $this;
}
public function getDate() {
return $this->date;
}
public function setDate($date) {
$this->date = $date;
return $this;
}
public function getAbout() {
return $this->about;
}
public function setAbout($about) {
$this->about = $about;
return $this;
}
public function getState() {
return $this->state;
}
public function setState($state) {
$this->state = $state;
return $this;
}
public function getSession() {
return $this->session;
}
public function toSession(Array $session){
$this->session = $session;
return $this;
}
}
?>
Profiling.php (clase esclavo)
Código PHP:
<?php
class Profiling implements Transaction{
private $profile;
private $connection;
public function __construct(Profile $profile){
$this->profile = $profile;
}
public function getProfile(){
return $this->profile;
}
public function getConnection(){
return $this->connection;
}
public function setConnection(Connection $connection) {
$this->connection = $connection;
}
public function toSave() {
return $this->connection->query("INSERT INTO profile
VALUES ({$this->profile->getId()},
'{$this->profile->getFirstname()}',
'{$this->profile->getLastname()}',
'{$this->profile->getEmail()}',
'{$this->profile->getPassword()}',
{$this->profile->getSex()},
'".implode('-',
$this->profile->getBirthday())."',
'{$this->profile->getDate()}',
1,
'Tester',
NULL)");
}
public function toEdit() {
}
public function toDelete() {
}
public function isExist() {
$result = $this->connection->query("SELECT p.id
FROM profile p
WHERE p.email LIKE '{$this->profile->getEmail()}'
AND p.state = 1");
return !(empty($result->num_rows));
}
public function toSaveSession(){
list($address, $datetime) = $this->profile->getSession();
return $this->connection->query("INSERT INTO session
VALUES (NULL,
{$this->profile->getId()},
'{$address}',
'{$datetime}')");
}
}
?>
En cuestión hay una clase esclavo por cada objeto, dependiendo de la función del objeto.
Gracias de antemano.