Class to manage LDAP entries case-insensitiveness

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
-redShadow-'s picture

I just wrote a class to be used to work with a LDAP entry avoiding some problems derived by the case-insensitiveness of the LDAP attributes names.
This class just takes an array of values, stores them internally with lower-cased keys but records the original case of the first time the attribute was set.
Then, attributes can be retrieved both as if it were an object ($entry->myAttr) or an array($entry['myAttr']); the case we use to ask for the attribute value is irrelevant, so $entry['MyATTR'] will return the exact same thing of $entry['myattr']. Also, we can iterate through the saved values, and the returned key will have the case of the first time it was inserted.

I'm pasting the code here hoping that this will be useful for someone..

<?php
/**
* Wrapper class for entries retrieved from LDAP.
* The main goal of this class is to quickly find attributes by name
* ignoring their case.
*/
class LDAPInterfaceEntry implements ArrayAccess, Iterator {
  private
$_attrs_original_case = array();
  private
$_entry = array();
  private
$_position = 0;
 
 
/**
   * Class constructor
   * @param $values
   */
 
function __construct($values=NULL) {
   
$this->_position = 0;
   
$this->_entry = array();
   
$this->_attrs_original_case = array();
   
$this->load($values);
  }
 
 
// --- ACT AS AN OBJECT ------------------------------------------------------
 
function __get($name)         { return $this->get($name); }
  function
__set($name, $value) { return $this->set($name, $value); }
  function
__isset($name)       { return $this->varIsset($name); }
  function
__unset($name)       { return $this->varUnset($name); }
 
 
// --- ACT AS AN ARRAY -------------------------------------------------------
 
function offsetExists($offset)      { return $this->varIsset($offset); }
  function
offsetGet($offset)         { return $this->get($offset); }
  function
offsetSet($offset, $value) { return $this->set($offset, $value); }
  function
offsetUnset($offset)       { return $this->varUnset($offset); }
 
 
// --- ITERATOR FUNCTIONS ----------------------------------------------------
 
 
function current() {
   
$keys = array_keys($this->_entry);
   
$_key = $keys[$this->_position];
    return
$this->_entry[$_key];
  }
 
  function
key() {
   
$keys = array_keys($this->_entry);
   
$_key = $keys[$this->_position];
    if (isset(
$this->_attrs_original_case[$_key])) {
      return
$this->_attrs_original_case[$_key];
    }
    else {
      return
$_key;
    }
  }
 
  function
next() {
   
$this->_position++;
  }
 
  function
rewind() {
   
$this->_position = 0;
  }
 
  function
valid() {
   
$keys = array_keys($this->_entry);
    return isset(
$keys[$this->_position]);
  }
 
 
// --- PUBLIC, COMMON METHODS ------------------------------------------------
 
  /**
   * Get a given attribute value
   * @param $name
   */
 
function get($name) {
   
$_name = strtolower($name);
    return isset(
$this->_entry[$_name]) ? $this->_entry[$_name] : NULL;
  }
 
 
/**
   * Set a given attribute value
   * @param $name
   * @param $value
   */
 
function set($name, $value) {
   
$_name = strtolower($name);
    if (
$_name !== $name && !isset($this->_attrs_original_case[$_name])) {
     
$this->_attrs_original_case[$_name] = $name;
    }
   
$this->_entry[$_name] = $value;
  }
 
 
/**
   * Unset a given attribute value
   * @param $name
   */
 
function varUnset($name) {
   
$_name = strtolower($name);
    unset(
$this->_entry[$name]);
    if (isset(
$this->_attrs_original_case[$_name])) {
      unset(
$this->_attrs_original_case[$_name]);
    }
  }
 
 
/**
   * Check if a given attribute value is set
   * @param $name
   */
 
function varIsset($name) {
   
$_name = strtolower($name);
    return isset(
$this->_entry[$name]);
  }
 
 
/**
   * Get all the values for this entry, as an array
   * @param $orig_case
   */
 
function getAll($orig_case=TRUE) {
   
$_values = array();
    foreach (
$this->_entry as $key => $val) {
     
$_key = ($orig_case && isset($this->_attrs_original_case[$key])) ? $this->_attrs_original_case[$key] : $key;
     
$_values[$_key] = $val;
    }
    return
$_values;
  }
 
 
/**
   * Load values into this class
   * @param $values
   */
 
function load($values) {
   
$this->_attrs_original_case = array();
   
$this->_entry = array();
    if (!
$values) return;
    foreach (
$values as $key => $val) {
     
$this->set($key, $val);
    }
  }
}
?>