Issues (34)

src/Entity/Field.php (1 issue)

1
<?php
2
3
/*
4
 * This file is part of the SexyField package.
5
 *
6
 * (c) Dion Snoeijen <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare (strict_types = 1);
13
14
namespace Tardigrades\Entity;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
use Tardigrades\SectionField\ValueObject\Created;
19
use Tardigrades\SectionField\ValueObject\FieldConfig;
20
use Tardigrades\SectionField\ValueObject\Handle;
21
use Tardigrades\SectionField\ValueObject\Id;
22
use Tardigrades\SectionField\ValueObject\Name;
23
use Tardigrades\SectionField\ValueObject\Updated;
24
25
class Field implements FieldInterface
26
{
27
    /** @var int */
28
    protected $id;
29
30
    /** @var string */
31
    protected $name;
32
33
    /** @var string */
34
    protected $handle;
35
36
    /** @var ArrayCollection */
37
    protected $sections;
38
39
    /** @var FieldType */
40
    protected $fieldType;
41
42
    /** @var array */
43
    protected $config;
44
45
    /** @var \DateTime */
46
    protected $created;
47
48
    /** @var \DateTime */
49
    protected $updated;
50
51
    public function __construct(
52
        Collection $sections = null
53
    ) {
54
        $this->sections = is_null($sections) ? new ArrayCollection() : $sections;
55
    }
56
57
    public function getId(): ?int
58
    {
59
        return $this->id;
60
    }
61
62
    public function setId(int $id): FieldInterface
63
    {
64
        $this->id = $id;
65
66
        return $this;
67
    }
68
69
    public function getName(): Name
70
    {
71
        return Name::fromString($this->name);
72
    }
73
74
    public function setName(string $name): FieldInterface
75
    {
76
        $this->name = $name;
77
78
        return $this;
79
    }
80
81
    public function setHandle(string $handle): FieldInterface
82
    {
83
        $this->handle = $handle;
84
85
        return $this;
86
    }
87
88
    public function getHandle(): Handle
89
    {
90
        return Handle::fromString($this->handle);
91
    }
92
93
    public function getIdValueObject(): Id
94
    {
95
        return Id::fromInt($this->id);
96
    }
97
98
    public function addSection(SectionInterface $section): FieldInterface
99
    {
100
        if ($this->sections->contains($section)) {
101
            return $this;
102
        }
103
        $this->sections->add($section);
104
        $section->addField($this);
105
106
        return $this;
107
    }
108
109
    public function removeSection(SectionInterface $section): FieldInterface
110
    {
111
        if (!$this->sections->contains($section)) {
112
            return $this;
113
        }
114
        $this->sections->removeElement($section);
115
        $section->removeField($this);
116
117
        return $this;
118
    }
119
120
    public function getSections(): Collection
121
    {
122
        return $this->sections;
123
    }
124
125
    public function setFieldType(FieldTypeInterface $fieldType): FieldInterface
126
    {
127
        $this->fieldType = $fieldType;
0 ignored issues
show
Documentation Bug introduced by
$fieldType is of type Tardigrades\Entity\FieldTypeInterface, but the property $fieldType was declared to be of type Tardigrades\Entity\FieldType. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
128
129
        return $this;
130
    }
131
132
    public function removeFieldType(FieldTypeInterface $fieldType): FieldInterface
133
    {
134
        $this->fieldType = null;
135
136
        return $this;
137
    }
138
139
    public function getFieldType(): FieldTypeInterface
140
    {
141
        return $this->fieldType;
142
    }
143
144
    public function setConfig(array $config): FieldInterface
145
    {
146
        $this->config = $config;
147
148
        return $this;
149
    }
150
151
    public function getConfig(): FieldConfig
152
    {
153
        return FieldConfig::fromArray((array) $this->config);
154
    }
155
156
    public function setCreated(\DateTime $created): FieldInterface
157
    {
158
        $this->created = $created;
159
160
        return $this;
161
    }
162
163
    public function getCreated(): \DateTime
164
    {
165
        return $this->created;
166
    }
167
168
    public function getCreatedValueObject(): Created
169
    {
170
        return Created::fromDateTime($this->created);
171
    }
172
173
    public function setUpdated(\DateTime $updated): FieldInterface
174
    {
175
        $this->updated = $updated;
176
177
        return $this;
178
    }
179
180
    public function getUpdated(): \DateTime
181
    {
182
        return $this->updated;
183
    }
184
185
    public function getUpdatedValueObject(): Updated
186
    {
187
        return Updated::fromDateTime($this->updated);
188
    }
189
190
    public function onPrePersist(): void
191
    {
192
        $this->created = new \DateTime("now");
193
        $this->updated = new \DateTime("now");
194
    }
195
196
    public function onPreUpdate(): void
197
    {
198
        $this->updated = new \DateTime("now");
199
    }
200
}
201