@@ 105-119 (lines=15) @@ | ||
102 | * @return string |
|
103 | * @throws \ValidationException |
|
104 | */ |
|
105 | public static function validate_url($data, $field, $options=[]) { |
|
106 | $options = array_merge([ |
|
107 | 'required' => true |
|
108 | ], $options); |
|
109 | $required = $options['required']; |
|
110 | if(isset($data[$field]) && is_string($data[$field])) { |
|
111 | $url = $data[$field]; |
|
112 | if(!self::is_url($url)) { |
|
113 | throw new \ValidationException("No valid url given"); |
|
114 | } |
|
115 | return $url; |
|
116 | } else if($required) { |
|
117 | throw new \ValidationException("No $field given, but $field is required"); |
|
118 | } |
|
119 | } |
|
120 | ||
121 | /** |
|
122 | * Validates an URL (defined in RFC 3986). |
|
@@ 166-180 (lines=15) @@ | ||
163 | * @return string |
|
164 | * @throws \ValidationException |
|
165 | */ |
|
166 | public static function validate_email($data, $field, $options=[]) { |
|
167 | $options = array_merge([ |
|
168 | 'required' => true |
|
169 | ], $options); |
|
170 | $required = $options['required']; |
|
171 | if(isset($data[$field]) && is_string($data[$field])) { |
|
172 | $email = $data[$field]; |
|
173 | if(\Email::is_valid_address($email) === 0) { |
|
174 | throw new \ValidationException("No valid email given"); |
|
175 | } |
|
176 | return $email; |
|
177 | } else if($required) { |
|
178 | throw new \ValidationException("No $field given, but $field is required"); |
|
179 | } |
|
180 | } |
|
181 | } |
|
182 |