-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.php
More file actions
166 lines (140 loc) · 5.78 KB
/
Controller.php
File metadata and controls
166 lines (140 loc) · 5.78 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
use Symfony\Component\Validator\Constraints\GreaterThan;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Validation;
$__department = __DIR__ . '/Department.php';
require_once $__department;
$__employee = __DIR__ . '/Employee.php';
require_once $__employee;
$__tools = __DIR__ . '/tools.php';
require_once $__tools;
class EmployeeController
{
private int $current_id = 0;
private function validate_field(mixed $field, array $constraints): array
{
$validator = Validation::createValidator();
$violations = [];
$violations_in_salary = $validator->validate($field, $constraints);
foreach ($violations_in_salary as $error_in_salary) {
$violations[] = (string)$error_in_salary;
}
return $violations;
}
private function validate(Employee $employee): string
{
$violations = [];
$violations = array_merge($violations, $this->validate_field($employee->getSalary(), [
new GreaterThan(100),
new NotBlank()
]));
$violations = array_merge($violations, $this->validate_field($employee->getName(), [
new Length(null, 2),
new NotBlank()
]));
$violations = array_merge($violations, $this->validate_field($employee->getDateStartWork(), [
new GreaterThan(1000),
new NotBlank()
]));
$violations = array_merge($violations, $this->validate_field($employee->getId(), [
new GreaterThan(-1),
new NotBlank()
]));
if (count($violations) > 0) {
$violations_string = implode("<br>", $violations);
return ($violations_string);
}
return
'<html><body>Employee added. Id = ' . $employee->getId() . '. Name = ' . $employee->getName() . '. Salary = ' . $employee->getSalary() . '. Experience = ' . $employee->getExperienceTime() . ' years. </body></html>';
}
public function create(): string
{
$name = getIfSet("name", "Ivan");
$salary = getIfSet("salary", 900);
$date_start_work = getIfSet("date_start_work", 77777);
$employee = new Employee($this->current_id, $name, $salary, $date_start_work);
$response = $this->validate($employee);
$this->current_id++;
return $response;
}
}
class DepartmentController
{
public function makeDepartmentWithPeople(int $employee_count): Department
{
$name = getIfSet("name", "Default");
$employees = array();
for ($i = 0; $i < $employee_count; $i++) {
$name = "Departament number " . (random_int(1, 2)*$i);
$salary = ($i + 1) * random_int(200, 1900);
$date_started_working = time();
$employees[] = new Employee($i, $name, $salary, $date_started_working);
}
return new Department($name, ...$employees);
}
private function caseSameTotalSalaryDepartment(Department ...$departments): string
{
$max_employee_count = $departments[0]->getEmployeesCount();
$max_departments = [$departments[0]];
//find max
for ($i = 1; $i < count($departments); $i++) {
$department = $departments[$i];
$employee_count = $department->getEmployeesCount();
if ($employee_count > $max_employee_count) {
$max_employee_count = $employee_count;
$max_departments = [$department];
} else if ($employee_count == $max_employee_count) {
$max_departments[] = $department;
}
}
//make response
$response = "";
foreach ($max_departments as $department) {
$response = $response . $department . " <br>\n";
}
return $response;
}
function findMinMaxDepartmentInArray(Department ...$departments): string
{
if (count($departments) < 1) {
return "<html <body>No departments</body></html>";
}
$min_total_salary = $departments[0]->getTotalSalary();
$min_departments = [$departments[0]];
$max_total_salary = $departments[0]->getTotalSalary();
$max_departments = [$departments[0]];
for ($i = 1; $i < count($departments); $i++) {
$department = $departments[$i];
$salary = $department->getTotalSalary();
if ($salary < $min_total_salary) {
$min_total_salary = $salary;
$min_departments = [$department];
} else if ($salary == $min_total_salary) {
$min_departments[] = $department;
}
if ($salary > $max_total_salary) {
$max_total_salary = $salary;
$max_departments = [$department];
} else
if ($salary == $max_total_salary) {
$max_departments[] = $department;
}
}
$responce = "\n<h3>Minimum</h1><br>\n";
$count_min_departments = count($min_departments);
if ($count_min_departments == 1) {
$responce = $responce . "Total salary (minimum): " . $min_departments[0] . "<br>\n ";
} else if ($count_min_departments > 1) {
$responce = $responce . $this->caseSameTotalSalaryDepartment(...$min_departments);
}
$responce = $responce . "\n\n<h3>Maximum</h1><br>\n";
$count_max_departments = count($max_departments);
if (count($max_departments) == 1) {
$responce = $responce . "Total salary (maximum): " . $max_departments[0] . " <br>\n ";
} else if ($count_max_departments > 1) {
$responce = $responce . $this->caseSameTotalSalaryDepartment(...$max_departments);
}
return "<html><body>" . $responce . "\n\n</body></html>";
}
}