1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_mapper\Records; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_mapper\Interfaces\ICanFill; |
7
|
|
|
use kalanis\kw_mapper\Interfaces\IEntryType; |
8
|
|
|
use kalanis\kw_mapper\MapperException; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class AStrictRecord |
13
|
|
|
* @package kalanis\kw_mapper\Records |
14
|
|
|
* Class to map entries to their respective values - strict typing |
15
|
|
|
* The level of "obstruction" to accessing properties is necessary |
16
|
|
|
* or it could not be possible to guarantee content values. |
17
|
|
|
*/ |
18
|
|
|
abstract class AStrictRecord extends ARecord |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param mixed $offset |
22
|
|
|
* @param mixed $value |
23
|
|
|
* @throws MapperException |
24
|
|
|
*/ |
25
|
10 |
|
final public function offsetSet($offset, $value): void |
26
|
|
|
{ |
27
|
10 |
|
$this->offsetCheck($offset); |
28
|
9 |
|
$data = & $this->entries[$offset]; |
29
|
9 |
|
switch ($data->getType()) { |
30
|
|
|
case IEntryType::TYPE_BOOLEAN: |
31
|
2 |
|
$this->checkBool($value, $offset); |
32
|
2 |
|
break; |
33
|
|
|
case IEntryType::TYPE_INTEGER: |
34
|
|
|
case IEntryType::TYPE_FLOAT: |
35
|
3 |
|
$this->checkNumeric($value, $offset); |
36
|
3 |
|
$this->checkSize($value, floatval($data->getParams())); |
37
|
3 |
|
break; |
38
|
|
|
case IEntryType::TYPE_STRING: |
39
|
3 |
|
$this->checkString($value, $offset); |
40
|
3 |
|
$this->checkLength($value, intval($data->getParams())); |
41
|
3 |
|
break; |
42
|
|
|
case IEntryType::TYPE_SET: |
43
|
1 |
|
$this->checkPreset($value, (array) $data->getParams()); |
44
|
1 |
|
break; |
45
|
|
|
case IEntryType::TYPE_ARRAY: |
46
|
2 |
|
$this->checkArrayForNotEntries($value, $offset); |
47
|
1 |
|
break; |
48
|
|
|
case IEntryType::TYPE_OBJECT: |
49
|
1 |
|
$this->reloadClass($data); |
50
|
|
|
/** @var ICanFill $class */ |
51
|
1 |
|
$class = $data->getData(); |
52
|
1 |
|
$class->fillData($value); |
53
|
1 |
|
return; // fill data elsewhere |
54
|
|
|
default: |
55
|
|
|
// @codeCoverageIgnoreStart |
56
|
|
|
// happens only when someone is evil enough and change type directly on entry |
57
|
|
|
throw new MapperException(sprintf('Unknown type *%d*', $data->getType())); |
58
|
|
|
// @codeCoverageIgnoreEnd |
59
|
|
|
} |
60
|
8 |
|
$data->setData($value); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param mixed $value |
65
|
|
|
* @param string $key |
66
|
|
|
* @throws MapperException |
67
|
|
|
*/ |
68
|
2 |
|
private function checkBool($value, string $key): void |
69
|
|
|
{ |
70
|
2 |
|
if (is_null($value)) { |
71
|
1 |
|
return; |
72
|
|
|
} |
73
|
2 |
|
if (!is_bool($value)) { |
74
|
1 |
|
throw new MapperException(sprintf('Try to set something other than number into key *%s*', $key)); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param mixed $value |
80
|
|
|
* @param string $key |
81
|
|
|
* @throws MapperException |
82
|
|
|
*/ |
83
|
3 |
|
private function checkNumeric($value, string $key): void |
84
|
|
|
{ |
85
|
3 |
|
if (is_null($value)) { |
86
|
1 |
|
return; |
87
|
|
|
} |
88
|
3 |
|
if (!is_numeric($value)) { |
89
|
1 |
|
throw new MapperException(sprintf('Try to set something other than number into key *%s*', $key)); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param mixed $value |
95
|
|
|
* @param string $key |
96
|
|
|
* @throws MapperException |
97
|
|
|
*/ |
98
|
3 |
|
private function checkString($value, string $key): void |
99
|
|
|
{ |
100
|
3 |
|
if (is_null($value)) { |
101
|
1 |
|
return; |
102
|
|
|
} |
103
|
3 |
|
if (!is_string($value)) { |
104
|
1 |
|
throw new MapperException(sprintf('Try to set something other than string into key *%s*', $key)); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param mixed $value |
110
|
|
|
* @param float $limit |
111
|
|
|
* @throws MapperException |
112
|
|
|
*/ |
113
|
3 |
|
private function checkSize($value, float $limit): void |
114
|
|
|
{ |
115
|
3 |
|
if (is_null($value)) { |
116
|
1 |
|
return; |
117
|
|
|
} |
118
|
2 |
|
if ($value > $limit) { |
119
|
1 |
|
throw new MapperException(sprintf('Try to set number larger than allowed size (*%.4f* > *%.4f*)', $value, $limit)); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param mixed $value |
125
|
|
|
* @param int $limit |
126
|
|
|
* @throws MapperException |
127
|
|
|
*/ |
128
|
3 |
|
private function checkLength($value, int $limit): void |
129
|
|
|
{ |
130
|
3 |
|
if (is_null($value)) { |
131
|
1 |
|
return; |
132
|
|
|
} |
133
|
2 |
|
$size = mb_strlen($value); |
134
|
2 |
|
if ($size > $limit) { |
135
|
1 |
|
throw new MapperException(sprintf('Try to set string longer than allowed size (*%d* > *%d*)', $size, $limit)); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param mixed $value |
141
|
|
|
* @param array<string|int|float> $preset |
142
|
|
|
* @throws MapperException |
143
|
|
|
*/ |
144
|
1 |
|
private function checkPreset($value, $preset): void |
145
|
|
|
{ |
146
|
1 |
|
if (is_null($value)) { |
147
|
1 |
|
return; |
148
|
|
|
} |
149
|
1 |
|
if (!in_array($value, $preset)) { |
150
|
1 |
|
throw new MapperException(sprintf('Try to set *%s* that is not in preset values', $value)); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param mixed $value |
156
|
|
|
* @param string $key |
157
|
|
|
* @throws MapperException |
158
|
|
|
*/ |
159
|
2 |
|
private function checkArrayForNotEntries($value, string $key): void |
160
|
|
|
{ |
161
|
2 |
|
if (!is_array($value)) { |
162
|
1 |
|
throw new MapperException(sprintf('You must set array into key *%s*', $key)); |
163
|
|
|
} |
164
|
1 |
|
foreach ($value as $item) { |
165
|
1 |
|
if (!$item instanceof ARecord) { |
166
|
1 |
|
throw new MapperException(sprintf('Array in key *%s* contains something that is not link to another mapper', $key)); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|