Open Wibbmer opened 2 years ago
This is actually a good idea! But I also see some duplication in these functions and the ConversionTrait. Ideally I want to reuse this logic.
The modifier now uses the ConversionTrait
<?php
namespace Vannut\StatamicWeather\Modifiers;
use Statamic\Modifiers\Modifier;
use Vannut\StatamicWeather\ConversionTrait;
use Vannut\StatamicWeather\Settings;
class ConvertSpeedToUnitModifier extends Modifier {
use ConversionTrait;
protected static $handle = 'unit';
public function index($value, $params, $context)
{
// Get the default units set in the control panel from which to convert
$config = (new Settings)->get();
$currentUnits = $config->get('units', 'metric');
$targetUnits;
if ($param = array_get($params, 0))
{
// Either get the variable from the context, or if it doesn't exist,
// use the parameter itself - we'll assume its a number.
$targetUnits = array_get($context, $param, $param);
}
return $this->speedToUnit($value, $currentUnits, $targetUnits);
}
}
The new function in ConversionTrait
/**
* Converts speed from default speed units to selected
*
* @param float $speed The current wind speed either in ms or mph
* @param string $measurement Metric/Imperial
* @param string $to The desired speed unit
*
* @return $convertedSpeed
*/
private function speedToUnit($speed, $measurement, $to)
{
if ($measurement == 'metric')
{
switch ($to)
{
case 'kmh':
$convertedSpeed = round($speed * 3.6, 2);
break;
case 'mph':
$convertedSpeed = round($speed * 2.237, 2);
break;
case 'bft':
$convertedSpeed = $this->msToBft($speed);
break;
case 'ms':
$convertedSpeed = $speed;
break;
}
} else
{
switch ($to)
{
case 'kmh':
$convertedSpeed = round($speed * 1.609, 2);
break;
case 'ms':
$convertedSpeed = round($speed / 2.237, 2);
break;
case 'bft':
$convertedSpeed = $this->mphToBft($speed);
break;
case 'mph':
$convertedSpeed = $speed;
break;
}
}
return $convertedSpeed;
}
I've left in ms and mph even though they are redundant to avoid accidental breakage when changing units but not updating the modifier in antlers.
I've created separate functions for each speed unit now, to DRY up the two switch
statements. I'm not quite sure about it, so let me know what you think. The modifier stays untouched by this.
In ConversionTrait.php
private function speedToUnit($speed, $measurement, $to)
{
switch ($to)
{
case 'kmh':
$convertedSpeed = $this->speedToKmh($speed,$measurement);
break;
case 'mph':
$convertedSpeed = $this->speedToMph($speed,$measurement);
break;
case 'bft':
$convertedSpeed = $this->speedToBft($speed,$measurement);
break;
case 'ms':
$convertedSpeed = $this->speedtToMs($speed,$measurement);
break;
}
return $convertedSpeed;
}
private function speedtToMs($speed, $measurement)
{
if ($measurement == 'metric')
{
return $speed;
} else
{
return round($speed / 2.237, 2);
}
}
private function speedToMph($speed, $measurement)
{
if ($measurement == 'metric')
{
return round($speed * 2.237, 2);
} else
{
return $speed;
}
}
private function speedToKmh($speed, $measurement)
{
if ($measurement == 'metric')
{
return round($speed * 3.6, 2);
} else
{
return round($speed * 1.609, 2);
}
}
private function speedToBft($speed, $measurement)
{
if ($measurement == 'metric')
{
return $this->msToBft($speed);
} else
{
return $this->mphToBft($speed);
}
}
Ill pick this up again in the V2 release of the addon.
I've tinkered around a little and created a modifier for the
{{ wind_speed }}
field. This replaces the need i.e. for a custom{{speed_bft}}
field.Now you can convert to speed units on the fly like so:
{{ wind_speed | unit:{unit} }}
replace
{unit}
with one of the following to convert or remove the modifier for default unitsfor metric units (no ms since it's the default)
kmh
mph
bft
for imperial units (no mph since it's the default)
kmh
ms
bft
To register it in
ServiceProvider.php
just addAnd for the modifier itself create the file
src\Modifiers\ConvertSpeedToUnitModifier.php
It's nothing too fancy, but it works :)