Completed
Push — master ( 3b79d8...b88358 )
by Mauro
02:24
created

UserValidation::validateName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace App\Validation;
4
5
use App\Message\UserMessage;
6
7
/**
8
 * User Validation.
9
 */
10
abstract class UserValidation extends BaseValidation
11
{
12
    /**
13
     * Validate and sanitize input data when create new user.
14
     *
15
     * @param array $input
16
     * @return string
17
     * @throws \Exception
18
     */
19
    public static function validateInputOnCreateUser($input)
20
    {
21
        if (!isset($input['name'])) {
22
            throw new \Exception(UserMessage::USER_NAME_REQUIRED, 400);
23
        }
24
        $name = self::validateName($input['name']);
25
        $email = null;
26
        if (isset($input['email'])) {
27
            $email = self::validateEmail($input['email']);
28
        }
29
30
        return ['name' => $name, 'email' => $email];
31
    }
32
33
    /**
34
     * Validate and sanitize input data when update a user.
35
     *
36
     * @param array $input
37
     * @param object $user
38
     * @return string
39
     * @throws \Exception
40
     */
41
    public static function validateInputOnUpdateUser($input, $user)
42
    {
43
        if (!isset($input['name']) && !isset($input['email'])) {
44
            throw new \Exception(UserMessage::USER_INFO_REQUIRED, 400);
45
        }
46
        $name = $user->name;
47
        if (isset($input['name'])) {
48
            $name = self::validateName($input['name']);
49
        }
50
        $email = $user->email;
51
        if (isset($input['email'])) {
52
            $email = self::validateEmail($input['email']);
53
        }
54
55
        return ['name' => $name, 'email' => $email];
56
    }
57
}
58