1: <?php
2: /**
3: * The Ingo_Script_Sieve_Test_Anyof class represents an Anyof test structure.
4: *
5: * Equivalent to a logical OR of all the tests it contains.
6: *
7: * See the enclosed file LICENSE for license information (ASL). If you
8: * did not receive this file, see http://www.horde.org/licenses/apache.
9: *
10: * @author Mike Cochrane <mike@graftonhall.co.nz>
11: * @package Ingo
12: */
13: class Ingo_Script_Sieve_Test_Anyof extends Ingo_Script_Sieve_Test
14: {
15: protected $_tests = array();
16:
17: /**
18: * Constructor.
19: *
20: * @param mixed $test An Ingo_Script_Sieve_Test object or a list of
21: * Ingo_Script_Sieve_Test objects.
22: */
23: public function __construct($test = null)
24: {
25: if (is_array($test)) {
26: $this->_tests = $test;
27: } elseif (!is_null($test)) {
28: $this->_tests[] = $test;
29: }
30: }
31:
32: /**
33: * Returns a script snippet representing this rule and any sub-rules.
34: *
35: * @return string A Sieve script snippet.
36: */
37: public function toCode()
38: {
39: $testlist = '';
40: if (count($this->_tests) > 1) {
41: $testlist = '';
42: foreach ($this->_tests as $test) {
43: $testlist .= (empty($testlist)) ? '' : ', ';
44: $testlist .= trim($test->toCode());
45: }
46:
47: $code = "anyof ( $testlist )";
48: } elseif (count($this->_tests) == 1) {
49: $code = $this->_tests[0]->toCode();
50: } else {
51: return 'true';
52: }
53: return $code;
54: }
55:
56: /**
57: */
58: public function addTest($test)
59: {
60: $this->_tests[] = $test;
61: }
62:
63: /**
64: */
65: public function getTests()
66: {
67: return $this->_tests;
68: }
69:
70: /**
71: * Checks if all sub-rules are valid.
72: *
73: * @return boolean|string True if all rules are valid, an error message
74: * otherwise.
75: */
76: public function check()
77: {
78: foreach ($this->_tests as $test) {
79: $res = $test->check();
80: if ($res !== true) {
81: return $res;
82: }
83: }
84:
85: return true;
86: }
87:
88: /**
89: * Returns a list of sieve extensions required for this rule and any
90: * sub-rules.
91: *
92: * @return array A Sieve extension list.
93: */
94: public function requires()
95: {
96: $requires = array();
97:
98: foreach ($this->_tests as $test) {
99: $requires = array_merge($requires, $test->requires());
100: }
101:
102: return $requires;
103: }
104:
105: }
106: