1: <?php
2: /**
3: * @author James Pepin <james@jamespepin.com>
4: * @category Horde
5: * @license http://www.horde.org/licenses/bsd BSD
6: * @package Log
7: * @subpackage Filters
8: */
9:
10: /**
11: * Filters log events using defined constraints on one or more fields of the
12: * $event array.
13: *
14: * @author James Pepin <james@jamespepin.com>
15: * @category Horde
16: * @license http://www.horde.org/licenses/bsd BSD
17: * @package Log
18: * @subpackage Filters
19: *
20: * @todo Implement constraint objects for the different types of filtering ie
21: * regex,required,type..etc.. so we can add different constaints ad infinitum.
22: */
23: class Horde_Log_Filter_Constraint implements Horde_Log_Filter
24: {
25: /**
26: * Constraint list.
27: *
28: * @var array
29: */
30: protected $_constraints = array();
31:
32: /**
33: * Default constraint coupler.
34: *
35: * @var Horde_Constraint_Coupler
36: * @default Horde_Constraint_And
37: */
38: protected $_coupler;
39:
40: /**
41: * Constructor
42: *
43: * @param Horde_Constraint_Coupler $coupler The default kind of
44: * constraint to use to couple
45: * multiple constraints.
46: * Defaults to And.
47: */
48: public function __construct(Horde_Constraint_Coupler $coupler = null)
49: {
50: $this->_coupler = is_null($coupler)
51: ? new Horde_Constraint_And()
52: : $coupler;
53: }
54:
55: /**
56: * Add a constraint to the filter
57: *
58: * @param string $field The field to apply the constraint
59: * to.
60: * @param Horde_Constraint $constraint The constraint to apply.
61: *
62: * @return Horde_Log_Filter_Constraint A reference to $this to allow
63: * method chaining.
64: */
65: public function addConstraint($field, Horde_Constraint $constraint)
66: {
67: if (!isset($this->_constraints[$field])) {
68: $this->_constraints[$field] = clone($this->_coupler);
69: }
70: $this->_constraints[$field]->addConstraint($constraint);
71:
72: return $this;
73: }
74:
75: /**
76: * Add a regular expression to filter by
77: *
78: * Takes a field name and a regex, if the regex does not match then the
79: * event is filtered.
80: *
81: * @param string $field The name of the field that should be part of the
82: * event.
83: * @param string $regex The regular expression to filter by.
84: * @return Horde_Log_Filter_Constraint A reference to $this to allow
85: * method chaining.
86: */
87: public function addRegex($field, $regex)
88: {
89: return $this->addConstraint($field, new Horde_Constraint_PregMatch($regex));
90: }
91:
92: /**
93: * Add a required field to the filter
94: *
95: * If the field does not exist on the event, then it is filtered.
96: *
97: * @param string $field The name of the field that should be part of the
98: * event.
99: *
100: * @return Horde_Log_Filter_Constraint A reference to $this to allow
101: * method chaining.
102: */
103: public function addRequiredField($field)
104: {
105: return $this->addConstraint($field, new Horde_Constraint_Not(new Horde_Constraint_Null()));
106: }
107:
108: /**
109: * Adds all arguments passed as required fields
110: *
111: * @return Horde_Log_Filter_Constraint A reference to $this to allow
112: * method chaining.
113: */
114: public function addRequiredFields()
115: {
116: foreach (func_get_args() as $f) {
117: $this->addRequiredField($f);
118: }
119:
120: return $this;
121: }
122:
123: /**
124: * Returns Horde_Log_Filter::ACCEPT to accept the message,
125: * Horde_Log_Filter::IGNORE to ignore it.
126: *
127: * @param array $event Log event.
128: *
129: * @return boolean accepted?
130: */
131: public function accept($event)
132: {
133: foreach ($this->_constraints as $field => $constraint) {
134: $value = isset($event[$field]) ? $event[$field] : null;
135: if (!$constraint->evaluate($value)) {
136: return Horde_Log_Filter::IGNORE;
137: }
138: }
139:
140: return Horde_Log_Filter::ACCEPT;
141: }
142:
143: }
144: