Pagination.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ Zuo77Bn8GZF94jwkspr4JiodYgnQzhd4lNW2p2HodY4=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2015 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.4
  11. */
  12. namespace Cube\View\Helper;
  13. use Cube\Paginator;
  14. /**
  15. * pagination view helper
  16. *
  17. * Class Pagination
  18. *
  19. * @package Cube\View\Helper
  20. */
  21. class Pagination extends AbstractHelper
  22. {
  23. /**
  24. *
  25. * paginator object
  26. *
  27. * @var \Cube\Paginator
  28. */
  29. protected $_paginator;
  30. /**
  31. *
  32. * path where to search for navigation view partials
  33. *
  34. * @var string
  35. */
  36. protected $_path;
  37. /**
  38. *
  39. * get the paginator object
  40. *
  41. * @return \Cube\Paginator
  42. * @throws \InvalidArgumentException
  43. */
  44. public function getPaginator()
  45. {
  46. if (!($this->_paginator instanceof Paginator)) {
  47. throw new \InvalidArgumentException(
  48. sprintf("'%s' must be of type \Cube\Paginator.", $this->_paginator));
  49. }
  50. return $this->_paginator;
  51. }
  52. /**
  53. *
  54. * set the paginator object
  55. *
  56. * @param \Cube\Paginator $paginator
  57. *
  58. * @return \Cube\View\Helper\Pagination
  59. * @throws \InvalidArgumentException
  60. */
  61. public function setPaginator($paginator)
  62. {
  63. if ($paginator instanceof Paginator) {
  64. $this->_paginator = $paginator;
  65. }
  66. else {
  67. throw new \InvalidArgumentException(
  68. sprintf("'%s' must be of type \Cube\Paginator.", $this->_paginator));
  69. }
  70. return $this;
  71. }
  72. /**
  73. *
  74. * get paginator partials path
  75. *
  76. * @return string
  77. */
  78. public function getPath()
  79. {
  80. if (empty($this->_path)) {
  81. $this->_path = $this->getView()->getViewsPath();
  82. }
  83. return $this->_path;
  84. }
  85. /**
  86. *
  87. * set paginator partials path
  88. *
  89. * @param string $path
  90. *
  91. * @return \Cube\View\Helper\Pagination
  92. * @throws \InvalidArgumentException
  93. */
  94. public function setPath($path)
  95. {
  96. if (!is_dir($path)) {
  97. throw new \InvalidArgumentException(
  98. sprintf("The navigation files path (%s) does not exist.", $path));
  99. }
  100. $this->_path = $path;
  101. return $this;
  102. }
  103. /**
  104. *
  105. * renders pagination helper
  106. *
  107. * @param \Cube\Paginator $paginator
  108. * @param mixed $scrollingStyle
  109. * @param string $partial
  110. *
  111. * @return $this|string
  112. */
  113. public function pagination(Paginator $paginator = null, $scrollingStyle = null, $partial = null)
  114. {
  115. if ($paginator === null) {
  116. return $this;
  117. }
  118. else {
  119. $this->setPaginator($paginator);
  120. $this->setPartial($partial);
  121. }
  122. if ($scrollingStyle !== null) {
  123. $this->getPaginator()->setScrollingStyle($scrollingStyle);
  124. }
  125. $view = $this->getView();
  126. $view->paginator = $paginator;
  127. $view->setVariables(
  128. get_object_vars($paginator->getPages()));
  129. return $view->process(
  130. $this->getPartial(), true);
  131. }
  132. }