Skip to main content
2 of 3
added 189 characters in body

How to organize code which converts from one type to another

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 {...}

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 function buildWhereCondition (DateRange $dr, $colName) {...}

}