Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. <?php
  2.  
  3. class User {
  4.  
  5. private $data;
  6. private $errors;
  7. public CONST FIELD_OPTION = [
  8. 'user_login' => [
  9. 'callback' => 'is_user_login_exists',
  10. 'error_message' => 'is already exists',
  11. 'response' => true
  12. ],
  13. 'user_email' => [
  14. 'callback' => 'is_email_exists',
  15. 'error_message' => 'Email is already exists',
  16. 'response' => true
  17. ],
  18. 'user_pwd' => [
  19. 'callback' => 'is_password_valid',
  20. 'error_message' => 'Password is not valid',
  21. 'response' => false
  22. ]
  23. ];
  24.  
  25. function __construct( array $data = [] ) {
  26. $this->dataParser( $data );
  27. }
  28.  
  29. /**
  30. *
  31. */
  32. public function get( array $filters = [] ) {
  33.  
  34. $db = db();
  35.  
  36. $query = $db->dsql()->table('users');
  37.  
  38. if(!empty($filters) && is_array($filters)) {
  39. foreach($filters as $key) {
  40. try {
  41. $value = $this->getData($key, true);
  42. $query->where($key, $value);
  43. }
  44.  
  45. catch(Exception $e) {
  46. $this->errors['data'][$key] = $e->getMessage();
  47. }
  48.  
  49. }
  50. }
  51.  
  52. return $query->get();
  53.  
  54. }
  55.  
  56. /**
  57. * Create new user
  58. */
  59. public function create() {
  60.  
  61. $db = db();
  62.  
  63. $data = [
  64. 'user_pwd' => $this->getData('user_pwd'),
  65. 'user_login' => $this->getData('user_login'),
  66. 'user_email' => $this->getData('user_email'),
  67. 'user_name' => $this->getData('user_name')
  68. ];
  69.  
  70. $query = $db->dsql()->table('users')->set($data);
  71.  
  72. $insert = $query->insert();
  73.  
  74. return $insert;
  75.  
  76. }
  77.  
  78. /**
  79. * Verifying all fields
  80. *
  81. * @param mixed(bool on failire|null on success) $fields
  82. */
  83. public function vertify_fields( $requireds ) {
  84.  
  85. if(is_string($requireds)) {
  86. $requireds = [$requireds];
  87. }
  88.  
  89. if(!is_array($requireds)) {
  90. return false;
  91. }
  92.  
  93. $fields = self::FIELD_OPTION;
  94.  
  95. foreach( $requireds as $field_name ) {
  96.  
  97. $field = $fields[$field_name];
  98. $value = $this->getData($field_name);
  99. if( empty($value) ) {
  100. $this->errors['fields'][$field_name] = 'This field is required';
  101. continue;
  102. }
  103.  
  104. if($field['response'] === call_user_func_array($field['callback'],[$value])) {
  105. $this->errors['fields'][$field_name] = $field['error_message'];
  106. }
  107.  
  108. }
  109.  
  110. }
  111.  
  112. public function identify() {
  113. /**
  114. * We identify user by :
  115. * @string $user_login
  116. * @string $user_pwd
  117. */
  118. $user_login = $this->getData('user_login');
  119. $user_pwd = $this->getData('user_pwd');
  120.  
  121. $db = db();
  122.  
  123. $query = $db->dsql()->table('users')->where('user_login', $user_login)->limit(1);
  124.  
  125. $user = $query->getRow();
  126.  
  127. if(!$user) {
  128. return false;
  129. }
  130.  
  131. $user_pwd_hash = $user['user_pwd'];
  132. return password_verify($user_pwd, $user_pwd_hash);
  133.  
  134. }
  135.  
  136. /**
  137. * Return mixed(null|array)
  138. */
  139. public function get_errors() {
  140. return $this->errors;
  141. }
  142.  
  143. /**
  144. * check if @prop $errors is null or not
  145. */
  146. public function have_errors() : bool {
  147. return ($this->errors === null) ? false : true;
  148. }
  149.  
  150. /**
  151. *
  152. * @param mixed $key
  153. * @return string
  154. */
  155. private function getData( $key, bool $force = false ) {
  156. $value = $this->data->$key ?? null;
  157. if( $force && is_null($value) ) {
  158. throw new Exception($key . ' is missing');
  159. }
  160.  
  161. return $value;
  162.  
  163. }
  164.  
  165. private function dataParser( array $data ) : void {
  166. $this->data = (object) $data;
  167. }
  168.  
  169. }
  170.  
  171. function create_user_ajax() {
  172.  
  173. $response = ['status' => 'error'];
  174.  
  175. $User = new User($_POST);
  176.  
  177. $User->vertify_fields(['user_email', 'user_login', 'user_pwd']);
  178.  
  179. if($User->have_errors()) {
  180. $response['errors'] = $User->get_errors();
  181. }else{
  182. $User->create();
  183. $response['status'] = 'success';
  184. }
  185.  
  186. echo json_encode($response);
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement