Completed
Push — master ( e6319e...8dcc47 )
by Adrien
07:50
created

EmailType::serialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Scalar;
6
7
use GraphQL\Language\AST\StringValueNode;
8
9
/**
10
 * Represent an email address
11
 *
12
 * This exceptionally accept empty string as null because email address are often unique
13
 * in DB and thus can never be empty string to indicate absence of email. So we simplify
14
 * the client work by accepting empty string and transparently transforming into a null value.
15
 */
16
class EmailType extends AbstractStringBasedType
17
{
18
    /**
19
     * Validate a email
20
     *
21
     * @param mixed $value
22
     *
23
     * @return bool
24
     */
25 10
    protected function isValid($value): bool
26
    {
27 10
        return $value === null || (is_string($value) && filter_var($value, FILTER_VALIDATE_EMAIL));
28
    }
29
30 6
    public function serialize($value)
31
    {
32 6
        if ($value === '') {
33 1
            return null;
34
        }
35
36 5
        return parent::serialize($value);
37
    }
38
39 7
    public function parseValue($value)
40
    {
41 7
        if ($value === '') {
42 1
            return null;
43
        }
44
45 6
        return parent::parseValue($value);
46
    }
47
48 5
    public function parseLiteral($ast, array $variables = null)
49
    {
50 5
        if ($ast instanceof StringValueNode && $ast->value === '') {
51 1
            return null;
52
        }
53
54 4
        return parent::parseLiteral($ast, $variables);
55
    }
56
}
57