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

BaseValidation::validateEmail()   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 1
1
<?php
2
3
namespace App\Validation;
4
5
use App\Message\TaskMessage;
6
use App\Message\UserMessage;
7
use Respect\Validation\Validator as v;
8
9
/**
10
 * Base Validation.
11
 */
12
abstract class BaseValidation
13
{
14
    /**
15
     * Validate and sanitize a username.
16
     *
17
     * @param string $name
18
     * @return string
19
     * @throws \Exception
20
     */
21 View Code Duplication
    protected static function validateName($name)
22
    {
23
        if (!v::alnum()->length(2, 100)->validate($name)) {
24
            throw new \Exception(UserMessage::USER_NAME_INVALID, 400);
25
        }
26
27
        return $name;
28
    }
29
30
    /**
31
     * Validate and sanitize a email address.
32
     *
33
     * @param string $emailValue
34
     * @return string
35
     * @throws \Exception
36
     */
37
    protected static function validateEmail($emailValue)
38
    {
39
        $email = filter_var($emailValue, FILTER_SANITIZE_EMAIL);
40
        if (!v::email()->validate($email)) {
41
            throw new \Exception(UserMessage::USER_EMAIL_INVALID, 400);
42
        }
43
44
        return $email;
45
    }
46
47
    /**
48
     * Validate and sanitize a task name.
49
     *
50
     * @param string $name
51
     * @return string
52
     * @throws \Exception
53
     */
54 View Code Duplication
    protected static function validateTaskName($name)
55
    {
56
        if (!v::alnum()->length(2, 100)->validate($name)) {
57
            throw new \Exception(TaskMessage::TASK_NAME_INVALID, 400);
58
        }
59
60
        return $name;
61
    }
62
63
    /**
64
     * Validate and sanitize a task status.
65
     *
66
     * @param int $status
67
     * @return string
68
     * @throws \Exception
69
     */
70
    protected static function validateStatus($status)
71
    {
72
        if (!v::numeric()->between(0, 1)->validate($status)) {
73
            throw new \Exception(TaskMessage::TASK_STATUS_INVALID, 400);
74
        }
75
76
        return $status;
77
    }
78
}
79