2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 // {{{ interface PlFilterCondition
23 interface PlFilterCondition
25 const COND_TRUE
= 'TRUE';
26 const COND_FALSE
= 'FALSE';
28 public function buildCondition(PlFilter
&$pf);
32 // {{{ class PFC_OneChild
33 abstract class PFC_OneChild
implements PlFilterCondition
37 public function __construct($child = null
)
39 if (!is_null($child) && ($child instanceof PlFilterCondition
)) {
40 $this->setChild($child);
44 public function setChild(PlFilterCondition
&$cond)
46 $this->child
=& $cond;
51 // {{{ class PFC_NChildren
52 abstract class PFC_NChildren
implements PlFilterCondition
54 protected $children = array();
56 public function __construct()
58 $children = func_get_args();
59 foreach ($children as &$child) {
60 if (!is_null($child) && ($child instanceof PlFilterCondition
)) {
61 $this->addChild($child);
66 public function addChild(PlFilterCondition
&$cond)
68 $this->children
[] =& $cond;
71 protected function catConds(array $cond, $op, $fallback)
73 if (count($cond) == 0) {
75 } else if (count($cond) == 1) {
78 return '(' . implode(') ' . $op . ' (', $cond) . ')';
85 class PFC_True
implements PlFilterCondition
87 public function buildCondition(PlFilter
&$uf)
89 return self
::COND_TRUE
;
94 // {{{ class PFC_False
95 class PFC_False
implements PlFilterCondition
97 public function buildCondition(PlFilter
&$uf)
99 return self
::COND_FALSE
;
105 class PFC_Not
extends PFC_OneChild
107 public function buildCondition(PlFilter
&$uf)
109 $val = $this->child
->buildCondition($uf);
110 if ($val == self
::COND_TRUE
) {
111 return self
::COND_FALSE
;
112 } else if ($val == self
::COND_FALSE
) {
113 return self
::COND_TRUE
;
115 return 'NOT (' . $val . ')';
122 class PFC_And
extends PFC_NChildren
124 public function buildCondition(PlFilter
&$uf)
126 if (empty($this->children
)) {
127 return self
::COND_FALSE
;
129 $true = self
::COND_FALSE
;
131 foreach ($this->children
as &$child) {
132 $val = $child->buildCondition($uf);
133 if ($val == self
::COND_TRUE
) {
134 $true = self
::COND_TRUE
;
135 } else if ($val == self
::COND_FALSE
) {
136 return self
::COND_FALSE
;
141 return $this->catConds($conds, 'AND', $true);
148 class PFC_Or
extends PFC_NChildren
150 public function buildCondition(PlFilter
&$uf)
152 if (empty($this->children
)) {
153 return self
::COND_TRUE
;
155 $true = self
::COND_TRUE
;
157 foreach ($this->children
as &$child) {
158 $val = $child->buildCondition($uf);
159 if ($val == self
::COND_TRUE
) {
160 return self
::COND_TRUE
;
161 } else if ($val == self
::COND_FALSE
) {
162 $true = self
::COND_FALSE
;
167 return $this->catConds($conds, 'OR', $true);