Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ValidatorMultiple.php
Go to the documentation of this file.
1<?php
2
4
9
10abstract class ValidatorMultiple extends Validator implements HasNotNullInterface {
11 protected $violation;
12 protected $notNull;
13
14 public function __construct() {
15 $this->message = [ 'notNull' => 'This value should not be null' ];
16 $this->notNull = false;
17 }
18
19 public function validate($value) {
20 if (null == $value) {
21 if ($this->notNull === true) {
22 $this->violation = 'notNull';
23 return false;
24 } else {
25 return;
26 }
27 }
28 if (! UString::isValid ( $value )) {
29 throw new ValidatorException ( 'This value can not be converted to string' );
30 }
31 return true;
32 }
33
38 protected function mergeMessages() {
39 if (! isset ( $this->modifiedMessage )) {
40 return $this->message;
41 } else {
42 if (\is_array ( $this->modifiedMessage ) && \is_array ( $this->message )) {
43 return \array_merge ( $this->message, $this->modifiedMessage );
44 } else {
46 }
47 }
48 }
49
50 protected function _getMessage() {
51 $parameters = $this->getParameters ();
52 $message = $this->mergeMessages ();
53 if (isset ( $this->violation ) && \is_array ( $message )) {
55 }
56 foreach ( $parameters as $param ) {
57 $message = \str_replace ( '{' . $param . '}', $this->$param??'', $message );
58 }
59 return $message;
60 }
61
62 protected function _getMessageViolation($messages) {
63 if (isset ( $messages [$this->violation] )) {
64 return $messages [$this->violation];
65 }
66 return \current ( $messages );
67 }
68
69 public function asUI(): array {
70 if ($this->notNull) {
71 return [ 'rules' => [[ 'type'=>'empty','prompt'=>$this->mergeMessages()['notNull']??'' ]] ];
72 }
73 return [ ];
74 }
75}
76
String utilities.
Definition UString.php:15