1: <?php
2:
3: namespace Charcoal\Validator;
4:
5: // Dependencies from `PHP`
6: use \DateTime as DateTime;
7: use \DateTimeInterface as DateTimeInterface;
8: use \InvalidArgumentException as InvalidArgumentException;
9:
10: /**
11: * A Validator Result object.
12: */
13: class ValidatorResult
14: {
15: /**
16: * @var string $ident
17: */
18: private $ident;
19:
20: /**
21: * Can be `notice`, `warning` or `error`
22: * @var string $level
23: */
24: private $level;
25:
26: /**
27: * @var string $message
28: */
29: private $message;
30:
31: /**
32: * @var DateTime $ts
33: */
34: private $ts;
35:
36: /**
37: * @param array|\ArrayAccess $data Optional data.
38: */
39: public function __construct($data = null)
40: {
41: $ts = new DateTime();
42: $this->setTs($ts);
43:
44: if (is_array($data) || ($data instanceof \ArrayAccess)) {
45: $this->setData($data);
46: }
47: }
48:
49: /**
50: * @param array $data The validator result data.
51: * @return ValidatorResult Chainable
52: */
53: public function setData(array $data)
54: {
55: if (isset($data['ident'])) {
56: $this->setIdent($data['ident']);
57: }
58: if (isset($data['level']) && $data['message'] !== null) {
59: $this->setLevel($data['level']);
60: }
61: if (isset($data['message']) && $data['message'] !== null) {
62: $this->setMessage($data['message']);
63: }
64: if (isset($data['ts']) && $data['ts'] !== null) {
65: $this->setTs($data['ts']);
66: }
67: return $this;
68: }
69:
70: /**
71: * @param string $ident The result identigier.
72: * @throws InvalidArgumentException If parameter is not valid.
73: * @return ValidatorResult
74: */
75: public function setIdent($ident)
76: {
77: if (!is_string($ident)) {
78: throw new InvalidArgumentException(
79: 'Ident must be a string.'
80: );
81: }
82: $this->ident = $ident;
83: return $this;
84: }
85:
86: /**
87: * @return string
88: */
89: public function ident()
90: {
91: return $this->ident;
92: }
93:
94: /**
95: * @param string $level The validation level ('notice', 'warning' or 'error').
96: * @throws InvalidArgumentException If parameter is not a valid level.
97: * @return ValidatorResult
98: */
99: public function setLevel($level)
100: {
101: if (!is_string($level)) {
102: throw new InvalidArgumentException(
103: 'Level must be a string.'
104: );
105: }
106: if (!in_array($level, ['notice', 'warning', 'error'])) {
107: throw new InvalidArgumentException(
108: 'Level can only be notice, warning or error.'
109: );
110: }
111: $this->level = $level;
112: return $this;
113: }
114:
115: /**
116: * @return string
117: */
118: public function level()
119: {
120: return $this->level;
121: }
122:
123: /**
124: * @param string $message The validation message.
125: * @throws InvalidArgumentException If parameter is not valid.
126: * @return ValidatorResult
127: */
128: public function setMessage($message)
129: {
130: if (!is_string($message)) {
131: throw new InvalidArgumentException(
132: 'Message must be a string.'
133: );
134: }
135: $this->message = $message;
136: return $this;
137: }
138:
139: /**
140: * @return string
141: */
142: public function message()
143: {
144: return $this->message;
145: }
146:
147: /**
148: * @param string|DateTime $ts The datetime value.
149: * @throws InvalidArgumentException If parameter is not valid "datetime".
150: * @return ValidatorResult
151: */
152: public function setTs($ts)
153: {
154: if (is_string($ts)) {
155: $ts = new DateTime($ts);
156: }
157: if (!($ts instanceof DateTimeInterface)) {
158: throw new InvalidArgumentException(
159: 'Invalid "Timestamp" value. Must be a date/time string or a DateTime object.'
160: );
161: }
162: $this->ts = $ts;
163: return $this;
164: }
165:
166: /**
167: * @return DateTime
168: */
169: public function ts()
170: {
171: return $this->ts;
172: }
173: }
174: