Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class User {
- private $data;
- private $errors;
- public CONST FIELD_OPTION = [
- 'user_login' => [
- 'callback' => 'is_user_login_exists',
- 'error_message' => 'is already exists',
- 'response' => true
- ],
- 'user_email' => [
- 'callback' => 'is_email_exists',
- 'error_message' => 'Email is already exists',
- 'response' => true
- ],
- 'user_pwd' => [
- 'callback' => 'is_password_valid',
- 'error_message' => 'Password is not valid',
- 'response' => false
- ]
- ];
- function __construct( array $data = [] ) {
- $this->dataParser( $data );
- }
- /**
- *
- */
- public function get( array $filters = [] ) {
- $db = db();
- $query = $db->dsql()->table('users');
- if(!empty($filters) && is_array($filters)) {
- foreach($filters as $key) {
- try {
- $value = $this->getData($key, true);
- $query->where($key, $value);
- }
- catch(Exception $e) {
- $this->errors['data'][$key] = $e->getMessage();
- }
- }
- }
- return $query->get();
- }
- /**
- * Create new user
- */
- public function create() {
- $db = db();
- $data = [
- 'user_pwd' => $this->getData('user_pwd'),
- 'user_login' => $this->getData('user_login'),
- 'user_email' => $this->getData('user_email'),
- 'user_name' => $this->getData('user_name')
- ];
- $query = $db->dsql()->table('users')->set($data);
- $insert = $query->insert();
- return $insert;
- }
- /**
- * Verifying all fields
- *
- * @param mixed(bool on failire|null on success) $fields
- */
- public function vertify_fields( $requireds ) {
- if(is_string($requireds)) {
- $requireds = [$requireds];
- }
- if(!is_array($requireds)) {
- return false;
- }
- $fields = self::FIELD_OPTION;
- foreach( $requireds as $field_name ) {
- $field = $fields[$field_name];
- $value = $this->getData($field_name);
- if( empty($value) ) {
- $this->errors['fields'][$field_name] = 'This field is required';
- continue;
- }
- if($field['response'] === call_user_func_array($field['callback'],[$value])) {
- $this->errors['fields'][$field_name] = $field['error_message'];
- }
- }
- }
- public function identify() {
- /**
- * We identify user by :
- * @string $user_login
- * @string $user_pwd
- */
- $user_login = $this->getData('user_login');
- $user_pwd = $this->getData('user_pwd');
- $db = db();
- $query = $db->dsql()->table('users')->where('user_login', $user_login)->limit(1);
- $user = $query->getRow();
- if(!$user) {
- return false;
- }
- $user_pwd_hash = $user['user_pwd'];
- return password_verify($user_pwd, $user_pwd_hash);
- }
- /**
- * Return mixed(null|array)
- */
- public function get_errors() {
- return $this->errors;
- }
- /**
- * check if @prop $errors is null or not
- */
- public function have_errors() : bool {
- return ($this->errors === null) ? false : true;
- }
- /**
- *
- * @param mixed $key
- * @return string
- */
- private function getData( $key, bool $force = false ) {
- $value = $this->data->$key ?? null;
- if( $force && is_null($value) ) {
- throw new Exception($key . ' is missing');
- }
- return $value;
- }
- private function dataParser( array $data ) : void {
- $this->data = (object) $data;
- }
- }
- function create_user_ajax() {
- $response = ['status' => 'error'];
- $User = new User($_POST);
- $User->vertify_fields(['user_email', 'user_login', 'user_pwd']);
- if($User->have_errors()) {
- $response['errors'] = $User->get_errors();
- }else{
- $User->create();
- $response['status'] = 'success';
- }
- echo json_encode($response);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement