/
form.php
55 lines (49 loc) · 1.4 KB
/
form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
$name = htmlspecialchars($_POST['name']);
$phone = htmlspecialchars($_POST['phone']);
$validate = new Validate($name, $phone);
echo json_encode($validate->getValidate(), JSON_UNESCAPED_UNICODE);
exit();
/**
* Class Validate
*/
class Validate
{
private $name;
private $phone;
public $result = [];
public function __construct(string $name, string $phone)
{
$this->phone = $phone;
$this->name = $name;
}
/**
* Проверка на заполнение телефона или email
* @return array
*/
public function getValidate()
{
if (!$this->name && !$this->phone){
return $this->getMessage(true, 'Заполните имя и телефон');
} elseif (!$this->name){
return $this->getMessage(true, 'Заполните имя');
} elseif (!$this->phone){
return $this->getMessage(true, 'Заполните телефон');
} else {
return $this->getMessage(false, 'Сообщение отправлено');
}
}
/**
* Выводит сообщение о результате
* @param bool $error
* @param string $message
* @return array
*/
public function getMessage(bool $error, string $message)
{
return $this->result = [
'error' => $error,
'message' => $message
];
}
}