refactor convert addon, replace each() calls with foreach loop

pull/1577/head
Art4 2024-11-26 08:23:27 +00:00
parent d7d6a43655
commit fc0dda0cd9
1 changed files with 8 additions and 12 deletions

View File

@ -6,7 +6,6 @@
* Author: Mike Macgirvin <http://macgirvin.com/profile/mike> * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
*/ */
use Friendica\App;
use Friendica\Core\Hook; use Friendica\Core\Hook;
function convert_install() { function convert_install() {
@ -26,7 +25,7 @@ function convert_content() {
// @TODO Let's one day rewrite this to a modern composer package // @TODO Let's one day rewrite this to a modern composer package
include 'UnitConvertor.php'; include 'UnitConvertor.php';
class TP_Converter extends UnitConvertor $conv = new class('en') extends UnitConvertor
{ {
public function __construct(string $lang = 'en') public function __construct(string $lang = 'en')
{ {
@ -43,7 +42,7 @@ function convert_content() {
private function findBaseUnit($from, $to) private function findBaseUnit($from, $to)
{ {
while (list($skey, $sval) = each($this->bases)) { foreach ($this->bases as $skey => $sval) {
if ($skey == $from || $to == $skey || in_array($to, $sval) || in_array($from, $sval)) { if ($skey == $from || $to == $skey || in_array($to, $sval) || in_array($from, $sval)) {
return $skey; return $skey;
} }
@ -63,7 +62,7 @@ function convert_content() {
$cells[] = $cell; $cells[] = $cell;
// We now have the base unit and value now lets produce the table; // We now have the base unit and value now lets produce the table;
while (list($key, $val) = each($this->bases[$base_unit])) { foreach ($this->bases[$base_unit] as $val) {
$cell ['value'] = $this->convert($value, $from_unit, $val, $precision) . ' ' . $val; $cell ['value'] = $this->convert($value, $from_unit, $val, $precision) . ' ' . $val;
$cell ['class'] = ($val == $from_unit || $val == $to_unit) ? 'framedred' : ''; $cell ['class'] = ($val == $from_unit || $val == $to_unit) ? 'framedred' : '';
$cells[] = $cell; $cells[] = $cell;
@ -86,9 +85,7 @@ function convert_content() {
return $string; return $string;
} }
} };
$conv = new TP_Converter('en');
$conversions = [ $conversions = [
'Temperature' => ['base' => 'Celsius', 'Temperature' => ['base' => 'Celsius',
@ -176,10 +173,10 @@ function convert_content() {
] ]
]; ];
while (list($key, $val) = each($conversions)) { foreach ($conversions as $key => $val) {
$conv->addConversion($val['base'], $val['conv']); $conv->addConversion($val['base'], $val['conv']);
$list[$key][] = $val['base']; $list[$key][] = $val['base'];
while (list($ukey, $uval) = each($val['conv'])) { foreach ($val['conv'] as $ukey => $uval) {
$list[$key][] = $ukey; $list[$key][] = $ukey;
} }
} }
@ -202,10 +199,9 @@ function convert_content() {
$o .= '<input name="value" type="text" id="value" value="' . $value . '" size="10" maxlength="10" />'; $o .= '<input name="value" type="text" id="value" value="' . $value . '" size="10" maxlength="10" />';
$o .= '<select name="from_unit" size="12">'; $o .= '<select name="from_unit" size="12">';
reset($list); foreach ($list as $key => $val) {
while(list($key, $val) = each($list)) {
$o .= "\n\t<optgroup label=\"$key\">"; $o .= "\n\t<optgroup label=\"$key\">";
while(list($ukey, $uval) = each($val)) { foreach ($val as $ukey => $uval) {
$selected = (($uval == $_POST['from_unit']) ? ' selected="selected" ' : ''); $selected = (($uval == $_POST['from_unit']) ? ' selected="selected" ' : '');
$o .= "\n\t\t<option value=\"$uval\" $selected >$uval</option>"; $o .= "\n\t\t<option value=\"$uval\" $selected >$uval</option>";
} }