PostalCodeNl   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
c 1
b 0
f 0
dl 0
loc 40
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 3 1
A parseLiteral() 0 7 2
A parseValue() 0 7 2
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