forked from unl/DB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecordList.php
125 lines (102 loc) · 3.86 KB
/
RecordList.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* Simple Active Record List implementation
*
* PHP version 5
*
* @category Publishing
* @package DB
* @author Michael Fairchild <mfairchild365@gmail.com>
* @copyright 2010 Regents of the University of Nebraska
* @license http://www1.unl.edu/wdn/wiki/Software_License BSD License
*/
namespace DB;
abstract class RecordList extends \LimitIterator implements \Countable
{
//By default, do not limit.
public $options = array('limit'=>-1, 'offset'=>0);
/**
* Get the defualt options for the list class
* Required to set the following:
* $options['listClass'] the class of the list.
* $options['itemClass'] the class of each item in the list.
* @return array
*/
abstract function getDefaultOptions();
/*
* @param $options Requires Listclass and ItemClass to work properlly.
*
* listClass: The class name of the list.
* itemClass: The class name of the items in the list.
*/
function __construct($options = array())
{
$this->options = $options + $this->options;
$this->options = $this->options + $this->getDefaultOptions();
if (!isset($this->options['listClass'])) {
Throw New Exception("No List Class was set", 500);
}
if (!isset($this->options['itemClass'])) {
Throw New Exception("No Item Class was set", 500);
}
if (!isset($this->options['array'])) {
//get a lit of all of them by default.
$this->options['array'] = $this->getAllForConstructor();
}
$list = new \ArrayIterator($this->options['array']);
parent::__construct($list, $this->options['offset'], $this->options['limit']);
}
private function getAllForConstructor()
{
$class = new $this->options['itemClass'];
$options['sql'] = "SELECT id FROM " . $class->getTable() . "";
$options['returnArray'] = true;
return $this->getBySql($options);
}
/**
* generate a list by sql.
*
* @param $options
* $options['sql'] = the sql string. (required)
* $options['listClass'] the class of the list. (optional (required if returning an iterator))
* $options['itemClass'] the class of each item in the list. (optional (required if returning an iterator))
* $options['returnArray'] return an array instead of an iterator. (optional).
*
* @return mixed
*/
public static function getBySql(array $options) {
if (!isset($options['sql'])) {
throw new exception("options['sql'] was not set!", 500);
}
$mysqli = Connection::getDB();
$options['array'] = array();
if ($result = $mysqli->query($options['sql'])) {
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$options['array'][] = $row['id'];
}
}
if (isset($options['returnArray']) && $options['returnArray'] == true) {
return $options['array'];
}
if (!isset($options['listClass'], $options['itemClass'])) {
throw new Exception("options['listClass'] or options['itemClass'] were not set!", 500);
}
return new $options['listClass']($options);
}
public static function escapeString($string)
{
$mysqli = Connection::getDB();
return $mysqli->escape_string($string);
}
function current() {
return call_user_func($this->options['itemClass'] . "::getByID", parent::current());
}
function count()
{
$iterator = $this->getInnerIterator();
if ($iterator instanceof EmptyIterator) {
return 0;
}
return count($this->getInnerIterator());
}
}