Completed
Branch 0.3.0 (b16461)
by Anton
04:03
created

Form::handle()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 8.8571
cc 5
eloc 6
nc 5
nop 2
1
<?php
2
3
namespace Utils {
4
5
	use Utils\Messages, Utils\Popup, Language;
6
7
	abstract class Form extends \Form {
8
9
		# Display error
10
11
		private function displayError(string $code, bool $popup) {
12
13
			$text = Language::get($code);
14
15
			if (!$popup) Messages::set('error', $text); else Popup::set('negative', $text);
16
17
			# ------------------------
18
19
			return false;
20
		}
21
22
		# Handle form
23
24
		public function handle(callable $callback, bool $popup = false) {
25
26
			if (false === ($post = $this->post())) return false;
27
28
			# Check form for errors
29
30
			if ($this->errors()) return $this->displayError('FORM_ERROR_REQUIRED', $popup);
31
32
			# Call controller method
33
34
			if (is_string($result = $callback($post))) return $this->displayError($result, $popup);
35
36
			if (is_array($result)) { $this->get($result[0])->error(true); return $this->displayError($result[1], $popup); }
37
38
			# ------------------------
39
40
			return (true === $result);
41
		}
42
	}
43
}
44