Completed
Push — master ( 260563...208a75 )
by Mauro
03:00
created

UserValidation::validateNameOnUpdateUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace App\Validation;
4
5
use App\Exception\UserException;
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|object|null $input
16
     * @return array
17
     * @throws \Exception
18
     */
19
    public static function validateInputOnCreateUser($input)
20
    {
21
        if (!isset($input['name'])) {
22
            throw new UserException(UserException::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
    public static function validateNameOnUpdateUser($input, $user)
34
    {
35
        $name = $user->name;
36
        if (isset($input['name'])) {
37
            $name = self::validateName($input['name']);
38
        }
39
40
        return $name;
41
    }
42
43
    public static function validateEmailOnUpdateUser($input, $user)
44
    {
45
        $email = $user->email;
46
        if (isset($input['email'])) {
47
            $email = self::validateEmail($input['email']);
48
        }
49
50
        return $email;
51
    }
52
}
53