1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Validation; |
4
|
|
|
|
5
|
|
|
use App\Exception\TaskException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Task Validation. |
9
|
|
|
*/ |
10
|
|
|
abstract class TaskValidation extends BaseValidation |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Validate and sanitize input data when create new task. |
14
|
|
|
* |
15
|
|
|
* @param array|object|null $input |
16
|
|
|
* @return array |
17
|
|
|
* @throws \Exception |
18
|
|
|
*/ |
19
|
|
|
public static function validateInputOnCreateTask($input) |
20
|
|
|
{ |
21
|
|
|
if (empty($input['name'])) { |
22
|
|
|
throw new TaskException(TaskException::TASK_NAME_REQUIRED, 400); |
23
|
|
|
} |
24
|
|
|
$task = self::validateTaskName($input['name']); |
25
|
|
|
$status = 0; |
26
|
|
|
if (isset($input['status'])) { |
27
|
|
|
$status = self::validateStatus($input['status']); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return ['name' => $task, 'status' => $status]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Validate and sanitize input data when update a task. |
35
|
|
|
* |
36
|
|
|
* @param array|object|null $input |
37
|
|
|
* @param object $task |
38
|
|
|
* @return array |
39
|
|
|
* @throws \Exception |
40
|
|
|
*/ |
41
|
|
|
public static function validateInputOnUpdateTask($input, $task) |
42
|
|
|
{ |
43
|
|
View Code Duplication |
if (!isset($input['name']) && !isset($input['status'])) { |
|
|
|
|
44
|
|
|
throw new TaskException(TaskException::TASK_INFO_REQUIRED, 400); |
45
|
|
|
} |
46
|
|
|
$name = $task->name; |
47
|
|
|
if (isset($input['name'])) { |
48
|
|
|
$name = self::validateTaskName($input['name']); |
49
|
|
|
} |
50
|
|
|
$status = $task->status; |
51
|
|
|
if (isset($input['status'])) { |
52
|
|
|
$status = self::validateStatus($input['status']); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return ['name' => $name, 'status' => $status]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param array $input |
60
|
|
|
* @param object $task |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public static function validateNameOnUpdateTask($input, $task) |
64
|
|
|
{ |
65
|
|
|
$name = $task->name; |
66
|
|
|
if (isset($input['name'])) { |
67
|
|
|
$name = self::validateTaskName($input['name']); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $name; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array $input |
75
|
|
|
* @param object $task |
76
|
|
|
* @return int |
77
|
|
|
*/ |
78
|
|
|
public static function validateStatusOnUpdateTask($input, $task) |
79
|
|
|
{ |
80
|
|
|
$status = $task->status; |
81
|
|
|
if (isset($input['status'])) { |
82
|
|
|
$status = self::validateStatus($input['status']); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $status; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.