1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PierInfor\Gears\Components; |
6
|
|
|
|
7
|
|
|
use PierInfor\Gears\Entity\Gears; |
8
|
|
|
use PierInfor\Gears\Entity\Gear; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* System calculate system of gears |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class System implements SystemInterface |
15
|
|
|
{ |
16
|
|
|
/** @var Gears */ |
17
|
|
|
protected Gears $gears; |
18
|
|
|
|
19
|
|
|
/** @var Gear */ |
20
|
|
|
protected Gear $previousGear; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ctor |
24
|
|
|
*/ |
25
|
16 |
|
public function __construct() |
26
|
|
|
{ |
27
|
16 |
|
$this->gears = (new Gears()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* load gears from json file |
32
|
|
|
*/ |
33
|
3 |
|
public function load(string $filename): System |
34
|
|
|
{ |
35
|
3 |
|
if (file_exists($filename)) { |
36
|
3 |
|
$content = file_get_contents($filename); |
37
|
3 |
|
$obj = json_decode($content); |
38
|
3 |
|
unset($content); |
39
|
3 |
|
if (false != $obj) { |
40
|
3 |
|
$this->gears->hydrate($obj); |
41
|
|
|
} |
42
|
3 |
|
unset($obj); |
43
|
|
|
} |
44
|
3 |
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* process gear system |
49
|
|
|
*/ |
50
|
2 |
|
public function process(): System |
51
|
|
|
{ |
52
|
2 |
|
$cpt = 0; |
53
|
2 |
|
$gears = $this->gears->getGears(); |
54
|
2 |
|
if (count($gears) > 0) { |
55
|
2 |
|
$forward = $gears[0]->getForward(); |
56
|
2 |
|
foreach ($gears as $gear) { |
57
|
2 |
|
if ($gear instanceof Gear) { |
58
|
2 |
|
if ($cpt != 0) { |
59
|
2 |
|
if (!$gear->getComposed()) { |
60
|
2 |
|
$forward = !$forward; |
61
|
|
|
} |
62
|
2 |
|
$gears[$cpt]->setForward($forward); |
63
|
2 |
|
$ratio = $this->ratio($this->previousGear->getTeeth(), $gear->getTeeth()); |
64
|
2 |
|
$rpm = (!$gear->getComposed()) |
65
|
2 |
|
? $this->speedOut($this->previousGear->getSpeed(), $ratio) |
66
|
2 |
|
: $this->previousGear->getSpeed(); |
67
|
2 |
|
$gears[$cpt]->setSpeed($rpm); |
68
|
2 |
|
$torque = (!$gear->getComposed()) |
69
|
2 |
|
? $this->torqueOut($ratio, $this->previousGear->getTorque()) |
70
|
2 |
|
: $this->previousGear->getTorque(); |
71
|
2 |
|
$gears[$cpt]->setTorque($torque); |
72
|
|
|
} |
73
|
2 |
|
$this->previousGear = $gear; |
74
|
|
|
} |
75
|
2 |
|
$cpt++; |
76
|
|
|
} |
77
|
2 |
|
$this->gears->setGears($gears); |
78
|
|
|
} |
79
|
2 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* returns gears |
84
|
|
|
* @return Gears |
85
|
|
|
*/ |
86
|
3 |
|
public function getGears(): Gears |
87
|
|
|
{ |
88
|
3 |
|
return $this->gears; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* returns system as string |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
2 |
|
public function __toString(): string |
96
|
|
|
{ |
97
|
2 |
|
$lines = sprintf( |
98
|
2 |
|
static::_TITLE_FMT, |
99
|
2 |
|
static::_ID, |
100
|
2 |
|
static::_TEETH, |
101
|
2 |
|
static::_TORQUE, |
102
|
2 |
|
static::_SPEED, |
103
|
2 |
|
static::_FORWARD, |
104
|
2 |
|
static::_COMPOSED |
105
|
|
|
); |
106
|
2 |
|
$gears = $this->getGears()->getGears(); |
107
|
2 |
|
foreach ($gears as $gear) { |
108
|
2 |
|
$lines .= sprintf( |
109
|
2 |
|
static::_ITEMS_FMT, |
110
|
2 |
|
$gear->getId(), |
111
|
2 |
|
$gear->getTeeth(), |
112
|
2 |
|
$gear->getTorque(), |
113
|
2 |
|
$gear->getSpeed(), |
114
|
2 |
|
$gear->getForward() ? static::_TRUE : static::_FALSE, |
115
|
2 |
|
$gear->getComposed() ? static::_TRUE : static::_FALSE, |
116
|
|
|
); |
117
|
|
|
} |
118
|
2 |
|
unset($gears); |
119
|
2 |
|
return $lines; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* returns teeth ratio |
124
|
|
|
*/ |
125
|
4 |
|
protected function ratio(float $nbTeethIn, float $nbTeethOut): float |
126
|
|
|
{ |
127
|
4 |
|
if ($nbTeethIn == 0) { |
128
|
2 |
|
return 0; |
129
|
|
|
} |
130
|
2 |
|
return $nbTeethOut / $nbTeethIn; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* returns speed in Rpm |
135
|
|
|
*/ |
136
|
4 |
|
protected function speedOut(float $rmpIn, float $ratio): float |
137
|
|
|
{ |
138
|
4 |
|
if ($ratio == 0) { |
139
|
2 |
|
return 0; |
140
|
|
|
} |
141
|
2 |
|
return $rmpIn / $ratio; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* return torque in Nm |
146
|
|
|
*/ |
147
|
4 |
|
protected function torqueOut(float $ratio, float $torqueIn): float |
148
|
|
|
{ |
149
|
4 |
|
return $ratio * $torqueIn; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|