Summary Files Commits Branches @commit @diff @stats
path: root / Year 4 / Web technologies / hw03.php
blob: c7a591eb62134e09e21a4bb33d168a4c7435e06b (plain) (blame)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
abstract class Form {
    const Name        = 'name';
    const Teacher     = 'teacher';
    const Description = 'description';
    const Group       = 'group';
    const Credits     = 'credits';

    static function to_bg(string $name) {
        switch ($name) {
            case Form::Name:        return 'Име на учебен предмет';
            case Form::Teacher:     return 'Преподавател';
            case Form::Description: return 'Описание';
            case Form::Group:       return 'Група';
            case Form::Credits:     return 'Кредити';
            default: throw new Exception("'$name' isn't an acceptable value!");
        }
    }
}

class Errors {
    public $name;
    public $teacher;
    public $description;
    public $group;
    public $credits;

    private static function get_error_submitted(string $name) {
        if (!array_key_exists($name, $_POST) || mb_strlen($_POST[$name]) == 0) {
            return Form::to_bg($name) . ' не е предадено! Полето е задължително!';
        }
    }

    private static function get_error_len_min(string $name, int $min) {
        if (mb_strlen($_POST[$name]) < $min) {
            return Form::to_bg($name) . " е твърде късо! Дължината може да е минимум $min на брой символа!";
        }
    }

    private static function get_error_len_max(string $name, int $max) {
        if (mb_strlen($_POST[$name]) > $max) {
            return Form::to_bg($name) . " е твърде дълго! Дължината може да е максимум $max на брой символа!";
        }
    }

    private static function get_error_len(string $name, int $min, int $max) {
        return Errors::get_error_len_min($name, $min) ??
               Errors::get_error_len_max($name, $max);
    }

    private static function get_error_int(string $name) {
        if (!is_numeric($_POST[$name])) {
            return Form::to_bg($name) . " не е число!";
        }
        elseif (floor($_POST[$name]) != $_POST[$name]) {
            return Form::to_bg($name) . " не е цяло число!";
        }
        elseif ($_POST[$name] <= 0) {
            return Form::to_bg($name) . " не е положително число!";
        }
    }

    public function __construct() {
        $this->name = $this->get_error_submitted(Form::Name) ??
                      $this->get_error_len(Form::Name, 2, 150);

        $this->teacher = $this->get_error_submitted(Form::Teacher) ??
                         $this->get_error_len(Form::Teacher, 3, 200);

        $this->description = $this->get_error_submitted(Form::Description) ??
                             $this->get_error_len_min(Form::Description, 10);

        $this->group = $this->get_error_submitted(Form::Group);
        if (is_null($this->group)) {
            switch ($_POST[Form::Group]) {
                case 'М': case 'ПМ': case 'ОКН': case 'ЯКН': break;
                default: $this->group = Form::to_bg(Form::Group) . ' не е измежду М, ПМ, ОКН и ЯКН!';
            }
        }

        $this->credits = $this->get_error_submitted(Form::Credits) ??
                         $this->get_error_int(Form::Credits);
    }

    public function has_any() {
        return !is_null($this->name) ||
               !is_null($this->teacher) ||
               !is_null($this->description) ||
               !is_null($this->group) ||
               !is_null($this->credits);
    }

    public function to_array() {
        $output = array();
        if (!is_null($this->name))        { $output[Form::Name] = $this->name; }
        if (!is_null($this->teacher))     { $output[Form::Teacher] = $this->teacher; }
        if (!is_null($this->description)) { $output[Form::Description] = $this->description; }
        if (!is_null($this->group))       { $output[Form::Group] = $this->group; }
        if (!is_null($this->credits))     { $output[Form::Credits] = $this->credits; }
        return $output;
    }
}

$errors = new Errors();

if ($errors->has_any()) {
    echo json_encode(
        array(
            "success" => false,
            "errors" => $errors->to_array()
        ),
        JSON_UNESCAPED_UNICODE
    );
}
else {
    echo json_encode(array("success" => true));
}