You are here

function context_context in Context 5

Same name and namespace in other branches
  1. 6.3 context.module \context_context()
  2. 6 context.module \context_context()
  3. 6.2 context.module \context_context()
  4. 7.3 context.module \context_context()

Master context function. Avoid calling this directly -- use one of the helper functions below.

Parameters

$op: The operation to perform - handled by the context helper functions. Use them.

$namespace: A string to be used as the namespace for the context information.

$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.)

$value: A value to set for the provided key. If omitted the value will be set to true.

Return value

Either the requested value, or false if the operation fails.

5 calls to context_context()
context_clear in ./context.module
Clears static context array() -- meant only for testing
context_exists in ./context.module
Deprecated context_exists() function. Retained for backwards compatibility -- please use context_isset() instead.
context_get in ./context.module
Retrieves a context by namespace + (optional) attribute.
context_isset in ./context.module
Returns a boolean for whether a context namespace + attribute have been set.
context_set in ./context.module
Sets a context by namespace + attribute.

File

./context.module, line 25

Code

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;
}