Add UserValidationByMatrixUsername.
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-08-19 12:19:24 +03:00
parent 1a7c585aa7
commit c9974b007a
5 changed files with 91 additions and 53 deletions
+13 -17
View File
@@ -8,7 +8,9 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
use App\Models\Validation\ValidationByNameInterface;
class User extends Authenticatable implements ValidationByNameInterface
{
use HasApiTokens, HasFactory, Notifiable;
@@ -23,24 +25,18 @@ class User extends Authenticatable
'phone',
];
public static function validateWithMatrixUsername(string $matrixUsername)
public static function isExistByName(string $name): bool
{
$matrixUsername = $matrixUsername ?? '';
$count = User::where('matrix_username', $name)->count();
if($matrixUsername == '')
return [
'error' => 'The username is empty, please, write username!!!'
];
return $count != 0;
}
$user = User::where('matrix_username', $matrixUsername)->first();
if(!$user)
return [
'error' => 'A user with the username does not exist!!!'
];
return [
'ok' => 'A user with the username is valid.'
];
public static function isNameValid(string $name): bool
{
return $name != '';
}
}
@@ -0,0 +1,20 @@
<?php
namespace App\Models\Validation\Messages;
use App\Models\Validation\Messages\BaseMessages;
class UserMessagesFactory
{
protected array $messages = [
'found' => 'A user with the username is valid.',
'not_found' => 'A user with the username does not exist!!!',
'invalid_name' => 'The username is empty, please, write username!!!',
];
public function create()
{
return new BaseMessages($this->messages);
}
}
@@ -0,0 +1,18 @@
<?php
namespace App\Models\Validation;
use App\Models\Validation\ModelValidationByName;
use App\Models\Validation\Messages\UserMessagesFactory;
class UserValidationByMatrixUsername extends ModelValidationByName
{
public function __construct(string $name)
{
parent::__construct(
$name,
'App\Models\User',
(new UserMessagesFactory())->create(),
);
}
}