1: <?php
2: /**
3: * The Ingo_Script_Sieve_If:: class represents a Sieve If Statement.
4: *
5: * See the enclosed file LICENSE for license information (ASL). If you
6: * did not receive this file, see http://www.horde.org/licenses/apache.
7: *
8: * @author Mike Cochrane <mike@graftonhall.co.nz>
9: * @package Ingo
10: */
11: class Ingo_Script_Sieve_If
12: {
13: /**
14: * The Ingo_Script_Sieve_Test object for the if test.
15: *
16: * @var Ingo_Script_Sieve_Test
17: */
18: protected $_test;
19:
20: /**
21: * A list of Ingo_Script_Sieve_Action objects that go into the if clause.
22: *
23: * @var array
24: */
25: protected $_actions = array();
26:
27: /**
28: * A list of Ingo_Script_Sieve_Elseif objects that create optional elsif
29: * clauses.
30: *
31: * @var array
32: */
33: protected $_elsifs = array();
34:
35: /**
36: * A Ingo_Script_Sieve_Else object that creates an optional else clause.
37: *
38: * @var Ingo_Script_Sieve_Else
39: */
40: protected $_else;
41:
42: /**
43: * Constructor.
44: *
45: * @param Ingo_Script_Sieve_Test $test A Ingo_Script_Sieve_Test object.
46: */
47: public function __construct($test = null)
48: {
49: $this->_test = is_null($test)
50: ? new Ingo_Script_Sieve_Test_False()
51: : $test;
52:
53: $this->_actions[] = new Ingo_Script_Sieve_Action_Keep();
54: $this->_else = new Ingo_Script_Sieve_Else();
55: }
56:
57: /**
58: */
59: public function getTest()
60: {
61: return $this->_test;
62: }
63:
64: /**
65: */
66: public function setTest($test)
67: {
68: $this->_test = $test;
69: }
70:
71: /**
72: */
73: public function getActions()
74: {
75: return $this->_actions;
76: }
77:
78: /**
79: */
80: public function setActions($actions)
81: {
82: $this->_actions = $actions;
83: }
84:
85: /**
86: */
87: public function getElsifs()
88: {
89: return $this->_elsifs;
90: }
91:
92: /**
93: */
94: public function setElsifs($elsifs)
95: {
96: $this->_elsifs = $elsifs;
97: }
98:
99: /**
100: */
101: public function addElsif($elsif)
102: {
103: $this->_elsifs[] = $elsif;
104: }
105:
106: /**
107: */
108: public function getElse()
109: {
110: return $this->_else;
111: }
112:
113: /**
114: */
115: public function setElse($else)
116: {
117: $this->_else = $else;
118: }
119:
120: /**
121: * Returns a script snippet representing this rule and any sub-rules.
122: *
123: * @return string A Sieve script snippet.
124: */
125: public function toCode()
126: {
127: $code = 'if ' . $this->_test->toCode() . " { \n";
128: foreach ($this->_actions as $action) {
129: $code .= ' ' . $action->toCode() . "\n";
130: }
131: $code .= "} ";
132:
133: foreach ($this->_elsifs as $elsif) {
134: $code .= $elsif->toCode();
135: }
136:
137: $code .= $this->_else->toCode();
138:
139: return $code . "\n";
140: }
141:
142: /**
143: * Checks if all sub-rules are valid.
144: *
145: * @return boolean|string True if all rules are valid, an error message
146: * otherwise.
147: */
148: public function check()
149: {
150: $res = $this->_test->check();
151: if ($res !== true) {
152: return $res;
153: }
154:
155: foreach ($this->_elsifs as $elsif) {
156: $res = $elsif->check();
157: if ($res !== true) {
158: return $res;
159: }
160: }
161:
162: $res = $this->_else->check();
163: if ($res !== true) {
164: return $res;
165: }
166:
167: foreach ($this->_actions as $action) {
168: $res = $action->check();
169: if ($res !== true) {
170: return $res;
171: }
172: }
173:
174: return true;
175: }
176:
177: /**
178: * Returns a list of sieve extensions required for this rule and any
179: * sub-rules.
180: *
181: * @return array A Sieve extension list.
182: */
183: public function requires()
184: {
185: $requires = array();
186:
187: foreach ($this->_actions as $action) {
188: $requires = array_merge($requires, $action->requires());
189: }
190:
191: foreach ($this->_elsifs as $elsif) {
192: $requires = array_merge($requires, $elsif->requires());
193: }
194:
195: return array_merge($requires, $this->_test->requires(), $this->_else->requires());
196: }
197:
198: }
199: