Add PlFilter, PFC, PFO
[platal.git] / classes / plfiltercondition.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 // {{{ interface PlFilterCondition
23 interface PlFilterCondition
24 {
25 const COND_TRUE = 'TRUE';
26 const COND_FALSE = 'FALSE';
27
28 public function buildCondition(PlFilter &$pf);
29 }
30 // }}}
31
32 // {{{ class PFC_OneChild
33 abstract class PFC_OneChild implements PlFilterCondition
34 {
35 protected $child;
36
37 public function __construct($child = null)
38 {
39 if (!is_null($child) && ($child instanceof PlFilterCondition)) {
40 $this->setChild($child);
41 }
42 }
43
44 public function setChild(PlFilterCondition &$cond)
45 {
46 $this->child =& $cond;
47 }
48 }
49 // }}}
50
51 // {{{ class PFC_NChildren
52 abstract class PFC_NChildren implements PlFilterCondition
53 {
54 protected $children = array();
55
56 public function __construct()
57 {
58 $children = func_get_args();
59 foreach ($children as &$child) {
60 if (!is_null($child) && ($child instanceof PlFilterCondition)) {
61 $this->addChild($child);
62 }
63 }
64 }
65
66 public function addChild(PlFilterCondition &$cond)
67 {
68 $this->children[] =& $cond;
69 }
70
71 protected function catConds(array $cond, $op, $fallback)
72 {
73 if (count($cond) == 0) {
74 return $fallback;
75 } else if (count($cond) == 1) {
76 return $cond[0];
77 } else {
78 return '(' . implode(') ' . $op . ' (', $cond) . ')';
79 }
80 }
81 }
82 // }}}
83
84 // {{{ class PFC_True
85 class PFC_True implements PlFilterCondition
86 {
87 public function buildCondition(PlFilter &$uf)
88 {
89 return self::COND_TRUE;
90 }
91 }
92 // }}}
93
94 // {{{ class PFC_False
95 class PFC_False implements PlFilterCondition
96 {
97 public function buildCondition(PlFilter &$uf)
98 {
99 return self::COND_FALSE;
100 }
101 }
102 // }}}
103
104 // {{{ class PFC_Not
105 class PFC_Not extends PFC_OneChild
106 {
107 public function buildCondition(PlFilter &$uf)
108 {
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;
114 } else {
115 return 'NOT (' . $val . ')';
116 }
117 }
118 }
119 // }}}
120
121 // {{{ class PFC_And
122 class PFC_And extends PFC_NChildren
123 {
124 public function buildCondition(PlFilter &$uf)
125 {
126 if (empty($this->children)) {
127 return self::COND_FALSE;
128 } else {
129 $true = self::COND_FALSE;
130 $conds = array();
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;
137 } else {
138 $conds[] = $val;
139 }
140 }
141 return $this->catConds($conds, 'AND', $true);
142 }
143 }
144 }
145 // }}}
146
147 // {{{ class PFC_Or
148 class PFC_Or extends PFC_NChildren
149 {
150 public function buildCondition(PlFilter &$uf)
151 {
152 if (empty($this->children)) {
153 return self::COND_TRUE;
154 } else {
155 $true = self::COND_TRUE;
156 $conds = array();
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;
163 } else {
164 $conds[] = $val;
165 }
166 }
167 return $this->catConds($conds, 'OR', $true);
168 }
169 }
170 }
171 // }}}
172 ?>