Last active
January 31, 2021 02:19
-
-
Save sroehrl/3f1f47e87043aaaa61c3fe9198171db5 to your computer and use it in GitHub Desktop.
Basic user model for neoan3 v3.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"user": { | |
"id": { | |
"type": "binary(16)", | |
"key": "primary", | |
"nullable": false, | |
"default": false, | |
"a_i": false | |
}, | |
"insert_date": { | |
"type": "timestamp", | |
"key": false, | |
"nullable": true, | |
"default": "current_timestamp()", | |
"a_i": false | |
}, | |
"delete_date": { | |
"type": "datetime", | |
"key": false, | |
"nullable": true, | |
"default": false, | |
"a_i": false | |
}, | |
"email": { | |
"type": "varchar(60)", | |
"key": "unique", | |
"nullable": false, | |
"default": false, | |
"a_i": false | |
}, | |
"password": { | |
"type": "varchar(80)", | |
"key": false, | |
"nullable": false, | |
"default": false, | |
"a_i": false | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Generated by neoan3-cli */ | |
namespace Neoan3\Model\User; | |
use Exception; | |
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 create(array $modelArray) | |
* @method static update(array $modelArray) | |
* @method static find(array $conditionArray) | |
* @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 | |
* @throws Exception | |
*/ | |
public static function __callStatic($method, $args): mixed | |
{ | |
if(!method_exists(self::class, $method)){ | |
$transform = new Transform('user', self::$db); | |
return self::filterOutgoing($transform->$method(...$args)); | |
} else { | |
return self::$method(...$args); | |
} | |
} | |
/** | |
* @param $transactionResult | |
* @return array | |
* @throws Exception | |
*/ | |
public static function filterOutgoing($transactionResult): array | |
{ | |
if(isset($transactionResult['id'])){ | |
unset($transactionResult['password']); | |
return $transactionResult; | |
} elseif(!empty($transactionResult) && is_array($transactionResult)) { | |
$resultList = []; | |
foreach ($transactionResult as $single){ | |
$resultList[] = self::filterOutgoing($single); | |
} | |
return $resultList; | |
} | |
throw new Exception('model:retrieval', 400); | |
} | |
/** | |
* @param array $modelArray | |
* @return array | |
* @throws Exception | |
*/ | |
public static function register(array $modelArray): array | |
{ | |
$modelArray['password'] = '=' . password_hash($modelArray['password'], PASSWORD_DEFAULT); | |
$transform = new Transform('user', self::$db); | |
return self::filterOutgoing($transform->create($modelArray)); | |
} | |
/** | |
* @param $credentials | |
* @return mixed | |
* @throws Exception | |
*/ | |
public static function login($credentials): array | |
{ | |
$user = self::$db->easy('user.id user.password', ['email' => $credentials['email'], '^delete_date']); | |
if(empty($user)){ | |
throw new Exception('login:unknown', 401); | |
} | |
if(!password_verify($credentials['password'], $user[0]['password'])){ | |
throw new Exception('login:password', 401); | |
} | |
return self::get($user[0]['id']); | |
} | |
/** | |
* @param array $providers | |
*/ | |
public static function init(array $providers) | |
{ | |
foreach ($providers as $key => $provider){ | |
if($key === 'db'){ | |
self::$db = $provider; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment