params = $params; $this->format = $format; } /** * @param $key * @return string * @throws AppException */ private function getTypeClass($key) { if (isset($this->format[$key])) { $className = ucfirst($this->format[$key]); } else { $className = 'Equal'; } $className = __NAMESPACE__ . '\\' . $className; if (!class_exists($className)) { throw new AppException('不存在的参数类型'.$className); } return $className; } /** * 根据参数生成where条件 * todo 这个方法应该定义在哪个类中 $builder 怎么传递 * @param Model $builder * @return Model * @throws AppException */ public function toWhere($builder) { // 过滤调空的 $result = array_filter($this->params, function ($value) { return isset($value) && $value !== ''; }); //根据key的类型 foreach ($result as $key => $value) { $className = $this->getTypeClass($key); (new $className($builder))->format($key, $value); }; return $builder; } // /** // * 输出所有参数 // * @return mixed // */ // public function toBuilder() // { // if (!$this->isFormatted) { // $this->params = $this->format($this->params); // } // return $this->params; // } }