|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_pedigree; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_mapper\MapperException; |
|
7
|
|
|
use kalanis\kw_mapper\Search\Search; |
|
8
|
|
|
use kalanis\kw_pedigree\Storage\APedigreeRecord; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class GetEntries |
|
13
|
|
|
* @package kalanis\kw_pedigree |
|
14
|
|
|
* Getting entries from DB |
|
15
|
|
|
*/ |
|
16
|
|
|
class GetEntries |
|
17
|
|
|
{ |
|
18
|
|
|
protected APedigreeRecord $record; |
|
19
|
|
|
protected Storage\AEntryAdapter $storage; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param APedigreeRecord $record |
|
23
|
|
|
* @throws PedigreeException |
|
24
|
|
|
*/ |
|
25
|
8 |
|
public function __construct(APedigreeRecord $record) |
|
26
|
|
|
{ |
|
27
|
8 |
|
$this->record = $record; |
|
28
|
8 |
|
$this->storage = Storage\FactoryAdapter::getAdapter($record); |
|
29
|
8 |
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
public function getRecord(): APedigreeRecord |
|
32
|
|
|
{ |
|
33
|
1 |
|
return $this->record; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
public function getStorage(): Storage\AEntryAdapter |
|
37
|
|
|
{ |
|
38
|
1 |
|
return $this->storage; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param int $id |
|
43
|
|
|
* @throws MapperException |
|
44
|
|
|
* @return Storage\AEntryAdapter|null |
|
45
|
|
|
*/ |
|
46
|
2 |
|
public function getById(int $id): ?Storage\AEntryAdapter |
|
47
|
|
|
{ |
|
48
|
2 |
|
return $this->fillStorage($this->getBy($this->storage->getIdKey(), strval($id))); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $key |
|
53
|
|
|
* @throws MapperException |
|
54
|
|
|
* @return Storage\AEntryAdapter|null |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function getByKey(string $key): ?Storage\AEntryAdapter |
|
57
|
|
|
{ |
|
58
|
2 |
|
return $this->fillStorage($this->getBy($this->storage->getShortKey(), $key)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $key |
|
63
|
|
|
* @param string $value |
|
64
|
|
|
* @throws MapperException |
|
65
|
|
|
* @return APedigreeRecord|null |
|
66
|
|
|
*/ |
|
67
|
4 |
|
protected function getBy(string $key, string $value): ?APedigreeRecord |
|
68
|
|
|
{ |
|
69
|
4 |
|
$search = new Search(clone $this->record); |
|
70
|
4 |
|
$search->exact($key, $value); |
|
71
|
4 |
|
$results = $search->getResults(); |
|
72
|
4 |
|
return empty($results) ? null : reset($results); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $sex |
|
77
|
|
|
* @param string|null $name which name |
|
78
|
|
|
* @param string|null $family from which family will be get |
|
79
|
|
|
* @param string|null $emptyString If you want to add empty record, you need to set an empty string; usually for empty choice |
|
80
|
|
|
* @throws MapperException |
|
81
|
|
|
* @return Storage\AEntryAdapter[] |
|
82
|
|
|
*/ |
|
83
|
3 |
|
public function getBySex(string $sex, ?string $name = null, ?string $family = null, ?string $emptyString = null): array |
|
84
|
|
|
{ |
|
85
|
3 |
|
$search = new Search(clone $this->record); |
|
86
|
3 |
|
if (!is_null($name)) { |
|
87
|
2 |
|
$search->like($this->storage->getNameKey(), $name); |
|
88
|
|
|
} |
|
89
|
3 |
|
if (!is_null($family)) { |
|
90
|
2 |
|
$search->like($this->storage->getFamilyKey(), $family); |
|
91
|
|
|
} |
|
92
|
3 |
|
$search->exact($this->storage->getSexKey(), $sex); |
|
93
|
3 |
|
if (is_null($emptyString)) { |
|
94
|
2 |
|
return array_filter(array_map([$this, 'fillStorage'], $search->getResults())); |
|
95
|
|
|
} |
|
96
|
1 |
|
$emptyRecord = clone $this->record; |
|
97
|
1 |
|
$emptyRecord->offsetSet($this->storage->getIdKey(), ''); |
|
98
|
1 |
|
$emptyRecord->offsetSet($this->storage->getShortKey(), ''); |
|
99
|
1 |
|
$emptyRecord->offsetSet($this->storage->getNameKey(), $emptyString); |
|
100
|
1 |
|
$emptyRecord->offsetSet($this->storage->getFamilyKey(), ''); |
|
101
|
1 |
|
$emptyRecord->offsetSet($this->storage->getBirthKey(), ''); |
|
102
|
1 |
|
$emptyRecord->offsetSet($this->storage->getDeathKey(), ''); |
|
103
|
1 |
|
$emptyRecord->offsetSet($this->storage->getSuccessesKey(), ''); |
|
104
|
1 |
|
$emptyRecord->offsetSet($this->storage->getSexKey(), 'none'); |
|
105
|
1 |
|
$emptyRecord->offsetSet($this->storage->getTextKey(), ''); |
|
106
|
1 |
|
return array_filter(array_map([$this, 'fillStorage'], array_merge([$emptyRecord], $search->getResults()))); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
6 |
|
public function fillStorage(?APedigreeRecord $record): ?Storage\AEntryAdapter |
|
110
|
|
|
{ |
|
111
|
6 |
|
if (empty($record)) { |
|
112
|
2 |
|
return null; |
|
113
|
|
|
} |
|
114
|
4 |
|
$storage = clone $this->storage; |
|
115
|
4 |
|
$storage->setRecord($record); |
|
116
|
4 |
|
return $storage; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|