1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_mapper\Records; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_mapper\MapperException; |
7
|
|
|
use kalanis\kw_mapper\Mappers; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* trait TMapper |
12
|
|
|
* @package kalanis\kw_mapper\Records |
13
|
|
|
* Class to map entries to their respective values |
14
|
|
|
* The level of "obstruction" to accessing properties is necessary |
15
|
|
|
* or it could not be possible to guarantee content values. |
16
|
|
|
* The children must stay too simple to avoid some usual problems which came with multilevel extending |
17
|
|
|
*/ |
18
|
|
|
trait TMapper |
19
|
|
|
{ |
20
|
|
|
/** @var Mappers\AMapper|null */ |
21
|
|
|
protected $mapper = null; |
22
|
|
|
/** @var Mappers\Factory|null */ |
23
|
|
|
private $mapperFactory = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $name |
27
|
|
|
* @throws MapperException |
28
|
|
|
*/ |
29
|
129 |
|
final protected function setMapper(string $name): void |
30
|
|
|
{ |
31
|
129 |
|
$this->mapper = $this->mapperFromFactory($name); |
32
|
129 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $name |
36
|
|
|
* @throws MapperException |
37
|
|
|
* @return Mappers\AMapper |
38
|
|
|
*/ |
39
|
129 |
|
protected function mapperFromFactory(string $name): Mappers\AMapper |
40
|
|
|
{ |
41
|
|
|
// factory returns class as static instance, so it is not necessary to fill more memory with necessities |
42
|
129 |
|
if (empty($this->mapperFactory)) { |
43
|
129 |
|
$this->mapperFactory = $this->mapperFactory(); |
44
|
|
|
} |
45
|
129 |
|
return $this->mapperFactory->getInstance($name); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* You can set own factory to load other mappers |
50
|
|
|
* @return Mappers\Factory |
51
|
|
|
*/ |
52
|
129 |
|
protected function mapperFactory(): Mappers\Factory |
53
|
|
|
{ |
54
|
129 |
|
return new Mappers\Factory(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param bool $forceInsert |
59
|
|
|
* @throws MapperException |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
15 |
|
final public function save(bool $forceInsert = false): bool |
63
|
|
|
{ |
64
|
15 |
|
return $this->getMapper()->/** @scrutinizer ignore-call */save($this->getSelf(), $forceInsert); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @throws MapperException |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
16 |
|
final public function load(): bool |
72
|
|
|
{ |
73
|
16 |
|
return $this->getMapper()->/** @scrutinizer ignore-call */load($this->getSelf()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @throws MapperException |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
8 |
|
final public function delete(): bool |
81
|
|
|
{ |
82
|
8 |
|
return $this->getMapper()->delete($this->getSelf()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @throws MapperException |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
5 |
|
final public function count(): int |
90
|
|
|
{ |
91
|
5 |
|
return $this->getMapper()->countRecord($this->getSelf()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @throws MapperException |
96
|
|
|
* @return ARecord[] |
97
|
|
|
*/ |
98
|
21 |
|
final public function loadMultiple(): array |
99
|
|
|
{ |
100
|
21 |
|
return $this->getMapper()->loadMultiple($this->getSelf()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @throws MapperException |
105
|
|
|
* @return Mappers\AMapper |
106
|
|
|
*/ |
107
|
108 |
|
public function getMapper(): Mappers\AMapper |
108
|
|
|
{ |
109
|
108 |
|
if (empty($this->mapper)) { |
110
|
1 |
|
throw new MapperException('Unknown entry mapper'); |
111
|
|
|
} |
112
|
107 |
|
return $this->mapper; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
abstract protected function getSelf(): ARecord; |
116
|
|
|
} |
117
|
|
|
|