+
+ /**
+ * Filter entries using this filter or see if a filter matches
+ *
+ * @todo Currently slow and naive implementation with preg_match, could be optimized (esp. begins, ends filters etc)
+ * @todo Currently only "="-based matches (equals, begins, ends, contains, any) implemented; Implement all the stuff!
+ * @todo Implement expert code with schema checks in case $entry is connected to a directory
+ * @param array|Net_LDAP2_Entry The entry (or array with entries) to check
+ * @param array If given, the array will be appended with entries who matched the filter. Return value is true if any entry matched.
+ * @return int|Net_LDAP2_Error Returns the number of matched entries or error
+ */
+ function matches(&$entries, &$results=array()) {
+ $numOfMatches = 0;
+
+ if (!is_array($entries)) {
+ $all_entries = array(&$entries);
+ } else {
+ $all_entries = &$entries;
+ }
+
+ foreach ($all_entries as $entry) {
+ // look at the current entry and see if filter matches
+
+ $entry_matched = false;
+ // if this is not a single component, do calculate all subfilters,
+ // then assert the partial results with the given combination modifier
+ if (!$this->isLeaf()) {
+
+ // get partial results from subfilters
+ $partial_results = array();
+ foreach ($this->_subfilters as $filter) {
+ $partial_results[] = $filter->matches($entry);
+ }
+
+ // evaluate partial results using this filters combination rule
+ switch ($this->_match) {
+ case '!':
+ // result is the neagtive result of the assertion
+ $entry_matched = !$partial_results[0];
+ break;
+
+ case '&':
+ // all partial results have to be boolean-true
+ $entry_matched = !in_array(false, $partial_results);
+ break;
+
+ case '|':
+ // at least one partial result has to be true
+ $entry_matched = in_array(true, $partial_results);
+ break;
+ }
+
+ } else {
+ // Leaf filter: assert given entry
+ // [TODO]: Could be optimized to avoid preg_match especially with "ends", "begins" etc
+
+ // Translate the LDAP-match to some preg_match expression and evaluate it
+ list($attribute, $match, $assertValue) = $this->getComponents();
+ switch ($match) {
+ case '=':
+ $regexp = '/^'.str_replace('*', '.*', $assertValue).'$/i'; // not case sensitive unless specified by schema
+ $entry_matched = $entry->pregMatch($regexp, $attribute);
+ break;
+
+ // -------------------------------------
+ // [TODO]: implement <, >, <=, >= and =~
+ // -------------------------------------
+
+ default:
+ $err = PEAR::raiseError("Net_LDAP2_Filter match error: unsupported match rule '$match'!");
+ return $err;
+ }
+
+ }
+
+ // process filter matching result
+ if ($entry_matched) {
+ $numOfMatches++;
+ $results[] = $entry;
+ }
+
+ }
+
+ return $numOfMatches;
+ }
+
+
+ /**
+ * Retrieve this leaf-filters attribute, match and value component.
+ *
+ * For leaf filters, this returns array(attr, match, value).
+ * Match is be the logical operator, not the text representation,
+ * eg "=" instead of "equals". Note that some operators are really
+ * a combination of operator+value with wildcard, like
+ * "begins": That will return "=" with the value "value*"!
+ *
+ * For non-leaf filters this will drop an error.
+ *
+ * @todo $this->_match is not always available and thus not usable here; it would be great if it would set in the factory methods and constructor.
+ * @return array|Net_LDAP2_Error
+ */
+ function getComponents() {
+ if ($this->isLeaf()) {
+ $raw_filter = preg_replace('/^\(|\)$/', '', $this->_filter);
+ $parts = Net_LDAP2_Util::split_attribute_string($raw_filter, true, true);
+ if (count($parts) != 3) {
+ return PEAR::raiseError("Net_LDAP2_Filter getComponents() error: invalid filter syntax - unknown matching rule used");
+ } else {
+ return $parts;
+ }
+ } else {
+ return PEAR::raiseError('Net_LDAP2_Filter getComponents() call is invalid for non-leaf filters!');
+ }
+ }
+
+