If I have a DateRange class, and I want to translate a DateRange into SQL (e.g. some_col >= '2015-3-5' AND some_col <= '2015-3-5'), where should I put the method that does the translating? Nothing I've come up so far with seems very good.
Option #1: Put a method in the DateRange class. (Downside: close coupling of the DateRange class and SQL?)
class DateRange {
public function toSQL ($colName) {...}
}
Opton #2: Make a class that only has one method in it?
class DateRangeTranslator {...}
Option #3: a standalone function
function dateRangeToSQLWhereCondition (DateRange $dr, $colName) {...}
I'm leaning towards option #2. But is there a better way?
EDIT:
It's a bit tedious but here's what I've got now:
class DateRangeSQLBuilder {
public $dateRange;
public function buildWhereCondition ($colName) {...}
}
And you would use it like so:
$dateRange = new DateRange($fromDate, $toDate);
$builder = new DateRangeSQLBuilder($dateRange);
$whereConditions[] = $builder->buildWhereCondition('runDate');