Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Collection.php 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace think\paginator;
  3. use Exception;
  4. use think\Paginator;
  5. /**
  6. * Class Collection
  7. * @package think\paginator
  8. * @method integer total()
  9. * @method integer listRows()
  10. * @method integer currentPage()
  11. * @method string render()
  12. * @method Paginator fragment($fragment)
  13. * @method Paginator appends($key, $value)
  14. * @method integer lastPage()
  15. * @method boolean hasPages()
  16. */
  17. class Collection extends \think\Collection
  18. {
  19. /** @var Paginator */
  20. protected $paginator;
  21. public function __construct($items = [], Paginator $paginator = null)
  22. {
  23. $this->paginator = $paginator;
  24. parent::__construct($items);
  25. }
  26. public static function make($items = [], Paginator $paginator = null)
  27. {
  28. return new static($items, $paginator);
  29. }
  30. public function toArray()
  31. {
  32. if ($this->paginator) {
  33. try {
  34. $total = $this->total();
  35. } catch (Exception $e) {
  36. $total = null;
  37. }
  38. return [
  39. 'total' => $total,
  40. 'per_page' => $this->listRows(),
  41. 'current_page' => $this->currentPage(),
  42. 'data' => parent::toArray()
  43. ];
  44. } else {
  45. return parent::toArray();
  46. }
  47. }
  48. public function __call($method, $args)
  49. {
  50. if ($this->paginator && method_exists($this->paginator, $method)) {
  51. return call_user_func_array([$this->paginator, $method], $args);
  52. } else {
  53. throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
  54. }
  55. }
  56. }