nyroFwk  0.2
where.class.php
Go to the documentation of this file.
1 <?php
10 class db_where extends object implements Countable {
11 
17  protected $clauses;
18 
19  protected function afterInit() {
20  if (is_array($this->cfg->clauses))
21  $this->clauses = $this->cfg->clauses;
22  else
23  $this->clauses = array($this->cfg->clauses);
24  }
25 
31  public function getDb() {
32  return $this->cfg->db;
33  }
34 
38  public function clear() {
39  $this->clauses = array();
40  }
41 
47  public function getClauses() {
48  return $this->clauses;
49  }
50 
56  public function setClauses(array $clauses) {
57  $this->clauses = $clauses;
58  }
59 
68  public function add($prm) {
69  if (is_array($prm) && !config::initTab($prm, array(
70  'field'=>null,
71  'op'=>'=',
72  'val'=>null
73  )))
74  return;
75  $this->clauses[] = $prm;
76  }
77 
85  public function toArray() {
86  $bind = array();
87  $where = array();
88 
89  foreach($this->clauses as $c) {
90  if ($c instanceof db_where) {
91  $tmp = $c->toArray();
92  $where[] = $tmp['where'];
93  $bind = array_merge($bind, $tmp['bind']);
94  } else if ($c instanceof db_whereClause) {
95  $where[] = ''.$c;
96  } else if (is_array($c)) {
97  $where[] = $this->getDb()->quoteIdentifier($c['field']).' '.$c['op'].' ?';
98  $bind[] = is_array($c['val']) ? '('.implode(',', $c['val']).')' : $this->getDb()->quoteValue($c['val']);
99  } else {
100  // Should be a raw string
101  $where[] = $c;
102  }
103  }
104 
105  return array(
106  'bind'=>$bind,
107  'where'=>'('.implode(') '.$this->cfg->op.' (', $where).')',
108  );
109  }
110 
116  public function count() {
117  return count($this->clauses);
118  }
119 
125  public function toString() {
126  $prm = $this->toArray();
127 
128  $tmp = explode('?', $prm['where'], count($prm['bind'])+1);
129  array_splice($prm['bind'], count($tmp));
130 
131  $where = '';
132  while($tmp2 = array_shift($tmp)) {
133  $where.= $tmp2.array_shift($prm['bind']);
134  }
135  return $where;
136  }
137 
138  public function __toString() {
139  return $this->toString();
140  }
141 
142 }
add($prm)
Definition: where.class.php:68
setClauses(array $clauses)
Definition: where.class.php:56
static initTab(array &$vars, array $init)
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13