Passed
Pull Request — 2.x (#57)
by Alexander
13:56
created

Field::setTypecast()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Definition;
6
7
use Cycle\Schema\Definition\Map\OptionMap;
8
use Cycle\Schema\Exception\FieldException;
9
10
/**
11
 * Field declaration, it's type and mapping to column.
12
 */
13
final class Field
14
{
15
    private OptionMap $options;
16
    private OptionMap $attributes;
17
    private ?string $column = null;
18
    private ?string $type = null;
19
    private bool $primary = false;
20
    private array|string|null $typecast = null;
21
    private bool $referenced = false;
22
    private ?string $entityClass = null;
23 1494
24
    public function __construct()
25 1494
    {
26 1494
        $this->options = new OptionMap();
27
        $this->attributes = new OptionMap();
28 58
    }
29
30 58
    public function __clone()
31 58
    {
32
        $this->options = clone $this->options;
33 712
        $this->attributes = clone $this->attributes;
34
    }
35 712
36
    public function getOptions(): OptionMap
37
    {
38 1172
        return $this->options;
39
    }
40 1172
41 2
    public function getAttributes(): OptionMap
42
    {
43
        return $this->attributes;
44 1170
    }
45
46
    public function getType(): string
47 1444
    {
48
        if (empty($this->column)) {
49 1444
            throw new FieldException('Field type must be set');
50
        }
51 1444
52
        return $this->type;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->type could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
53
    }
54 1254
55
    public function setType(string $type): self
56 1254
    {
57
        $this->type = $type;
58 1254
59
        return $this;
60
    }
61 1386
62
    public function setPrimary(bool $primary): self
63 1386
    {
64
        $this->primary = $primary;
65
66 1474
        return $this;
67
    }
68 1474
69
    public function isPrimary(): bool
70 1474
    {
71
        return $this->primary || in_array($this->type, ['primary', 'bigPrimary']);
72
    }
73
74
    public function setColumn(string $column): self
75
    {
76 972
        $this->column = $column;
77
78 972
        return $this;
79 2
    }
80
81
    /**
82 970
     * @throws FieldException
83
     */
84
    public function getColumn(): string
85 958
    {
86
        if (empty($this->column)) {
87 958
            throw new FieldException('Column mapping must be set');
88
        }
89 958
90
        return $this->column;
91
    }
92 788
93
    public function setTypecast(array|string|null $typecast): self
94 788
    {
95
        $this->typecast = $typecast;
96
97 958
        return $this;
98
    }
99 958
100
    public function hasTypecast(): bool
101
    {
102 952
        return $this->typecast !== null;
103
    }
104 952
105
    public function getTypecast(): array|string|null
106 952
    {
107
        return $this->typecast;
108
    }
109 796
110
    public function setReferenced(bool $indexed): self
111 796
    {
112
        $this->referenced = $indexed;
113
114 2
        return $this;
115
    }
116 2
117
    public function isReferenced(): bool
118
    {
119 952
        return $this->referenced;
120
    }
121 952
122
    public function getEntityClass(): ?string
123 952
    {
124
        return $this->entityClass;
125
    }
126
127
    public function setEntityClass(?string $entityClass): self
128
    {
129
        $this->entityClass = $entityClass;
130
131
        return $this;
132
    }
133
}
134