Horde_Ldap_Filter::create PHP Method

create() public static method

The following matching rules exists: - equals: One of the attributes values is exactly $value. Please note that case sensitiviness depends on the attributes syntax configured in the server. - begins: One of the attributes values must begin with $value. - ends: One of the attributes values must end with $value. - contains: One of the attributes values must contain $value. - present | any: The attribute can contain any value but must exist. - greater: The attributes value is greater than $value. - less: The attributes value is less than $value. - greaterOrEqual: The attributes value is greater or equal than $value. - lessOrEqual: The attributes value is less or equal than $value. - approx: One of the attributes values is similar to $value. If $escape is set to true then $value will be escaped. If set to false then $value will be treaten as a raw filter value string. You should then escape it yourself using {@link Horde_Ldap_Util::escapeFilterValue()}. Examples: This will find entries that contain an attribute "sn" that ends with "foobar": $filter = Horde_Ldap_Filter::create('sn', 'ends', 'foobar'); This will find entries that contain an attribute "sn" that has any value set: $filter = Horde_Ldap_Filter::create('sn', 'any');
public static create ( string $attribute, string $match, string $value = '', boolean $escape = true ) : Horde_Ldap_Filter
$attribute string Name of the attribute the filter should apply to.
$match string Matching rule (equals, begins, ends, contains, greater, less, greaterOrEqual, lessOrEqual, approx, any).
$value string If given, then this is used as a filter value.
$escape boolean Should $value be escaped?
return Horde_Ldap_Filter
    public static function create($attribute, $match, $value = '', $escape = true)
    {
        if ($escape) {
            $array = Horde_Ldap_Util::escapeFilterValue(array($value));
            $value = $array[0];
        }
        switch (Horde_String::lower($match)) {
            case 'equals':
            case '=':
                $filter = '(' . $attribute . '=' . $value . ')';
                break;
            case 'begins':
                $filter = '(' . $attribute . '=' . $value . '*)';
                break;
            case 'ends':
                $filter = '(' . $attribute . '=*' . $value . ')';
                break;
            case 'contains':
                $filter = '(' . $attribute . '=*' . $value . '*)';
                break;
            case 'greater':
            case '>':
                $filter = '(' . $attribute . '>' . $value . ')';
                break;
            case 'less':
            case '<':
                $filter = '(' . $attribute . '<' . $value . ')';
                break;
            case 'greaterorequal':
            case '>=':
                $filter = '(' . $attribute . '>=' . $value . ')';
                break;
            case 'lessorequal':
            case '<=':
                $filter = '(' . $attribute . '<=' . $value . ')';
                break;
            case 'approx':
            case '~=':
                $filter = '(' . $attribute . '~=' . $value . ')';
                break;
            case 'any':
            case 'present':
                $filter = '(' . $attribute . '=*)';
                break;
            default:
                throw new Horde_Ldap_Exception('Matching rule "' . $match . '" unknown');
        }
        return new Horde_Ldap_Filter(array('filter' => $filter));
    }

Usage Example

Example #1
0
 /**
  * Retrieves user preferences from the backend.
  *
  * @throws Sam_Exception
  */
 public function retrieve()
 {
     $attrib = Horde_String::lower($this->_params['attribute']);
     try {
         $search = $this->_ldap->search($this->_params['basedn'], Horde_Ldap_Filter::create($this->_params['uid'], 'equals', $this->_user), array('attributes' => array($attrib)));
         $entry = $search->shiftEntry();
         if (!$entry) {
             throw new Sam_Exception(sprintf('LDAP user "%s" not found.', $this->_user));
         }
         foreach ($entry->getValue($attrib, 'all') as $attribute) {
             list($a, $v) = explode(' ', $attribute);
             $ra = $this->_mapOptionToAttribute($a);
             if (is_numeric($v)) {
                 if (strstr($v, '.')) {
                     $newoptions[$ra][] = (double) $v;
                 } else {
                     $newoptions[$ra][] = (int) $v;
                 }
             } else {
                 $newoptions[$ra][] = $v;
             }
         }
     } catch (Horde_Ldap_Exception $e) {
         throw new Sam_Exception($e);
     }
     /* Go through new options and pull single values out of their
      * arrays. */
     foreach ($newoptions as $k => $v) {
         if (count($v) > 1) {
             $this->_options[$k] = $v;
         } else {
             $this->_options[$k] = $v[0];
         }
     }
 }
All Usage Examples Of Horde_Ldap_Filter::create