Advertisement
Guest User

Untitled

a guest
Feb 16th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. class User
  2. {
  3. public $id;
  4. public $email;
  5. public $password;
  6. public $errors;
  7.  
  8. public function isValid()
  9. {
  10. if (strpos($this->email, '@') === false) {
  11. $this->errors['email'] = 'Please enter an email address';
  12. }
  13. if (!$this->password) {
  14. $this->errors['password'] = 'Please enter a password';
  15. } elseif (strlen($this->password) < 4) {
  16. $this->errors['password'] = 'Please enter a longer password';
  17. }
  18.  
  19. return !$this->errors;
  20. }
  21. }
  22.  
  23. class UserDAO
  24. {
  25. protected $conn;
  26. protected $logger;
  27.  
  28. public function __construct(PDO $dbh, Logger $logger)
  29. {
  30. $this->dbh = $dbh;
  31. $this->logger = $logger;
  32. }
  33.  
  34. public function getUsers()
  35. {
  36. $rows = null;
  37. try {
  38. $rows = $this->dbh->query("SELECT * FROM users")->fetchAll();
  39. } catch (PDOException $e) {
  40. $this->logger->log($e->getMessage(), __METHOD__);
  41. }
  42. return $rows;
  43. }
  44.  
  45. public function getUserById($id)
  46. {
  47. $row = null;
  48. try {
  49. $sth = $this->dbh->prepare("SELECT * FROM users WHERE id = ?");
  50. $sth->execute(array($id));
  51. $row = $sth->fetchObject('User');
  52. } catch (PDOException $e) {
  53. $this->logger->log($e->getMessage(), __METHOD__);
  54. }
  55. return $row;
  56. }
  57.  
  58. public function addUser(User &$user)
  59. {
  60. $success = false;
  61. try {
  62. $sth = $this->dbh->prepare("
  63. INSERT INTO users (email, password) VALUES (?, ?)
  64. ");
  65. $sth->execute(array($user->email, $user->password));
  66. if ($success = (bool) $sth->rowCount()) {
  67. $user->id = $this->dbh->lastInsertId();
  68. }
  69. } catch (PDOException $e) {
  70. $this->logger->log($e->getMessage(), __METHOD__);
  71. }
  72. return $success;
  73. }
  74.  
  75. public function updateUser(User $user)
  76. {
  77. // ...
  78. }
  79.  
  80. public function deleteUser($id)
  81. {
  82. // ...
  83. }
  84.  
  85. public function isEmailUnique($email)
  86. {
  87. $count = 0;
  88. try {
  89. $sth = $this->dbh->prepare("SELECT COUNT(id) FROM users WHERE email = LOWER(?)");
  90. $sth->execute(array($email));
  91. $count = $sth->fetchColumn();
  92. } catch (PDOException $e) {
  93. $this->logger->log($e->getMessage(), __METHOD__);
  94. }
  95. return !$count;
  96. }
  97. }
  98.  
  99. // ...
  100.  
  101. $userDAO = new UserDAO($dbh, $logger);
  102. $user = new User();
  103. $user->email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
  104. $user->password = filter_input(INPUT_POST, 'password');
  105. // validate user
  106. if ($user->isValid()) {
  107. // check if email address is unique (SO UGLY!)
  108. if ($userDAO->isEmailUnique($user->email)) {
  109. $user->errors['email'] = 'Please use a different email address';
  110. }
  111. // save user
  112. if ($user->addUser($user)) {
  113. // ...
  114. } else {
  115. // ...
  116. }
  117. } else {
  118. // do something with $user->errors
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement