* @package tao */ class tao_helpers_Context { /** * The list of current loaded modes. This array contains strings. * * @access protected * @var array */ protected static $current = []; /** * load a new current mode * * @author Cedric Alfonsi, * @param string mode * @throws InvalidArgumentException If the mode variable is not a string or is empty. */ public static function load($mode) { if (!is_string($mode)) { throw new InvalidArgumentException('Try to load an irregular mode in the context. The mode must be a string, ' . gettype($mode) . ' given.'); } elseif (empty($mode)) { throw new InvalidArgumentException('Cannot load an empty mode in the context.'); } if (!in_array($mode, self::$current)) { self::$current[] = $mode; } } /** * check if the mode in parameter is loaded in the context. * * @author Cedric Alfonsi, * @param string mode The mode you want to check it is loaded or not. * @return boolean * @throws InvalidArgumentException If the mode variable is not a string or is empty. */ public static function check($mode) { $returnValue = (bool) false; if (!is_string($mode)) { throw new InvalidArgumentException('Try to check an irregular mode. The mode must be a string, ' . gettype($mode) . ' given.'); } elseif (empty($mode)) { throw new InvalidArgumentException('Cannot check an empty mode.'); } $returnValue = in_array($mode, self::$current); return (bool) $returnValue; } /** * reset the context. * * @author Cedric Alfonsi, */ public static function reset() { self::$current = []; } /** * Unload a specific mode. * * @author Cedric Alfonsi, * @param string mode * @throws InvalidArgumentException If the mode variable has not the right type (string) or is empty. */ public static function unload($mode) { if (!is_string($mode)) { throw new InvalidArgumentException('Try to unload an irregular mode in the context. The mode must be a string, ' . gettype($mode) . ' given.'); } elseif (empty($mode)) { throw new InvalidArgumentException('Cannot unload an empty mode in the context.'); } if (in_array($mode, self::$current)) { $index = array_search($mode, self::$current); if ($index !== false) { unset(self::$current[$index]); } } } /** * Get the set of currently loaded modes. * * @author Jerome Bogaerts, * @return array An array of strings that representent current modes. */ public static function get() { return self::$current; } }