Sélectionner une révision Git
user.class.php 2,46 Kio
<?php
namespace BackEnd\User;
require_once 'class/BackEnd/DB/sqlmanager.class.php';
abstract class User
{
protected $id;
protected $name;
protected $phone;
protected $email;
protected $address;
protected $password;
protected $sql;
protected $type;
public function __construct(string $field, string $value)
{
$this->sql=\BackEnd\DB\SQLManager::getInstance()->getSQLInstance();
$bind = array(
':search' => $value
);
if($field === 'login')
{
$result = $this->sql->select("User", "login = :search", $bind);
} else if($field === 'id')
{
$result = $this->sql->select("User", "id = :search", $bind);
} else
{
throw new \Exception('Invalid type to find the user.');
}
if(empty($result))
{
throw new \Exception('User not found');
}
$this->id = $result[0]['id'];
$this->name = $result[0]['name'];
$this->phone = $result[0]['phone'];
$this->email = $result[0]['email'];
$this->address = $result[0]['address'];
$this->password = $result[0]['password'];
$this->type = $result[0]['type'];
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function getPhone()
{
return $this->phone;
}
public function getEmail()
{
return $this->email;
}
public function getAddress()
{
return $this->address;
}
public function getPassword()
{
return $this->password;
}
/* No setter for id: we shouldn't change id */
public function setName(string $name)
{
$this->name = $name;
$update = array("name" => $name);
$bind = array(":id" => $this->id);
$sql->update("User", $update, "id = :id", $bind);
}
public function setPhone(string $phone)
{
$this->phone = $phone;
$update = array(":phone" => $phone);
$bind = array(":id" => $this->id);
$sql->update("User", $update, ":id = $id", $bind);
}
public function setEmail(string $email)
{
$this->email = $email;
$update = array(":email" => $email);
$bind = array(":id" => $this->id);
$sql->update("User", $update, ":id = $id", $bind);
}
public function setAddress(string $address)
{
$this->address = $address;
$update = array(":address" => $address);
$bind = array(":id" => $this->id);
$sql->update("User", $update, ":id = $id", $bind);
}
public function setPassword(string $password)
{
$this->password = $password;
$update = array(":password" => $password);
$bind = array(":id" => $this->id);
$sql->update("User", $update, ":id = $id", $bind);
}
public abstract static function addUser(string $login, string $email, string $password, string $name, string $address, string $phone);
}