You are here

context.module in Context 5

File

context.module
View source
<?php

define('CONTEXT_GET', 0);
define('CONTEXT_SET', 1);
define('CONTEXT_ISSET', 2);
define('CONTEXT_CLEAR', 3);

/**
 * Master context function. Avoid calling this directly -- use one of the helper functions below.
 *
 * @param $op
 *   The operation to perform - handled by the context helper functions. Use them.
 * @param $namespace
 *   A string to be used as the namespace for the context information.
 * @param $attribute
 *   Usually a string to be used as a key to set/retrieve context information. An array can
 *   also be used when setting context to establish an entire context namespace at once.  
 *   (At some point objects may also be accepted, but currently functionaliy isn't complete.)
 * @param $value
 *   A value to set for the provided key. If omitted the value will be set to true.
 *
 * @return
 *   Either the requested value, or false if the operation fails.
 */
function context_context($op = CONTEXT_GET, $namespace = null, $attribute = null, $value = null) {
  static $context;
  $context = !$context ? array() : $context;
  switch ($op) {
    case CONTEXT_GET:

      // return entire context
      if (!$namespace) {
        return $context;
      }
      else {
        if (!$attribute && isset($context[(string) $namespace])) {
          return $context[(string) $namespace];
        }
        else {
          if (is_array($context[(string) $namespace]) && isset($context[(string) $namespace][(string) $attribute])) {
            return $context[(string) $namespace][(string) $attribute];
          }
        }
      }
      break;
    case CONTEXT_SET:

      // bail if invalid space is specified or context is already set
      if (is_string($namespace) || is_int($namespace)) {

        // initialize namespace if no key is specified
        if (!$attribute) {
          $context[(string) $namespace] = array();
          return true;
        }

        // set to true if key is a usable identifier. otherwise, allow a key or object to be inserted
        if (!$value) {
          if (is_string($attribute) || is_int($attribute)) {
            $context[(string) $namespace][(string) $attribute] = true;
            return true;
          }
          elseif (is_array($attribute) || is_object($attribute)) {
            $context[(string) $namespace] = $attribute;
            return true;
          }
        }

        // set value if key is valid
        if ((is_string($attribute) || is_int($attribute)) && $value) {
          $context[$namespace][$attribute] = $value;
          return true;
        }
      }
      break;
    case CONTEXT_ISSET:

      // return entire context
      if (!$namespace) {
        return false;
      }
      if (!$attribute) {

        // return entire space if set
        return isset($context[$namespace]);
      }

      // return val of key from space
      return isset($context[$namespace][$attribute]);
    case CONTEXT_CLEAR:
      $context = array();
      return true;
  }
  return false;
}

/**
 * Sets a context by namespace + attribute.
 */
function context_set($namespace, $attribute = null, $value = null) {
  return context_context(CONTEXT_SET, $namespace, $attribute, $value);
}

/**
 * Retrieves a context by namespace + (optional) attribute.
 */
function context_get($namespace = null, $attribute = null) {
  return context_context(CONTEXT_GET, $namespace, $attribute, null);
}

/**
 * Returns a boolean for whether a context namespace + attribute have been set.
 */
function context_isset($namespace = null, $attribute = null) {
  return context_context(CONTEXT_ISSET, $namespace, $attribute, null);
}

/**
 * Deprecated context_exists() function. Retained for backwards
 * compatibility -- please use context_isset() instead.
 */
function context_exists($namespace = null, $attribute = null) {
  return context_context(CONTEXT_ISSET, $namespace, $attribute, null);
}

/**
 * Clears static context array() -- meant only for testing
 */
function context_clear() {
  return context_context(CONTEXT_CLEAR);
}

Functions

Namesort descending Description
context_clear Clears static context array() -- meant only for testing
context_context Master context function. Avoid calling this directly -- use one of the helper functions below.
context_exists Deprecated context_exists() function. Retained for backwards compatibility -- please use context_isset() instead.
context_get Retrieves a context by namespace + (optional) attribute.
context_isset Returns a boolean for whether a context namespace + attribute have been set.
context_set Sets a context by namespace + attribute.

Constants