activeQuery = $activeQuery; } /** * @param Closure|array $callback * @throws Exception */ public function setCallback($callback) { if (!is_callable($callback, true)) { throw new Exception('非法回调函数~'); } $this->_callback = $callback; } /** * @param int $number */ public function setOffset(int $number) { if ($number < 0) { $number = 0; } $this->_offset = $number; } /** * @param int $number */ public function setLimit(int $number) { if ($number < 1) { $number = 100; } else if ($number > 5000) { $number = 5000; } $this->_limit = $number; } /** * @param int $number */ public function setMax(int $number) { if ($number < 0) { return; } $this->_max = $number; } /** * @param array $param */ public function search($param = []) { if ($this->_length >= $this->_max) { return; } [$length, $data] = $this->load(); if ($param !== null) { call_user_func($this->_callback, $data, $param); } else { call_user_func($this->_callback, $data); } unset($data); if ($length < $this->_limit) { return; } $this->search($param); } /** * @return array|Collection */ private function load() { if ($this->_length + $this->_limit > $this->_max) { $this->_limit = $this->_length + $this->_limit - $this->_max; } $data = $this->activeQuery->limit($this->_offset, $this->_limit)->get(); $this->_offset += $this->_limit; $this->_length += $data->size(); return [$data->size(), $data]; } }