- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
function &_static($name = null, $value = null, $unsetVal = false) {
static $data = array();
$null = null;
if (is_array($name)) {
$data = $name;
return $null;
}
$state = 0;
$args = array($name, $value, $unsetVal);
while (count($args)) {
$arg = array_shift($args);
switch ($state) {
case 0:
if ($arg === null) {
$state = 8;
}
elseif ($arg !== null) {
$state = 1;
}
else {
throw new \InvalidArgumentException();
}
break;
case 1:
if ($arg === null) {
$state = 5;
}
elseif ($arg !== null) {
$state = 2;
}
else {
throw new \InvalidArgumentException();
}
break;
case 2:
if ($arg === true) {
$data[$name] = $value; // $name, $value, true - overwrite saved value and return reference on it.
return $data[$name];
}
elseif ($arg === false) {
if (!array_key_exists($name, $data)) {
$data[$name] = $value;
}
return $data[$name]; // $name, $value, false - save value if not exist, return reference on stored value.
}
else {
throw new \InvalidArgumentException();
}
case 5:
if ($arg === false) {
if (!array_key_exists($name, $data)) {
$data[$name] = $value; // $name, null, false, - initialize var if not exist, return reference on stored value.
}
return $data[$name];
}
elseif ($arg === true) {
$data[$name] = $value; // $name, $value, true - overwrite saved value and return reference on it.
return $data[$name]; // $name, null, true - overwrite saved value and return reference on it.
}
else {
throw new \InvalidArgumentException();
}
break;
case 8:
if ($arg === null) {
continue;
}
if ($arg === true) {
$data = array(); // null, null, true - unset all
break 2;
}
elseif ($arg === false) {
return $data; // null, null, false - return all
}
else {
throw new \InvalidArgumentException();
}
default:
throw new \RuntimeException('Unknown state.');
}
}
return $null;
}