PostalCodeNl::serialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace DeInternetJongens\LighthouseUtils\Schema\Scalars;
4
5
use GraphQL\Error\Error;
6
use GraphQL\Language\AST\StringValueNode;
7
use GraphQL\Type\Definition\ScalarType;
8
9
class PostalCodeNl extends ScalarType
10
{
11
    private const PATTERN = '/^\d{4}[a-zA-Z]{2}$/';
12
13
    /** @var string */
14
    public $name = 'PostalCodeNl';
15
16
    /** @var string */
17
    public $description = 'A valid postalcode for The Netherlands with pattern [1234aa]. Example: 1234AA.';
18
19
    /**
20
     * @inheritDoc
21
     */
22
    public function serialize($value)
23
    {
24
        return $value;
25
    }
26
27
    /**
28
     * @inheritDoc
29
     */
30
    public function parseValue($value)
31
    {
32
        if (! \preg_match(self::PATTERN, $value)) {
33
            throw new Error(sprintf('Input error: Expected valid postal code with pattern [1234aa], got: [%s]', $value));
34
        }
35
36
        return $value;
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42
    public function parseLiteral($valueNode, array $variables = null)
43
    {
44
        if (! $valueNode instanceof StringValueNode) {
45
            throw new Error('Query error: Can only parse strings got: ' . $valueNode->kind, [$valueNode]);
46
        }
47
48
        return $this->parseValue($valueNode->value);
49
    }
50
}
51