Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / RedBeanPHP / BeanCollection.php
1 <?php
2
3 namespace RedBeanPHP;
4
5 use RedBeanPHP\Cursor as Cursor;
6 use RedBeanPHP\Repository as Repository;
7
8 /**
9  * BeanCollection.
10  *
11  * The BeanCollection represents a collection of beans and
12  * makes it possible to use database cursors. The BeanCollection
13  * has a method next() to obtain the first, next and last bean
14  * in the collection. The BeanCollection does not implement the array
15  * interface nor does it try to act like an array because it cannot go
16  * backward or rewind itself.
17  *
18  * Use the BeanCollection for large datasets where skip/limit is not an
19  * option. Keep in mind that ID-marking (querying a start ID) is a decent
20  * alternative though.
21  *
22  * @file    RedBeanPHP/BeanCollection.php
23  * @author  Gabor de Mooij and the RedBeanPHP community
24  * @license BSD/GPLv2
25  *
26  * @copyright
27  * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community
28  * This source file is subject to the BSD/GPLv2 License that is bundled
29  * with this source code in the file license.txt.
30  */
31 class BeanCollection
32 {
33         /**
34          * @var Cursor
35          */
36         protected $cursor = NULL;
37
38         /**
39          * @var Repository
40          */
41         protected $repository = NULL;
42
43         /**
44          * @var string
45          */
46         protected $type = NULL;
47
48         /**
49          * Constructor, creates a new instance of the BeanCollection.
50          *
51          * @param string     $type       type of beans in this collection
52          * @param Repository $repository repository to use to generate bean objects
53          * @param Cursor     $cursor     cursor object to use
54          *
55          * @return void
56          */
57         public function __construct( $type, Repository $repository, Cursor $cursor )
58         {
59                 $this->type = $type;
60                 $this->cursor = $cursor;
61                 $this->repository = $repository;
62         }
63
64         /**
65          * Returns the next bean in the collection.
66          * If called the first time, this will return the first bean in the collection.
67          * If there are no more beans left in the collection, this method
68          * will return NULL.
69          *
70          * @return OODBBean|NULL
71          */
72         public function next()
73         {
74                 $row = $this->cursor->getNextItem();
75                 if ( $row ) {
76                         $beans = $this->repository->convertToBeans( $this->type, array( $row ) );
77                         $bean = array_shift( $beans );
78                         return $bean;
79                 }
80                 return NULL;
81         }
82
83         /**
84          * Closes the underlying cursor (needed for some databases).
85          *
86          * @return void
87          */
88         public function close()
89         {
90                 $this->cursor->close();
91         }
92 }