I am currently working on a Zend Framework project whereby the end result of the users journey through the application presents them with a large table containing their results. This table can contain varying numbers of rows, most of which are generated as the results of calculations. So far, so good.
The first; albeit minor hurdle, is that the table needs to be presented horizontally. That is, the headings are in column 1 and each additional column represents a data entity item. For example:
Forename | James | Richard Surname | Jones | Mayfair
Again, this isn’t an issue. The problem arises when I come to construct the table neatly. Using the builder pattern, I’ve got three classes: Table, Group and Row. They can be used like this:
$table = new Table($attribs);
$group = new Group(); // Nothing special about groups, they're just there for helping with presentation
$row = new Row();
$row->setTitle('Forename')
->setData(array('item1' => 'James', 'item2' => 'Richard'))
$row2 = new Row();
$row2->setTitle('Surname')
->setData(array('item1' => 'Jones', 'item2' => 'Mayfair'))
$group->addRow($row)
->addRow($row2);
$table->addGroup($group);
The above code snippet appears within a controllers’ action. The table object is then passed to the view and I’ve got a view helper that outputs to the table to my own specification.
The challenge that I’m facing however, is that I’ve ended up with a very messy ‘Results’ model. For example, I’ve got lots of:
$group->addRow($this->_resultsModel->getSurnameRow());
The ‘_resultsModel’ property is an object which contains a lot of methods that do:
public function getSurnameRow()
{
$row = new Row();
$row->setTitle('Surname');
$row->setData($this->_getAssociateArrayOfSurnames());
return $row;
}
Furthermore, the results object extends a results abstract class which contains accessor, setter and calculation methods for the raw data. Each of these ‘calculation’ methods returns an associative array of data which can be assigned to a row object. Both the results object and the abstracted class are both long and just seem disjointed.
So to summarise… I’ve ended up with one big results model that does all the calculations and returns all the row objects. Whilst it does work, I’m convinced there’s a better way of doing things. Should I be building the rows in the controller? Should I only be calling the calculations from the models? Does anyone have any experience abstracting stuff like this?
I apologise is this is a poor explanation. If anyone has any questions that I can field, let me know.
First off, controllers shouldn’t have too much logic inside – controllers are there to handle input to models and forward results to output (view), not to calculate stuff.
Second: Your results has so many other things to do, why does it have to deal with the table representation? Also, why does it have to have a lot of separate functions which differ only in a string?
I guess I’d do something along with declarative arrays: that is, I’d collect a declarative array like this:
and build something which parses this description and fills it up with actual data (using reflection if needed)
The main issue is: form follows function, algorhythms follow data structure.
If you have a data structure, which is divided into tables, groups and rows, then you are likely have an algorhythm which does the same.
A second question is: where does the change likely occur? If the table scheme can change easily, put them into a “config” (a descriptor). If the rows’ formatting can change easily, make sure they’re self-formatting (or someone knows how to ask for a formatter, without knowing what the row actually contains)
A good idea is to be able to fall back to defaults: that is, if most of the rows are constructed in a default way, then make sure you’re able to use a default method, like:
Now, it seems, that your resultsModel has too many responsibilities, you have these prefix names (getSurnameRow) – why should the resultsModel know about rows at all? Also, associative arrays (maps) are pretty fine constructs, especially in PHP. Why isn’t a getData(‘Surname’) sufficient? Why isn’t the Row capable of constructing itself from a title and an associative data?
If you want to do the same thing but with different properties, use an enum, or a string. If you need to be able to plug in things sometimes, use a factory, which looks up an index table based on property names, and falls back to a default implementation in case it isn’t an exception.
Code duplication like this is fine as long as it’s two properties and simple orthogonality – like, getX, getY. But with an infinite amount of properties, I guess it’s just duplication.