Passed
Push — master ( 34f20f...a12a6d )
by Enjoys
57s queued 12s
created

Email::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Forms\Rule;
6
7
use Enjoys\Forms\Element;
8
use Enjoys\Forms\Interfaces\Ruleable;
9
use Enjoys\Forms\Traits\Request;
10
11
class Email implements RuleInterface
12
{
13
    use Request;
14
15
//    private $idn_to_ascii = false;
16
17
    private string $message;
18
19 3
    public function __construct(?string $message = null)
20
    {
21 3
        $this->message = $message ?? 'Не правильно введен email';
22
    }
23
24
25
26
    /**
27
     * @psalm-suppress PossiblyNullReference
28
     * @param Ruleable&Element $element
29
     * @return bool
30
     */
31 3
    public function validate(Ruleable $element): bool
32
    {
33 3
        $method = $this->getRequest()->getRequest()->getMethod();
34 3
        $requestData = match (strtolower($method)) {
35 3
            'get' => $this->getRequest()->getQueryData()->toArray(),
36
            'post' => $this->getRequest()->getPostData()->toArray(),
37
            default => []
38
        };
39
        /** @var string $value */
40 3
        $value = \getValueByIndexPath($element->getName(), $requestData);
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Enjoys\Forms\Interfaces\Ruleable. Since it exists in all sub-types, consider adding an abstract or default implementation to Enjoys\Forms\Interfaces\Ruleable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        $value = \getValueByIndexPath($element->/** @scrutinizer ignore-call */ getName(), $requestData);
Loading history...
41 3
        if ($this->check(\trim($value)) === false) {
42 1
            $element->setRuleError($this->message);
43 1
            return false;
44
        }
45 2
        return true;
46
    }
47
48
49 3
    private function check(string $value): bool
50
    {
51 3
        if (empty($value)) {
52 1
            return true;
53
        }
54
55
//        if ($this->idn_to_ascii === true) {
56
//            $value = idn_to_ascii($value);
57
//        }
58
59 2
        return (bool)filter_var($value, \FILTER_VALIDATE_EMAIL);
60
    }
61
}
62