1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_pedigree\Storage; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_mapper\MapperException; |
7
|
|
|
use kalanis\kw_mapper\Records\ARecord; |
8
|
|
|
use kalanis\kw_mapper\Search\Search; |
9
|
|
|
use kalanis\kw_pedigree\Interfaces\IEntry; |
10
|
|
|
use kalanis\kw_pedigree\PedigreeException; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class AEntryAdapter |
15
|
|
|
* @package kalanis\kw_pedigree\Storage\File |
16
|
|
|
*/ |
17
|
|
|
abstract class AEntryAdapter implements IEntry |
18
|
|
|
{ |
19
|
|
|
use TBasicKeys; |
20
|
|
|
|
21
|
|
|
/** @var APedigreeRecord|null */ |
22
|
|
|
protected $record = null; |
23
|
|
|
|
24
|
17 |
|
public function setRecord(?APedigreeRecord $record): void |
25
|
|
|
{ |
26
|
17 |
|
$this->record = $record; |
27
|
17 |
|
} |
28
|
|
|
|
29
|
6 |
|
public function getRecord(): ?APedigreeRecord |
30
|
|
|
{ |
31
|
6 |
|
return $this->record; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @throws PedigreeException |
36
|
|
|
* @return APedigreeRecord |
37
|
|
|
*/ |
38
|
20 |
|
public function getLoadedRecord(): APedigreeRecord |
39
|
|
|
{ |
40
|
20 |
|
if (empty($this->record)) { |
41
|
3 |
|
throw new PedigreeException('No basic record set!'); |
42
|
|
|
} |
43
|
17 |
|
return $this->record; |
44
|
|
|
} |
45
|
|
|
|
46
|
13 |
|
public function setId(int $id): IEntry |
47
|
|
|
{ |
48
|
13 |
|
$this->getLoadedRecord()->id = $id; |
49
|
10 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
12 |
|
public function getId(): int |
53
|
|
|
{ |
54
|
12 |
|
return intval($this->getLoadedRecord()->id); |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
public function setShort(string $key): IEntry |
58
|
|
|
{ |
59
|
3 |
|
$this->getLoadedRecord()->short = $key; |
60
|
3 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
10 |
|
public function getShort(): string |
64
|
|
|
{ |
65
|
10 |
|
return strval($this->getLoadedRecord()->short); |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
public function setName(string $name): IEntry |
69
|
|
|
{ |
70
|
3 |
|
$this->getLoadedRecord()->name = $name; |
71
|
3 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
9 |
|
public function getName(): string |
75
|
|
|
{ |
76
|
9 |
|
return strval($this->getLoadedRecord()->name); |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
public function setFamily(string $family): IEntry |
80
|
|
|
{ |
81
|
3 |
|
$this->getLoadedRecord()->family = $family; |
82
|
3 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
9 |
|
public function getFamily(): string |
86
|
|
|
{ |
87
|
9 |
|
return strval($this->getLoadedRecord()->family); |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
public function setBirth(?string $birth): IEntry |
91
|
|
|
{ |
92
|
3 |
|
$this->getLoadedRecord()->birth = strval($birth); |
93
|
3 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
9 |
|
public function getBirth(): ?string |
97
|
|
|
{ |
98
|
9 |
|
$data = $this->getLoadedRecord()->birth; |
99
|
9 |
|
return empty($data) ? null : strval($data); |
100
|
|
|
} |
101
|
|
|
|
102
|
3 |
|
public function setDeath(?string $death): IEntry |
103
|
|
|
{ |
104
|
3 |
|
$this->getLoadedRecord()->death = strval($death); |
105
|
3 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
9 |
|
public function getDeath(): ?string |
109
|
|
|
{ |
110
|
9 |
|
$data = $this->getLoadedRecord()->death; |
111
|
9 |
|
return empty($data) ? null : strval($data); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @throws MapperException |
116
|
|
|
* @throws PedigreeException |
117
|
|
|
* @return ARecord[] |
118
|
|
|
*/ |
119
|
1 |
|
public function getChildren(): array |
120
|
|
|
{ |
121
|
|
|
// unhook the original class, use only definition and create new clear copy |
122
|
1 |
|
$record = get_class($this->getLoadedRecord()); |
123
|
|
|
|
124
|
1 |
|
$search = new Search(new $record()); |
125
|
1 |
|
$search->exact('motherId', strval($this->getLoadedRecord()->id)); |
126
|
1 |
|
$search->exact('fatherId', strval($this->getLoadedRecord()->id)); |
127
|
1 |
|
$search->useOr(); |
128
|
1 |
|
return $search->getResults(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param int|null $fatherId |
133
|
|
|
* @param int|null $motherId |
134
|
|
|
* @throws MapperException |
135
|
|
|
* @throws PedigreeException |
136
|
|
|
* @return bool|null |
137
|
|
|
*/ |
138
|
2 |
|
public function saveFamily(?int $fatherId, ?int $motherId): ?bool |
139
|
|
|
{ |
140
|
2 |
|
$willSave = false; |
141
|
2 |
|
if (boolval($this->setFatherId($fatherId))) { |
142
|
2 |
|
$willSave = true; |
143
|
|
|
} |
144
|
2 |
|
if (boolval($this->setMotherId($motherId))) { |
145
|
2 |
|
$willSave = true; |
146
|
|
|
} |
147
|
2 |
|
if ($willSave) { |
148
|
2 |
|
return $this->getLoadedRecord()->save(); |
149
|
|
|
} |
150
|
2 |
|
return null; |
151
|
|
|
} |
152
|
|
|
|
153
|
3 |
|
public function setSuccesses(string $successes): IEntry |
154
|
|
|
{ |
155
|
3 |
|
$this->getLoadedRecord()->successes = $successes; |
156
|
3 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
9 |
|
public function getSuccesses(): string |
160
|
|
|
{ |
161
|
9 |
|
return strval($this->getLoadedRecord()->successes); |
162
|
|
|
} |
163
|
|
|
|
164
|
3 |
|
public function setSex(string $sex): IEntry |
165
|
|
|
{ |
166
|
3 |
|
$this->getLoadedRecord()->sex = $sex; |
167
|
3 |
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
10 |
|
public function getSex(): string |
171
|
|
|
{ |
172
|
10 |
|
return strval($this->getLoadedRecord()->sex); |
173
|
|
|
} |
174
|
|
|
|
175
|
3 |
|
public function setText(string $text): IEntry |
176
|
|
|
{ |
177
|
3 |
|
$this->getLoadedRecord()->text = $text; |
178
|
3 |
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
9 |
|
public function getText(): string |
182
|
|
|
{ |
183
|
9 |
|
return strval($this->getLoadedRecord()->text); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param string $what |
188
|
|
|
* @param string|null $sex |
189
|
|
|
* @throws MapperException |
190
|
|
|
* @throws PedigreeException |
191
|
|
|
* @return ARecord[] |
192
|
|
|
*/ |
193
|
3 |
|
public function getLike(string $what, ?string $sex): array |
194
|
|
|
{ |
195
|
3 |
|
return $this->getLoadedRecord()->getLike($what, $sex); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|