Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_Context | |
0.00% |
0 / 30 |
|
0.00% |
0 / 5 |
210 | |
0.00% |
0 / 1 |
| load | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| check | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| reset | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| unload | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| get | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * The context class enables you to define some context to the application |
| 27 | * and to check statically which context/mode is actually loaded. |
| 28 | * |
| 29 | * @author Cedric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 30 | * @package tao |
| 31 | |
| 32 | */ |
| 33 | class tao_helpers_Context |
| 34 | { |
| 35 | /** |
| 36 | * The list of current loaded modes. This array contains strings. |
| 37 | * |
| 38 | * @access protected |
| 39 | * @var array |
| 40 | */ |
| 41 | protected static $current = []; |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * load a new current mode |
| 46 | * |
| 47 | * @author Cedric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 48 | * @param string mode |
| 49 | * @throws InvalidArgumentException If the mode variable is not a string or is empty. |
| 50 | */ |
| 51 | public static function load($mode) |
| 52 | { |
| 53 | if (!is_string($mode)) { |
| 54 | throw new InvalidArgumentException( |
| 55 | 'Try to load an irregular mode in the context. The mode must be a string, ' . gettype($mode) . ' given.' |
| 56 | ); |
| 57 | } elseif (empty($mode)) { |
| 58 | throw new InvalidArgumentException('Cannot load an empty mode in the context.'); |
| 59 | } |
| 60 | |
| 61 | if (!in_array($mode, self::$current)) { |
| 62 | self::$current[] = $mode; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * check if the mode in parameter is loaded in the context. |
| 68 | * |
| 69 | * @author Cedric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 70 | * @param string mode The mode you want to check it is loaded or not. |
| 71 | * @return boolean |
| 72 | * @throws InvalidArgumentException If the mode variable is not a string or is empty. |
| 73 | */ |
| 74 | public static function check($mode) |
| 75 | { |
| 76 | $returnValue = (bool) false; |
| 77 | |
| 78 | if (!is_string($mode)) { |
| 79 | throw new InvalidArgumentException( |
| 80 | 'Try to check an irregular mode. The mode must be a string, ' . gettype($mode) . ' given.' |
| 81 | ); |
| 82 | } elseif (empty($mode)) { |
| 83 | throw new InvalidArgumentException('Cannot check an empty mode.'); |
| 84 | } |
| 85 | |
| 86 | $returnValue = in_array($mode, self::$current); |
| 87 | |
| 88 | return (bool) $returnValue; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * reset the context. |
| 93 | * |
| 94 | * @author Cedric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 95 | */ |
| 96 | public static function reset() |
| 97 | { |
| 98 | self::$current = []; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Unload a specific mode. |
| 103 | * |
| 104 | * @author Cedric Alfonsi, <cedric.alfonsi@tudor.lu> |
| 105 | * @param string mode |
| 106 | * @throws InvalidArgumentException If the mode variable has not the right type (string) or is empty. |
| 107 | */ |
| 108 | public static function unload($mode) |
| 109 | { |
| 110 | |
| 111 | if (!is_string($mode)) { |
| 112 | throw new InvalidArgumentException( |
| 113 | 'Try to unload an irregular mode in the context. The mode must be a string, ' . gettype($mode) |
| 114 | . ' given.' |
| 115 | ); |
| 116 | } elseif (empty($mode)) { |
| 117 | throw new InvalidArgumentException('Cannot unload an empty mode in the context.'); |
| 118 | } |
| 119 | |
| 120 | if (in_array($mode, self::$current)) { |
| 121 | $index = array_search($mode, self::$current); |
| 122 | if ($index !== false) { |
| 123 | unset(self::$current[$index]); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Get the set of currently loaded modes. |
| 130 | * |
| 131 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 132 | * @return array An array of strings that representent current modes. |
| 133 | */ |
| 134 | public static function get() |
| 135 | { |
| 136 | return self::$current; |
| 137 | } |
| 138 | } |