microserviciile de register, login finalizate

This commit is contained in:
Cerbu Andrei Mihnea
2023-06-15 22:21:53 +03:00
parent a739a66ae4
commit 4e585ba278
159 changed files with 4775 additions and 1747 deletions
Regular → Executable
+118 -103
View File
@@ -1,104 +1,119 @@
<?php
namespace app\core;
abstract class Model{
public const RULE_REQUIRED = 'required';
public const RULE_EMAIL = 'email';
public const RULE_MIN = 'min';
public const RULE_MAX = 'max';
public const RULE_UNIQUE = 'unique';
public function loadData($data){
foreach ($data as $key => $value){
if(property_exists($this, $key)){
$this->{$key} = $value;
}
}
}
abstract public function rules():array;
public function labels(): array{
return [];
}
public array $errors = [];
private function addErrorForRule(string $attribute, string $rule, $params =[]){
$message = $this->errorMessages()[$rule] ?? '';
foreach($params as $key => $value){
$message = str_replace("{{$key}}", $value, $message);
}
$this->errors[$attribute][] = $message;
}
public function addError(string $attribute, string $message){
$this->errors[$attribute][] = $message;
}
public function errorMessages(){
return [
self::RULE_REQUIRED => 'This field is required',
self::RULE_EMAIL => 'This field must be valid email address',
self::RULE_MIN => 'Min length of this field must be {min}',
self::RULE_MAX => 'Max length of this field must be {max}',
self::RULE_UNIQUE => 'Email already exists'
];
}
public function validate(){
foreach($this->rules() as $attribute => $rules){
$value = $this->{$attribute};
foreach($rules as $rule){
$ruleName = $rule;
if(!is_string($ruleName)){
$ruleName = $rule[0];
}
if($ruleName === self::RULE_REQUIRED && !$value){
$this->addErrorForRule($attribute, self::RULE_REQUIRED);
}
if($ruleName === self::RULE_EMAIL && !filter_var($value, FILTER_VALIDATE_EMAIL)){
$this->addErrorForRule($attribute, self::RULE_EMAIL);
}
if($ruleName === self::RULE_MIN && strlen($value) < $rule['min']){
$this->addErrorForRule($attribute, self::RULE_MIN, $rule);
}
if($ruleName === self::RULE_MAX && strlen($value) > $rule['max']){
$this->addErrorForRule($attribute, self::RULE_MAX, $rule);
}
if($ruleName === self::RULE_UNIQUE){
$className = $rule['class'];
$uniqueAttr = $rule['attirubute'] ?? $attribute;
$tableName = $className::tableName();
$statement = Application::$app->db->prepare(("SELECT * FROM $tableName WHERE $uniqueAttr = :attr"));
$statement->bindValue(":attr", $value);
$statement->execute();
$record = $statement->fetchObject();
if($record){
$this->addErrorForRule($attribute, SELF::RULE_UNIQUE);
}
}
}
}
return empty($this->errors);
}
public function hasError($attribute){
return $this->errors[$attribute] ?? false;
}
public function getFirstError($attribute){
return $this->errors[$attribute][0] ?? '';
}
<?php
namespace app\core;
use app\external_requests\CurlRequestBuilder;
abstract class Model{
public const RULE_REQUIRED = 'required';
public const RULE_EMAIL = 'email';
public array $errors = array();
private ?string $request = NULL;
private string $url = "192.168.0.13:701/authentication";
private ?array $headers = [];
private ?array $body = [];
private ?int $code = NULL;
private ?string $message = NULL;
public function loadData($data): void
{
foreach ($data as $key => $value){
if(property_exists($this, $key)){
$this->{$key} = $value;
}
}
}
abstract public function rules(): array;
public abstract function labels(): array;
public function errorMessages(): array{
return [
self::RULE_REQUIRED => 'This field is required',
self::RULE_EMAIL => 'This field must be a valid email address',
];
}
public function checkForErrors(): void{
foreach($this->rules() as $field => $rules){
$value = $this->{$field};
foreach ($rules as $rule){
if($rule == self::RULE_REQUIRED && !strlen($value)) {
$this->addError($field, $this->errorMessages()[$rule]);
break;
}
if($rule == self::RULE_EMAIL && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
$this->addError($field, $this->errorMessages()[$rule]);
break;
}
}
}
}
protected abstract function createBody(): array;
protected abstract function createHeaders(string $body): array;
protected abstract function formatHeaderArray(array $headers): array;
public function setParametersForRequest(string $request, array $headers, array $body): void
{
$this->request = $request;
$this->headers = $headers;
$this->body = $body;
}
public function execute(): void
{
$requestBuilder = new CurlRequestBuilder();
$requestBuilder->setRequest($this->request);
$requestBuilder->setUrl($this->url);
$requestBuilder->setHeaders($this->headers);
$requestBuilder->setBody($this->body);
$requestBuilder->setIsReturnable(true);
$curl = $requestBuilder->getCurlRequestObject();
$response = json_decode($curl->makeRequest(), true);
$this->code = $response["code"];
$this->message = $response["message"];
}
public function isRequestFulfilled(): bool
{
return $this->code == DVC::$successReturnCode;
}
public function setRequestError(): void
{
Application::$app->view->setActionError($this->message);
}
public function addError($attribute, $message): void
{
$this->errors[$attribute] = $message;
}
public function removeErrors(): void
{
unset($this->errors);
$this->errors = array();
}
public function hasErrors(): bool
{
return count($this->errors);
}
public function getError($field): ?string
{
return array_key_exists($field, $this->errors) ? $this->errors[$field] : NULL;
}
public function getValueForField($field):string
{
return $this->{$field};
}
}