Passed
Pull Request — master (#54)
by Frank
05:26
created

PayloadDefinition::field()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
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 1
nop 3
dl 0
loc 6
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 EventSauce\EventSourcing\CodeGeneration;
6
7
final class PayloadDefinition
8
{
9
    /**
10
     * @var DefinitionGroup
11
     */
12
    private $group;
13
14
    /**
15
     * @var string
16
     */
17
    private $name;
18
19
    /**
20
     * @var array[]
21
     */
22
    private $fields = [];
23
24
    /**
25
     * @var string
26
     */
27
    private $fieldsFrom = '';
28
29
    /**
30
     * @var array
31
     */
32
    private $fieldSerializers = [];
33
34
    /**
35
     * @var array
36
     */
37
    private $fieldDeserializers = [];
38
39 4
    public function __construct(DefinitionGroup $group, string $name)
40
    {
41 4
        $this->name = $name;
42 4
        $this->group = $group;
43 4
    }
44
45
    /**
46
     * @param string $otherType
47
     *
48
     * @return $this
49
     */
50 4
    public function withFieldsFrom(string $otherType)
51
    {
52 4
        $this->fieldsFrom = $otherType;
53
54 4
        return $this;
55
    }
56
57 12
    public function name(): string
58
    {
59 12
        return $this->name;
60
    }
61
62 11
    public function fields(): array
63
    {
64 11
        return $this->fields;
65
    }
66
67 12
    public function fieldsFrom(): string
68
    {
69 12
        return $this->fieldsFrom;
70
    }
71
72 3
    public function field(string $name, string $type, string $example = null)
73
    {
74 3
        $example = $example ?: $this->group->exampleForField($name);
75 3
        $this->fields[] = compact('name', 'type', 'example');
76
77 3
        return $this;
78
    }
79
80 1
    public function fieldSerializer($field, $template)
81
    {
82 1
        $this->fieldSerializers[$field] = $template;
83
84 1
        return $this;
85
    }
86
87 10
    public function serializerForField($field)
88
    {
89 10
        return $this->fieldSerializers[$field] ?? $this->group->serializerForField($field);
90
    }
91
92 1
    public function fieldDeserializer($field, $template)
93
    {
94 1
        $this->fieldDeserializers[$field] = $template;
95
96 1
        return $this;
97
    }
98
99 10
    public function deserializerForField($fieldName)
100
    {
101 10
        return $this->fieldDeserializers[$fieldName] ?? $this->group->deserializerForField($fieldName);
102
    }
103
104 8
    public function deserializerForType($type)
105
    {
106 8
        return $this->group->deserializerForType($type);
107
    }
108
109 8
    public function serializerForType($type)
110
    {
111 8
        return $this->group->serializerForType($type);
112
    }
113
}
114