Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.00% covered (danger)
50.00%
18 / 36
44.44% covered (danger)
44.44%
4 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Validator
50.00% covered (danger)
50.00%
18 / 36
44.44% covered (danger)
44.44%
4 / 9
70.00
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isNotEmpty
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 isNotWhiteSpace
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 inLength
50.00% covered (danger)
50.00%
2 / 4
0.00% covered (danger)
0.00%
0 / 1
2.50
 inRange
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 isMatch
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 isNotMatch
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 isEmail
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 isWebsite
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\Core\Mvc;
6
7use PeServer\Core\Regex;
8use PeServer\Core\Text;
9use PeServer\Core\Mvc\IValidationReceiver;
10
11/**
12 * 共通検証処理。
13 */
14class Validator
15{
16    #region define
17
18    public const COMMON = Text::EMPTY;
19
20    public const KIND_EMPTY = 0;
21    public const KIND_WHITE_SPACE = 1;
22    public const KIND_LENGTH = 2;
23    public const KIND_RANGE = 3;
24    public const KIND_MATCH = 4;
25    public const KIND_EMAIL = 5;
26    public const KIND_WEBSITE = 6;
27
28    #endregion
29
30    #region variable
31
32    /**
33     * 検証移譲取得処理。
34     */
35    private IValidationReceiver $receiver;
36    private Regex $regex;
37
38    #endregion
39
40    public function __construct(IValidationReceiver $receiver)
41    {
42        $this->regex = new Regex();
43        $this->receiver = $receiver;
44    }
45
46    #region function
47
48    public function isNotEmpty(string $key, ?string $value): bool
49    {
50        if (Text::isNullOrEmpty($value)) {
51            $this->receiver->receiveErrorKind($key, self::KIND_EMPTY, ['VALUE' => Text::EMPTY]);
52            return false;
53        }
54
55        return true;
56    }
57
58    /**
59     * ホワイトスペース以外か。
60     *
61     * @param string $key
62     * @param string|null $value
63     * @return bool
64     * @phpstan-assert-if-true non-empty-string $value
65     */
66    public function isNotWhiteSpace(string $key, ?string $value): bool
67    {
68        if (Text::isNullOrWhiteSpace($value)) {
69            $this->receiver->receiveErrorKind($key, self::KIND_WHITE_SPACE, ['VALUE' => $value ? $value : Text::EMPTY]);
70            return false;
71        }
72
73        return true;
74    }
75
76
77    public function inLength(string $key, int $length, string $value): bool
78    {
79        if ($length < Text::getLength($value)) {
80            $this->receiver->receiveErrorKind($key, self::KIND_LENGTH, ['VALUE' => $value, 'SAFE_LENGTH' => $length, 'ERROR_LENGTH' => mb_strlen($value)]);
81            return false;
82        }
83
84        return true;
85    }
86
87    public function inRange(string $key, int $min, int $max, string $value): bool
88    {
89        $length = Text::getLength($value);
90        if ($length < $min || $max < $length) {
91            $this->receiver->receiveErrorKind($key, self::KIND_RANGE, ['VALUE' => $value, 'RANGE_MIN' => $min, 'RANGE_MAX' => $max, 'ERROR_LENGTH' => mb_strlen($value)]);
92            return false;
93        }
94
95        return true;
96    }
97
98    /**
99     * 正規表現にマッチするか。
100     *
101     * @param string $key
102     * @param string $pattern
103     * @phpstan-param literal-string $pattern
104     * @param string $value
105     * @return bool
106     */
107    public function isMatch(string $key, string $pattern, string $value): bool
108    {
109        if (!$this->regex->isMatch($value, $pattern)) {
110            $this->receiver->receiveErrorKind($key, self::KIND_MATCH, ['VALUE' => $value, 'PATTERN' => $pattern]);
111            return false;
112        }
113
114        return true;
115    }
116
117    /**
118     * 正規表現にマッチしないか。
119     *
120     * @param string $key
121     * @param string $pattern
122     * @phpstan-param literal-string $pattern
123     * @param string $value
124     * @return bool
125     */
126    public function isNotMatch(string $key, string $pattern, string $value): bool
127    {
128        if ($this->regex->isMatch($value, $pattern)) {
129            $this->receiver->receiveErrorKind($key, self::KIND_MATCH, ['value' => $value, 'pattern' => $pattern]);
130            return false;
131        }
132
133        return true;
134    }
135    public function isEmail(string $key, string $value): bool
136    {
137        if (filter_var($value, FILTER_VALIDATE_EMAIL)) {
138            return true;
139        }
140
141        $this->receiver->receiveErrorKind($key, self::KIND_EMAIL, ['VALUE' => $value]);
142
143        return false;
144    }
145
146    public function isWebsite(string $key, string $value): bool
147    {
148        if (filter_var($value, FILTER_VALIDATE_URL)) {
149            if (preg_match('|^https?://.+|', $value)) {
150                return true;
151            }
152        }
153
154        $this->receiver->receiveErrorKind($key, self::KIND_WEBSITE, ['VALUE' => $value]);
155
156        return false;
157    }
158
159    #endregion
160}