mrandrewmills/Fixed-Width-Text-File-Toolkit

more sophisticated constructor?

mrandrewmills opened this issue · 4 comments

Right now the constructor allows you to create a "blank slate" object, or to specify the file that is to be used (in which case it pre-loads and parses the data file). You can't set any of the other properties, such as line length or the header row boolean, through the constructor. It would be nice to be able to have the constructor set all necessary values in addition to the file name.

Code added, testing pending.

Testing has uncovered two complications, which may or may not be related.

  1. the constructor currently calls setFilename(), then setLineLength(), then setHasHeaderRow() in that sequence. Since setFilename() subsequently invokes the readHeaderRow() and readData() methods, it is possible for the data file to be read with the default values (i.e. hasHeaderRow = true, and lineLength = 4096) before those properties get modified.

A subtle logic bug-- could be good for a blog entry, or job interview question.

Modifying the sequence of function calls within the constructor so that setFilename() is the last one called should fix this.

  1. getHasHeaderRow() seems to always return true, even if it has been explicitly set to false. Cause currently unknown. Further testing should isolate the underlying cause.

Fixed complication 2. Using if ($hasHeaderRow) to screen out empty value also had side effect of filtering out value of false. Relying on is_bool() instead. Need to do same thing with setHasHeaderRow() conditional in constructor too, I think.

Tested initializing and retrieving the lineLength, filename and hasHeaderRow properties, both with the constructor and the invidual setters, and everything appears to work.