I have an object that houses only static functions. The functions are stand alone and are not dependent on the state of the object.
I hear that static functions are a nightmare in terms of testing, an anti-pattern if you will. I get it, the state of the object could be impossible to predict.. But these are stateless.
My question is, Should I place these functions into a class of their own or am I ok by leaving them as static methods?
class Utility {
public static function arrayToInsertStatement($table, array $data) {
$sql = "INSERT INTO %s (%s) VALUES (:%s)";
return sprintf($sql, $table, implode(', ', array_keys($data)), implode(', :', array_keys($data)));
}
public static function arrayToSelectStatment($table, array $fields, $whereStatement) {
$sql = "SELECT %s FROM %s %s";
if ($whereStatement) {
$whereStatement = sprintf(" WHERE %s", $whereStatement);
}
return sprintf($sql, implode(',', $fields), $table, $whereStatement);
}
public static function arrayToUpdateStatement($table, array $data, $whereStatement = null) {
$t_sql = "UPDATE %s SET %s%s";
$sets = self::arrayToKeyedArray($data);
if ($whereStatement) {
$whereStatement = sprintf(" WHERE %s", $whereStatement);
}
return sprintf($t_sql, $table, implode(',', $sets), $whereStatement);
}
public static function arrayToWhereStatement($data, $joinType = 'AND') {
$joiner = sprintf(" %s ", $joinType);
return implode($joiner, self::arrayToKeyedArray($data));
}
public static function arrayToOrderBy(array $data) {
$o = [];
foreach ($data as $field => $direction) {
$directionBool = substr(strtolower($direction), 0, strlen($direction)) === substr('descending', 0, strlen($direction));
$o[] = sprintf($field . ' %1$s', ($directionBool ? 'DESC' : 'ASC'));
}
return implode($o, ',');
}
public static function arrayToKeyedArray($data) {
return array_map(function($value) {
return sprintf('%1$s = :%1$s', $value);
}, array_keys($data));
}