Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
Validator
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
7 / 7
12
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validate
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
6
 getMsg
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDomainCatalogLoader
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setMsg
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isRequired
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setRequired
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setMsgContent
n/a
0 / 0
n/a
0 / 0
0
 testValue
n/a
0 / 0
n/a
0 / 0
0
1<?php
2
3/**
4 * @author: Doug Wilbourne (dougwilbourne@gmail.com)
5 */
6declare(strict_types=1);
7
8namespace pvc\validator;
9
10use pvc\interfaces\msg\DomainCatalogLoaderInterface;
11use pvc\interfaces\msg\MsgInterface;
12use pvc\interfaces\validator\ValidatorInterface;
13use pvc\validator\messages\ValidatorCatalogLoader;
14
15/**
16 * Class ValidatorAbstract
17 * @template DataType
18 * @implements ValidatorInterface<DataType>
19 */
20abstract class Validator implements ValidatorInterface
21{
22    /**
23     * @var MsgInterface
24     */
25    protected MsgInterface $msg;
26
27    /**
28     * @var bool
29     */
30    protected bool $required = true;
31
32    /**
33     * @param MsgInterface $msg
34     */
35    public function __construct(MsgInterface $msg)
36    {
37        $this->setMsg($msg);
38    }
39
40    /**
41     * validate
42     * @param DataType $data
43     * @return bool
44     */
45    public function validate($data = null): bool
46    {
47        /**
48         * initialize message each time, so it is correct if validate is called multiple times in succession
49         */
50        $this->getMsg()->clearContent();
51
52        /**
53         * return true if data is not required and null was passed
54         */
55        if (!$this->isRequired() && is_null($data)) {
56            return true;
57        }
58
59        /**
60         * return false if date is required and data is null.  Empty is not good enough (e.g. anything that evaluates
61         * to false) - must explicitly be null.
62         */
63        if ($this->isRequired() && is_null($data)) {
64            $msgId = 'not_null';
65            $parameters = [];
66            $domain = 'Validator';
67            $this->getMsg()->setContent($domain, $msgId, $parameters);
68            return false;
69        }
70
71        /**
72         * test the value
73         */
74        if (!$this->testValue($data)) {
75            $this->setMsgContent();
76            return false;
77        }
78        return true;
79    }
80
81    /**
82     * getMsg
83     * @return MsgInterface
84     */
85    public function getMsg(): MsgInterface
86    {
87        return $this->msg;
88    }
89
90    /**
91     * @return DomainCatalogLoaderInterface
92     */
93    public function getDomainCatalogLoader(): DomainCatalogLoaderInterface
94    {
95        return new ValidatorCatalogLoader();
96    }
97
98
99    /**
100     * setMsg
101     * @param MsgInterface $msg
102     */
103    public function setMsg(MsgInterface $msg): void
104    {
105        $this->msg = $msg;
106    }
107
108    /**
109     * @return bool
110     */
111    public function isRequired(): bool
112    {
113        return $this->required;
114    }
115
116    /**
117     * @param bool $required
118     */
119    public function setRequired(bool $required): void
120    {
121        $this->required = $required;
122    }
123
124    /**
125     * setMsgContent
126     */
127    abstract protected function setMsgContent(): void;
128
129    /**
130     * testValue
131     * @param $data<DataType>
132     * @return bool
133     */
134    abstract protected function testValue(mixed $data): bool;
135}