1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author KonstantinKuklin <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace KonstantinKuklin\DoctrineCompressedFields; |
7
|
|
|
|
8
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
9
|
|
|
|
10
|
|
|
class Engine |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param int $hub |
14
|
|
|
* @param \KonstantinKuklin\DoctrineCompressedFields\Annotation\Mask[] $compressedList |
15
|
|
|
* @param int[] $valueList |
16
|
|
|
* |
17
|
|
|
* @return int |
18
|
|
|
* @throws \Exception |
19
|
|
|
*/ |
20
|
5 |
|
public function getPackedValue($hub, $compressedList, $valueList) |
21
|
|
|
{ |
22
|
5 |
|
if (!$hub) { |
23
|
4 |
|
$hub = 0; |
24
|
|
|
} |
25
|
5 |
|
$hubBin = decbin($hub); |
26
|
|
|
|
27
|
5 |
|
foreach ($compressedList as $valueKey => $compressed) { |
28
|
5 |
|
$compressedAnnotation = $compressed[MetadataLayer::ANNOTATION]; |
29
|
5 |
|
$bits = $compressedAnnotation->bits; |
30
|
5 |
|
$value = $valueList[$valueKey] ?: 0; |
31
|
5 |
|
if (!$value) { |
32
|
2 |
|
continue; |
33
|
|
|
} |
34
|
5 |
|
$valueBin = decbin($value); |
35
|
5 |
|
if (strlen($valueBin) > count($bits)) { |
36
|
|
|
throw new \Exception('bits are to few'); |
37
|
|
|
} |
38
|
5 |
|
foreach ($bits as $valuePosition => $hubPosition) { |
39
|
5 |
|
$hubBin[$hubPosition] = isset($valueBin[$valuePosition]) ? $valueBin[$valuePosition] : '0'; |
40
|
|
|
} |
41
|
|
|
} |
42
|
5 |
|
$hubBin = str_replace(' ', '0', $hubBin); |
43
|
5 |
|
$hubBinCorrectOrder = strrev($hubBin); |
44
|
|
|
|
45
|
5 |
|
return bindec($hubBinCorrectOrder); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $hub |
50
|
|
|
* @param array $compressedList |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
3 |
|
public function getUnpackedValueList($hub, $compressedList) |
55
|
|
|
{ |
56
|
3 |
|
if (!$hub) { |
57
|
|
|
return []; |
58
|
|
|
} |
59
|
3 |
|
$hubBin = strrev(decbin($hub)); |
60
|
|
|
|
61
|
3 |
|
$valueList = []; |
62
|
3 |
|
foreach ($compressedList as $valueKey => $compressed) { |
63
|
3 |
|
$compressedAnnotation = $compressed[MetadataLayer::ANNOTATION]; |
64
|
3 |
|
$bits = $compressedAnnotation->bits; |
65
|
3 |
|
$valueBin = '0'; |
66
|
3 |
|
foreach ($bits as $valuePosition => $hubPosition) { |
67
|
3 |
|
$valueBin[$valuePosition] = !empty($hubBin[$hubPosition]) ? $hubBin[$hubPosition] : '0'; |
68
|
|
|
} |
69
|
3 |
|
$valueBin = str_replace(' ', '0', $valueBin); |
70
|
3 |
|
$valueList[$valueKey] = bindec(strrev($valueBin)); |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
return $valueList; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadata $entityClassMetadata |
78
|
|
|
* @param string $hub |
79
|
|
|
* |
80
|
|
|
* @return \int[] |
81
|
|
|
* @throws \Exception |
82
|
|
|
*/ |
83
|
1 |
|
public function getFreeBits(ClassMetadata $entityClassMetadata, $hub) |
84
|
|
|
{ |
85
|
1 |
|
$HubAnnotation = MetadataLayer::getHubAnnotation($entityClassMetadata, $hub); |
86
|
1 |
|
$bitsCount = $HubAnnotation->size; |
87
|
|
|
|
88
|
1 |
|
$maskList = MetadataLayer::getMaskList($entityClassMetadata, $hub); |
89
|
|
|
|
90
|
1 |
|
$bitsUsed = []; |
91
|
1 |
|
foreach ($maskList as $maskGrouped) { |
92
|
1 |
|
$MaskAnnotation = $maskGrouped[MetadataLayer::ANNOTATION]; |
93
|
1 |
|
$bitsUsed = array_merge($bitsUsed, $MaskAnnotation->bits); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
$bitsUsed = array_flip($bitsUsed); |
97
|
|
|
|
98
|
1 |
|
$bitsFree = []; |
99
|
1 |
|
for ($i = 0; $i < $bitsCount; $i++) { |
100
|
1 |
|
if (isset($bitsUsed[$i])) { |
101
|
1 |
|
continue; |
102
|
|
|
} |
103
|
1 |
|
$bitsFree[] = $i; |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return $bitsFree; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|