-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserModel.php
115 lines (102 loc) · 2.88 KB
/
UserModel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/* Generated by neoan3-cli */
namespace Neoan3\Model\User;
use Neoan3\Core\RouteException;
use Neoan3\Provider\MySql\Database;
use Neoan3\Provider\Model\Model;
use Neoan3\Provider\MySql\Transform;
/**
* Class UserModel
* @package Neoan3\Model\User
* @method static get(string $id)
* @method static update(array $modelArray, array $modifier = [])
* @method static find(array $conditionArray, array $callFunctions = [])
* @method static delete(string $id, bool $hard = false)
*/
class UserModel implements Model{
/**
* @var Database|null
*/
private static ?Database $db = null;
/**
* @param $method
* @param $args
* @return mixed
*/
public static function __callStatic($method, $args)
{
if(!method_exists(self::class, $method)){
$transform = new Transform('user', self::$db);
return self::out($transform->$method(...$args));
} else {
return self::out(self::$method(...$args));
}
}
public static function out($model)
{
if(isset($model['password'])){
unset($model['password']);
} else {
foreach ($model as $i => $item){
unset($model[$i]['password']);
}
}
return $model;
}
/**
* @throws RouteException
*/
public static function login($credentials)
{
$credentials = self::validate($credentials);
$find = self::$db->easy('user.id user.password',['email'=>$credentials['email']]);
if(empty($find)){
throw new RouteException('unauthorized',401);
}
if(!password_verify($credentials['password'], $find[0]['password'])){
throw new RouteException('unauthorized',401);
}
return self::get($find[0]['id']);
}
/**
* @throws RouteException
*/
public static function create($modelArray)
{
return self::register($modelArray);
}
/**
* @throws RouteException
*/
public static function register($credentials)
{
// validate
$credentials = self::validate($credentials);
$credentials['password'] = '=' . password_hash($credentials['password'], PASSWORD_DEFAULT);
return self::create($credentials);
}
/**
* @throws RouteException
*/
private static function validate($input): array
{
if(!isset($input['email']) || !isset($input['password'])){
throw new RouteException('Bad request: set "email" and "password"',400);
}
return [
'email' => $input['email'],
'password' => $input['password']
];
}
/**
* @param array $providers
*/
public static function init(array $providers)
{
foreach ($providers as $key => $provider){
if($key === 'db'){
self::$db = $provider;
}
}
}
}