Horde_Ldap::buildClause PHP Метод

buildClause() публичный статический Метод

Builds an LDAP search filter fragment.
public static buildClause ( string $lhs, string $op, string $rhs, array $params = [] ) : string
$lhs string The attribute to test.
$op string The operator.
$rhs string The comparison value.
$params array Any additional parameters for the operator.
Результат string The LDAP search fragment.
    public static function buildClause($lhs, $op, $rhs, $params = array())
    {
        switch ($op) {
            case 'LIKE':
                if (empty($rhs)) {
                    return '(' . $lhs . '=*)';
                }
                if (!empty($params['begin'])) {
                    return sprintf('(|(%s=%s*)(%s=* %s*))', $lhs, self::quote($rhs), $lhs, self::quote($rhs));
                }
                if (!empty($params['approximate'])) {
                    return sprintf('(%s~=%s)', $lhs, self::quote($rhs));
                }
                return sprintf('(%s=*%s*)', $lhs, self::quote($rhs));
            default:
                return sprintf('(%s%s%s)', $lhs, $op, self::quote($rhs));
        }
    }

Usage Example

Пример #1
0
 /**
  * Build a piece of a search query.
  *
  * @param array  $criteria  The array of criteria.
  *
  * @return string  An LDAP query fragment.
  */
 protected function _buildSearchQuery(array $criteria)
 {
     $clause = '';
     foreach ($criteria as $key => $vals) {
         if (!empty($vals['OR']) || $key === 'OR') {
             $clause .= '(|' . $this->_buildSearchQuery($vals) . ')';
         } elseif (!empty($vals['AND'])) {
             $clause .= '(&' . $this->_buildSearchQuery($vals) . ')';
         } else {
             if (isset($vals['field'])) {
                 $rhs = Horde_String::convertCharset($vals['test'], 'UTF-8', $this->_params['charset']);
                 $clause .= Horde_Ldap::buildClause($vals['field'], $vals['op'], $rhs, array('begin' => !empty($vals['begin'])));
             } else {
                 foreach ($vals as $test) {
                     if (!empty($test['OR'])) {
                         $clause .= '(|' . $this->_buildSearchQuery($test) . ')';
                     } elseif (!empty($test['AND'])) {
                         $clause .= '(&' . $this->_buildSearchQuery($test) . ')';
                     } else {
                         $rhs = Horde_String::convertCharset($test['test'], 'UTF-8', $this->_params['charset']);
                         $clause .= Horde_Ldap::buildClause($test['field'], $test['op'], $rhs, array('begin' => !empty($vals['begin'])));
                     }
                 }
             }
         }
     }
     return $clause;
 }