Horde_Ldap_Filter::combine PHP Method

combine() public static method

Example: $filter = Horde_Ldap_Filter::combine('or', array($filter1, $filter2)); If the array contains filter strings instead of filter objects, they will be parsed.
public static combine ( string $operator, array | Horde_Ldap_Filter | string $filters ) : Horde_Ldap_Filter
$operator string The logical operator, either "and", "or", "not" or the logical equivalents "&", "|", "!".
$filters array | Horde_Ldap_Filter | string Array with Horde_Ldap_Filter objects and/or strings or a single filter when using the "not" operator.
return Horde_Ldap_Filter
    public static function combine($operator, $filters)
    {
        // Substitute named operators with logical operators.
        switch ($operator) {
            case 'and':
                $operator = '&';
                break;
            case 'or':
                $operator = '|';
                break;
            case 'not':
                $operator = '!';
                break;
        }
        // Tests for sane operation.
        switch ($operator) {
            case '!':
                // Not-combination, here we only accept one filter object or filter
                // string.
                if ($filters instanceof Horde_Ldap_Filter) {
                    $filters = array($filters);
                    // force array
                } elseif (is_string($filters)) {
                    $filters = array(self::parse($filters));
                } elseif (is_array($filters)) {
                    throw new Horde_Ldap_Exception('Operator is "not" but $filter is an array');
                } else {
                    throw new Horde_Ldap_Exception('Operator is "not" but $filter is not a valid Horde_Ldap_Filter nor a filter string');
                }
                break;
            case '&':
            case '|':
                if (!is_array($filters) || count($filters) < 2) {
                    throw new Horde_Ldap_Exception('Parameter $filters is not an array or contains less than two Horde_Ldap_Filter objects');
                }
                break;
            default:
                throw new Horde_Ldap_Exception('Logical operator is unknown');
        }
        foreach ($filters as $key => $testfilter) {
            // Check for errors.
            if (is_string($testfilter)) {
                // String found, try to parse into an filter object.
                $filters[$key] = self::parse($testfilter);
            } elseif (!$testfilter instanceof Horde_Ldap_Filter) {
                throw new Horde_Ldap_Exception('Invalid object passed in array $filters!');
            }
        }
        return new Horde_Ldap_Filter(array('filters' => $filters, 'operator' => $operator));
    }

Usage Example

Example #1
0
 /**
  * Tries to find a DN for a given kolab mail address.
  *
  * @param string $mail  The mail address to search for.
  *
  * @return string  The corresponding dn or false.
  * @throws Horde_Group_Exception
  */
 protected function _dnForMail($mail)
 {
     try {
         $filter = Horde_Ldap_Filter::combine('and', array(Horde_Ldap_Filter::create('objectclass', 'equals', 'kolabInetOrgPerson'), Horde_Ldap_Filter::create('mail', 'equals', $mail)));
         $search = $this->_ldap->search($this->_params['basedn'], $filter, array('dn'));
         if ($search->count()) {
             return $search->shiftEntry()->dn();
         }
     } catch (Horde_Ldap_Exception $e) {
         throw new Horde_Group_Exception($e);
     }
     throw new Horde_Group_Exception(sprintf('Error searching for user with the email address "%s"', $mail));
 }
All Usage Examples Of Horde_Ldap_Filter::combine