You are here

protected function SimpleLdapServerSchema::parseSchemaValue in Simple LDAP 8

Parse a schema value into a usable array.

@link http://pear.php.net/package/Net_LDAP2/

@license http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3

Parameters

string $value:

Return value

array An array of meta-data about the schema attribute passed in. Some keys that might be in the array:

  • oid - will always be there
  • name
  • syntax
  • max_length
  • aliases
1 call to SimpleLdapServerSchema::parseSchemaValue()
SimpleLdapServerSchema::loadSchema in src/SimpleLdapServerSchema.php
Load the schema.

File

src/SimpleLdapServerSchema.php, line 178
Contains \Drupal\simple_ldap\SimpleLdapServerSchema

Class

SimpleLdapServerSchema

Namespace

Drupal\simple_ldap

Code

protected function parseSchemaValue($value) {

  // Tokens that have no associated value.
  $novalue = array(
    'single-value',
    'obsolete',
    'collective',
    'no-user-modification',
    'abstract',
    'structural',
    'auxiliary',
  );

  // Tokens that can have multiple values.
  $multivalue = array(
    'must',
    'may',
    'sup',
  );

  // Initialization.
  $schema_entry = array(
    'aliases' => array(),
  );

  // Get an array of tokens.
  $tokens = $this
    ->tokenize($value);

  // Remove left paren.
  if ($tokens[0] == '(') {
    array_shift($tokens);
  }

  // Remove right paren.
  if ($tokens[count($tokens) - 1] == ')') {
    array_pop($tokens);
  }

  // The first token is the OID.
  $schema_entry['oid'] = array_shift($tokens);

  // Loop through the tokens until there are none left.
  while (count($tokens) > 0) {
    $token = mb_strtolower(array_shift($tokens));
    if (in_array($token, $novalue)) {

      // Single value token.
      $schema_entry[$token] = 1;
    }
    else {

      // This one follows a string or a list if it is multivalued.
      if (($schema_entry[$token] = array_shift($tokens)) == '(') {

        // This creates the list of values and cycles through the tokens until
        // the end of the list is reached ')'.
        $schema_entry[$token] = array();
        while ($tmp = array_shift($tokens)) {
          if ($tmp == ')') {
            break;
          }
          if ($tmp != '$') {
            array_push($schema_entry[$token], $tmp);
          }
        }
      }

      // Create an array if the value should be multivalued but was not.
      if (in_array($token, $multivalue) && !is_array($schema_entry[$token])) {
        $schema_entry[$token] = array(
          $schema_entry[$token],
        );
      }
    }
  }

  // Get the max length from syntax.
  if (array_key_exists('syntax', $schema_entry)) {
    if (preg_match('/{(\\d+)}/', $schema_entry['syntax'], $matches)) {
      $schema_entry['max_length'] = $matches[1];
    }
  }

  // Force a name.
  if (empty($schema_entry['name'])) {
    $schema_entry['name'] = $schema_entry['oid'];
  }

  // Make one name the default and put the others into aliases.
  if (is_array($schema_entry['name'])) {
    $aliases = $schema_entry['name'];
    $schema_entry['name'] = array_shift($aliases);
    $schema_entry['aliases'] = $aliases;
  }
  return $schema_entry;
}