I have a use case where I do internal data computations using English system, due to various precomputed graph coefficients and other data already using the English system. But for output I need to convert some of my data to Metric system and leave some data in English system to be used for debugging purposes to follow the computation. (Debugging information will be there to stay long-term and not just used during development).
My question is this - where within the MVC do I make the value conversions, and why?
My current choices to do this conversion are in the Controller or in the View.
If I do the conversion inside the controller, I will essentially need to maintain two sets of variables (one for computations, one for to pass to View.
If I do conversion in View, my Controller will be leaner due to only knowing English system, but the View will acquire various conversion coefficients and conversion code and thus become heavier.
There is perhaps a third option - does conversion need to essentially become another layer between Controller and the View? If so where or how do I wire it in?
More Info on my specific situation
I have a PHP web application written in Zend Expressive. Controllers there are called Handlers
.
class Handler // aka Controller
{
private $renderer; // renders view template
function handle($request)
{
$system = $request->getUnitSystem(); //"English", "Metric"
$page = $request->getPage(); //page number
$data = $this->repository->getEnglishData(); //PHP array of English-valued data sets
/* Conversions in Handler */
$data[$page]['height'] = $this->valueConverter->feet_to_meters($data[$page]['height']);
$data[$page]['height_unit'] = $system->isMetric() ? 'meters' : 'feet';
$params = array(
'paginator' => $this->paginator->getPaginator($data, $page),
);
/* Send to View */
return new HtmlResponse($this->renderer->render('show_data.phtml', $params));
}
}
So above I have the following players:
$system
, which is the value of the unit system (English or Metric)$valueConverter
, which is a library of value conversion methods$data
, a PHP array with a set of of English-valued data
I use conversion in the Controller to update values to use correct Unit System, and I send those off into the View. View displays them as-is.
Extra Complication when to different systems are to be displayed
I also have a use case where I need to show debugging information on the screen in English units (show internal computations for debugging purposes) and show "Real" information in converted Metric units. So different unit systems may need to be displayed at the same time. How to structure my code to handle this use case?