Zend_Paginator-Advanced.xml 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.paginator.advanced">
  4. <title>Advanced usage</title>
  5. <sect2 id="zend.paginator.advanced.adapters">
  6. <title>Custom data source adapters</title>
  7. <para>
  8. At some point you may run across a data type that is not covered by
  9. the packaged adapters. In this case, you will need to write your
  10. own.
  11. </para>
  12. <para>
  13. To do so, you must implement
  14. <classname>Zend_Paginator_Adapter_Interface</classname>. There are two
  15. methods required to do this:
  16. </para>
  17. <itemizedlist>
  18. <listitem>
  19. <para>count()</para>
  20. </listitem>
  21. <listitem>
  22. <para>getItems($offset, $itemCountPerPage)</para>
  23. </listitem>
  24. </itemizedlist>
  25. <para>
  26. Additionally, you'll want to implement a constructor that takes
  27. your data source as a parameter and stores it as a protected or
  28. private property. How you wish to go about doing this
  29. specifically is up to you.
  30. </para>
  31. <para>
  32. If you've ever used the SPL interface <ulink
  33. url="http://www.php.net/~helly/php/ext/spl/interfaceCountable.html">Countable</ulink>,
  34. you're familiar with <methodname>count()</methodname>. As used with
  35. <classname>Zend_Paginator</classname>, this is the total number of items
  36. in the data collection.
  37. Additionally, the <classname>Zend_Paginator</classname> instance provides a method
  38. <methodname>countAllItems()</methodname> that proxies to the adapter
  39. <methodname>count()</methodname> method.
  40. </para>
  41. <para>
  42. The <methodname>getItems()</methodname> method is only slightly more
  43. complicated. For this, your adapter is supplied with an offset and
  44. the number of items to display per page. You must return the
  45. appropriate slice of data. For an array, that would be:
  46. </para>
  47. <programlisting language="php"><![CDATA[
  48. return array_slice($this->_array, $offset, $itemCountPerPage);
  49. ]]></programlisting>
  50. <para>
  51. Take a look at the packaged adapters (all of which implement the
  52. <classname>Zend_Paginator_Adapter_Interface</classname>) for ideas of how you
  53. might go about implementing your own.
  54. </para>
  55. </sect2>
  56. <sect2 id="zend.paginator.advanced.scrolling-styles">
  57. <title>Custom scrolling styles</title>
  58. <para>
  59. Creating your own scrolling style requires that you implement
  60. <classname>Zend_Paginator_ScrollingStyle_Interface</classname>, which defines
  61. a single method, <methodname>getPages()</methodname>. Specifically,
  62. </para>
  63. <programlisting language="php"><![CDATA[
  64. public function getPages(Zend_Paginator $paginator, $pageRange = null);
  65. ]]></programlisting>
  66. <para>
  67. This method should calculate a lower and upper bound for page
  68. numbers within the range of so-called "local" pages (that is, pages
  69. that are nearby the current page).
  70. </para>
  71. <para>
  72. Unless it extends another scrolling style (see
  73. <classname>Zend_Paginator_ScrollingStyle_Elastic</classname> for an example),
  74. your custom scrolling style will inevitably end with something
  75. similar to the following line of code:
  76. </para>
  77. <programlisting language="php"><![CDATA[
  78. return $paginator->getPagesInRange($lowerBound, $upperBound);
  79. ]]></programlisting>
  80. <para>
  81. There's nothing special about this call; it's merely a convenience
  82. method to check the validity of the lower and upper bound and
  83. return an array of the range to the paginator.
  84. </para>
  85. <para>
  86. When you're ready to use your new scrolling style, you'll need to
  87. tell <classname>Zend_Paginator</classname> what directory to look in. To do
  88. that, do the following:
  89. </para>
  90. <programlisting language="php"><![CDATA[
  91. $prefix = 'My_Paginator_ScrollingStyle';
  92. $path = 'My/Paginator/ScrollingStyle/';
  93. Zend_Paginator::addScrollingStylePrefixPath($prefix, $path);
  94. ]]></programlisting>
  95. </sect2>
  96. <sect2 id="zend.paginator.advanced.caching">
  97. <title>Caching features</title>
  98. <para>
  99. <classname>Zend_Paginator</classname> can be told to cache the data it has already
  100. passed on, preventing the adapter from fetching them each time they are used.
  101. To tell paginator to automatically cache the adapter's data, just pass to
  102. its <methodname>setCache()</methodname> method a <classname>Zend_Cache_Core</classname>
  103. instance.
  104. </para>
  105. <programlisting language="php"><![CDATA[
  106. $paginator = Zend_Paginator::factory($someData);
  107. $fO = array('lifetime' => 3600, 'automatic_serialization' => true);
  108. $bO = array('cache_dir'=>'/tmp');
  109. $cache = Zend_cache::factory('Core', 'File', $fO, $bO);
  110. Zend_Paginator::setCache($cache);
  111. ]]></programlisting>
  112. <para>
  113. As far as <classname>Zend_Paginator</classname> has got a
  114. <classname>Zend_Cache_Core</classname> instance, data will be cached. Sometimes you
  115. would like not to cache data even if you already passed a cache instance. You should
  116. then use <methodname>setCacheEnable()</methodname> for that.
  117. </para>
  118. <programlisting language="php"><![CDATA[
  119. $paginator = Zend_Paginator::factory($someData);
  120. // $cache is a Zend_Cache_Core instance
  121. Zend_Paginator::setCache($cache);
  122. // ... later on the script
  123. $paginator->setCacheEnable(false);
  124. // cache is now disabled
  125. ]]></programlisting>
  126. <para>
  127. When a cache is set, data are automatically stored in it and pulled out from
  128. it. It then can be useful to empty the cache manually. You can get this done by
  129. calling <methodname>clearPageItemCache($pageNumber)</methodname>.
  130. If you don't pass any parameter, the whole cache will be empty. You can optionally
  131. pass a parameter representing the page number to empty in the cache:
  132. </para>
  133. <programlisting language="php"><![CDATA[
  134. $paginator = Zend_Paginator::factory($someData);
  135. Zend_Paginator::setCache($cache);
  136. $items = $paginator->getCurrentItems();
  137. // page 1 is now in cache
  138. $page3Items = $paginator->getItemsByPage(3);
  139. // page 3 is now in cache
  140. // clear the cache of the results for page 3
  141. $paginator->clearPageItemCache(3);
  142. // clear all the cache data
  143. $paginator->clearPageItemCache();
  144. ]]></programlisting>
  145. <para>
  146. Changing the item count per page will empty the whole cache
  147. as it would have become invalid:
  148. </para>
  149. <programlisting language="php"><![CDATA[
  150. $paginator = Zend_Paginator::factory($someData);
  151. Zend_Paginator::setCache($cache);
  152. // fetch some items
  153. $items = $paginator->getCurrentItems();
  154. // all the cache data will be flushed:
  155. $paginator->setItemCountPerPage(2);
  156. ]]></programlisting>
  157. <para>
  158. It is also possible to see the data in cache and ask for them directly.
  159. <methodname>getPageItemCache()</methodname> can be used for that:
  160. </para>
  161. <programlisting language="php"><![CDATA[
  162. $paginator = Zend_Paginator::factory($someData);
  163. $paginator->setItemCountPerPage(3);
  164. Zend_Paginator::setCache($cache);
  165. // fetch some items
  166. $items = $paginator->getCurrentItems();
  167. $otherItems = $paginator->getItemsPerPage(4);
  168. // see the cached items as a two-dimension array:
  169. var_dump($paginator->getPageItemCache());
  170. ]]></programlisting>
  171. </sect2>
  172. <sect2 id="zend.paginator.advanced.aggregator">
  173. <title>Zend_Paginator_AdapterAggregate Interface</title>
  174. <para>
  175. Depending on your application you might want to paginate objects, whose internal
  176. data-structure is equal to existing adapters, but you don't want to break up your
  177. encapsulation to allow access to this data. In other cases an object might be in a
  178. "has-an adapter" relationship, rather than the "is-an adapter" relationsship that
  179. <classname>Zend_Paginator_Adapter_Abstract</classname> promotes. For this cases you can
  180. use the <classname>Zend_Paginator_AdapterAggregate</classname> interface that behaves
  181. much like the <classname>IteratorAggregate</classname> interface of the
  182. <acronym>PHP</acronym> SPL extension.
  183. </para>
  184. <programlisting language="php"><![CDATA[
  185. interface Zend_Paginator_AdapterAggregate
  186. {
  187. /**
  188. * Return a fully configured Paginator Adapter from this method.
  189. *
  190. * @return Zend_Paginator_Adapter_Abstract
  191. */
  192. public function getPaginatorAdapter();
  193. }
  194. ]]></programlisting>
  195. <para>
  196. The interface is fairly small and only expects you to return an instance of
  197. <classname>Zend_Paginator_Adapter_Abstract</classname>. An Adapter Aggregate instance is
  198. then recognized by both <methodname>Zend_Paginator::factory()</methodname> and the
  199. constructor of <classname>Zend_Paginator</classname> and handled accordingly.
  200. </para>
  201. </sect2>
  202. </sect1>
  203. <!--
  204. vim:se ts=4 sw=4 et:
  205. -->