|
|
1.1 misho 1: --TEST--
2: sqlite-spl: Iteration
3: --SKIPIF--
4: <?php # vim:ft=php
5: if (!extension_loaded("sqlite")) print "skip";
6: if (!extension_loaded("spl")) print "skip SPL is not present";
7: ?>
8: --FILE--
9: <?php
10: include "blankdb_oo.inc";
11:
12: $db->query("CREATE TABLE menu(id_l int PRIMARY KEY, id_r int UNIQUE, key VARCHAR(10))");
13: $db->query("INSERT INTO menu VALUES( 1, 12, 'A')");
14: $db->query("INSERT INTO menu VALUES( 2, 9, 'B')");
15: $db->query("INSERT INTO menu VALUES(10, 11, 'F')");
16: $db->query("INSERT INTO menu VALUES( 3, 6, 'C')");
17: $db->query("INSERT INTO menu VALUES( 7, 8, 'E')");
18: $db->query("INSERT INTO menu VALUES( 4, 5, 'D')");
19:
20: class SqliteNestedsetElement
21: {
22: protected $id_l;
23: protected $id_r;
24: protected $key;
25:
26: function __construct($db)
27: {
28: $this->db = $db;
29: }
30:
31: function getLeft()
32: {
33: return $this->id_l;
34: }
35:
36: function getRight()
37: {
38: return $this->id_r;
39: }
40:
41: function __toString()
42: {
43: return $this->key;
44: }
45:
46: function key()
47: {
48: return $this->key;
49: }
50: }
51:
52: class SqliteNestedset implements RecursiveIterator
53: {
54: protected $id;
55: protected $id_l;
56: protected $id_r;
57: protected $entry;
58:
59: function __construct($db, $id_l = 1)
60: {
61: $this->db = $db;
62: $this->id_l = $id_l;
63: $this->id_r = $this->db->singleQuery('SELECT id_r FROM menu WHERE id_l='.$id_l, 1);
64: $this->id = $id_l;
65: }
66:
67: function rewind()
68: {
69: $this->id = $this->id_l;
70: $this->fetch();
71: }
72:
73: function valid()
74: {
75: return is_object($this->entry);
76: }
77:
78: function current()
79: {
80: return $this->entry->__toString();
81: }
82:
83: function key()
84: {
85: return $this->entry->key();;
86: }
87:
88: function next()
89: {
90: $this->id = $this->entry->getRight() + 1;
91: $this->fetch();
92: }
93:
94: protected function fetch()
95: {
96: $res = $this->db->unbufferedQuery('SELECT * FROM menu WHERE id_l='.$this->id);
97: $this->entry = $res->fetchObject('SqliteNestedsetElement', array(&$this->db));
98: unset($res);
99: }
100:
101: function hasChildren()
102: {
103: return $this->entry->getLeft() + 1 < $this->entry->getRight();
104: }
105:
106: function getChildren()
107: {
108: return new SqliteNestedset($this->db, $this->entry->getLeft() + 1, $this->entry->getRight() - 1);
109: }
110: }
111:
112: $menu_iterator = new RecursiveIteratorIterator(new SqliteNestedset($db), RecursiveIteratorIterator::SELF_FIRST);
113: foreach($menu_iterator as $entry) {
114: echo $menu_iterator->getDepth() . $entry . "\n";
115: }
116: ?>
117: ===DONE===
118: --EXPECT--
119: 0A
120: 1B
121: 2C
122: 3D
123: 2E
124: 1F
125: ===DONE===