一个类中的两个方法如此接近它们的作用

时间:2012-10-12 17:29:04

标签: php

我有这个类,这两个方法彼此密切相关。我不想传递旗帜,所以我将它们分开。我想知道是否有办法重写它,以便我不必如此密切地重复!

  class Test extends Controller
    {
        public static function nonFormattedData($param)
        {
            $arr = array();
            if (is_array($param)) {
                $i = 0;
                $sql = "
                    select *
                    from table1
                    where
                    ";


                if (isset($param['startDate'])) {
                    $sql .= "   date_created between ? AND  ?";
                    $arr[] = $param['startDate'];
                    $arr[] = $param['endDate'];
                    $i++;
                }
                if (isset($param['amount']) && !empty($param['amount'])) {

                    if ($i > 0) $sql .= " AND ";
                    $sql .= " balance= ?";
                    $arr[] = $param['amount'];
                    $i++;
                }
                if (isset($param) && !empty($param['amount'])) {

                    if ($i > 0) $sql .= " AND ";
                    $sql .= " balance= ?";
                    $arr[] = $param['amount'];
                    $i++;
                }
                if (isset($param['createdBy']) && !empty($param['createdBy'])) {

                    if ($i > 0) $sql .= " AND ";
                    $sql .= " column2 like '%Created By: " . $param['createdBy'] . "%'";
                }
                $sql .= ' group by id.table1 ';
                $rs = Query::RunQuery($sql, $arr);
                foreach ($rs as $row) {
                    $records = new Account();
                    $results[] = $records;
                }
                return $results;
            }
        }




        public static function formattedData($serArray, $orderBy = "giftcardaccount_id desc", $offset = 0, $limit = 10)
        {
                $arr = array();
            if (is_array($param)) {
                $i = 0;
                $sql = "
                    select *
                    from table1
                    where
                    ";


                if (isset($param['startDate'])) {
                    $sql .= "   date_created between ? AND  ?";
                    $arr[] = $param['startDate'];
                    $arr[] = $param['endDate'];
                    $i++;
                }
                if (isset($param['amount']) && !empty($param['amount'])) {

                    if ($i > 0) $sql .= " AND ";
                    $sql .= " balance= ?";
                    $arr[] = $param['amount'];
                    $i++;
                }
                if (isset($param) && !empty($param['amount'])) {

                    if ($i > 0) $sql .= " AND ";
                    $sql .= " balance= ?";
                    $arr[] = $param['amount'];
                    $i++;
                }
                if (isset($param['createdBy']) && !empty($param['createdBy'])) {

                    if ($i > 0) $sql .= " AND ";
                    $sql .= " column2 like '%Created By: " . $param['createdBy'] . "%'";
                }
                $sql .= ' group by id.table1 ';
                $rs = Query::RunQuery($sql, $arr);

                return array("data" => $rs);
            }
        }


    }

1 个答案:

答案 0 :(得分:2)

为什么没有一个方法,但有一个可选的格式化选项对象/数组?

public static function getData($params, $formatting = null) {
    // continue as normal, adding formatting if it's there
}