Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
39.29% covered (danger)
39.29%
11 / 28
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
AccountValidator
39.29% covered (danger)
39.29%
11 / 28
50.00% covered (danger)
50.00%
3 / 6
38.08
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isLoginId
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 isPassword
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 isUserName
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 isDescription
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 isFreeLoginId
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\App\Models\Domain;
6
7use PeServer\Core\I18n;
8use PeServer\Core\TrueKeeper;
9use PeServer\Core\Mvc\Validator;
10use PeServer\Core\Text;
11use PeServer\Core\Mvc\IValidationReceiver;
12use PeServer\Core\Database\IDatabaseContext;
13use PeServer\App\Models\Domain\ValidatorBase;
14use PeServer\App\Models\Dao\Entities\UsersEntityDao;
15
16class AccountValidator extends ValidatorBase
17{
18    public const LOGIN_ID_RANGE_MIN = 6;
19    public const LOGIN_ID_RANGE_MAX = 50;
20    public const PASSWORD_RANGE_MIN = 8;
21    public const PASSWORD_RANGE_MAX = 50;
22    public const USER_NAME_RANGE_MIN = 4;
23    public const USER_NAME_RANGE_MAX = 100;
24    public const USER_DESCRIPTION_LENGTH = 1000;
25
26    public function __construct(IValidationReceiver $receiver, Validator $validator)
27    {
28        parent::__construct($receiver, $validator);
29    }
30
31    public function isLoginId(string $key, ?string $value): bool
32    {
33        if ($this->validator->isNotWhiteSpace($key, $value)) {
34            $trueKeeper = new TrueKeeper();
35
36            $trueKeeper->state = $this->validator->inRange($key, self::LOGIN_ID_RANGE_MIN, self::LOGIN_ID_RANGE_MAX, $value);
37            $trueKeeper->state = $this->validator->isMatch($key, '/^[a-zA-Z0-9\\-\\._]+$/', $value);
38
39            return $trueKeeper->state;
40        }
41
42        return false;
43    }
44
45    public function isPassword(string $key, ?string $value): bool
46    {
47        if ($this->validator->isNotWhiteSpace($key, $value)) {
48            $trueKeeper = new TrueKeeper();
49
50            $trueKeeper->state = $this->validator->inRange($key, self::PASSWORD_RANGE_MIN, self::PASSWORD_RANGE_MAX, $value);
51            $trueKeeper->state = $this->validator->isMatch($key, '/^[a-zA-Z0-9!-~]+$/', $value);
52
53            return $trueKeeper->state;
54        }
55
56        return false;
57    }
58
59    public function isUserName(string $key, ?string $value): bool
60    {
61        if ($this->validator->isNotWhiteSpace($key, $value)) {
62            $trueKeeper = new TrueKeeper();
63
64            $trueKeeper->state = $this->validator->inRange($key, self::USER_NAME_RANGE_MIN, self::USER_NAME_RANGE_MAX, $value);
65
66            return $trueKeeper->state;
67        }
68
69        return false;
70    }
71
72    public function isDescription(string $key, ?string $value): bool
73    {
74        if (!Text::isNullOrWhiteSpace($value)) {
75            $trueKeeper = new TrueKeeper();
76
77            $trueKeeper->state = $this->validator->inLength($key, self::USER_DESCRIPTION_LENGTH, $value);
78
79            return $trueKeeper->state;
80        }
81
82        return true;
83    }
84
85
86    public function isFreeLoginId(IDatabaseContext $context, string $key, string $loginId): bool
87    {
88        $usersEntityDao = new UsersEntityDao($context);
89
90        if ($usersEntityDao->selectExistsLoginId($loginId)) {
91            $this->receiver->receiveErrorMessage($key, I18n::message('error/unusable_login_id'));
92            return false;
93        }
94
95        return true;
96    }
97}