|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@see Mistralys\WidthsCalculator\Calculator\SurplusRemover} class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package WidthsCalculator |
|
6
|
|
|
* @see Mistralys\WidthsCalculator\Calculator\SurplusRemover |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare (strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Mistralys\WidthsCalculator\Calculator; |
|
12
|
|
|
|
|
13
|
|
|
use Mistralys\WidthsCalculator\Calculator; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Handles subtracting values above the maximum total. |
|
17
|
|
|
* This can happen when there are missing values combined |
|
18
|
|
|
* with a minimum column width. |
|
19
|
|
|
* |
|
20
|
|
|
* @package WidthsCalculator |
|
21
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class SurplusRemover |
|
24
|
|
|
{ |
|
25
|
|
|
private Calculator $calculator; |
|
26
|
|
|
private Operations $operations; |
|
27
|
|
|
private float $leftover = 0; |
|
28
|
|
|
private float $baseTotal = 0; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(Calculator $calculator) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->calculator = $calculator; |
|
33
|
|
|
$this->operations = $calculator->getOperations(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function remove() : void |
|
37
|
|
|
{ |
|
38
|
|
|
$this->leftover = $this->calculator->getMaxTotal() - $this->operations->calcTotal(); |
|
39
|
|
|
|
|
40
|
|
|
if($this->leftover >= 0) |
|
41
|
|
|
{ |
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$this->leftover *= -1; // we want a positive number |
|
46
|
|
|
$this->baseTotal = $this->operations->calcTotalNotMissing(); |
|
47
|
|
|
$cols = $this->calculator->getColumns(); |
|
48
|
|
|
|
|
49
|
|
|
foreach($cols as $col) |
|
50
|
|
|
{ |
|
51
|
|
|
if(!$this->processColumn($col)) |
|
52
|
|
|
{ |
|
53
|
|
|
break; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// There is some surplus left after the operation: |
|
58
|
|
|
// this means there were columns from which the |
|
59
|
|
|
// surplus could not be removed because of the min |
|
60
|
|
|
// column width. |
|
61
|
|
|
// |
|
62
|
|
|
// We simply run the removal again, to remove the |
|
63
|
|
|
// surplus from the columns it can be removed from. |
|
64
|
|
|
if($this->leftover > 0) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->remove(); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function processColumn(Column $col) : bool |
|
71
|
|
|
{ |
|
72
|
|
|
if($col->isMissing()) |
|
73
|
|
|
{ |
|
74
|
|
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if($this->leftover <= 0) |
|
78
|
|
|
{ |
|
79
|
|
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$percent = $col->getValue() * 100 / $this->baseTotal; |
|
83
|
|
|
$amount = round($this->leftover * $percent / 100); |
|
84
|
|
|
$val = $col->getValue() - $amount; |
|
85
|
|
|
|
|
86
|
|
|
if($val < $this->calculator->getMinWidth()) |
|
87
|
|
|
{ |
|
88
|
|
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$this->leftover -= $amount; |
|
92
|
|
|
|
|
93
|
|
|
$col->setValue($val); |
|
94
|
|
|
|
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|