forked from nigelgbanks/objective_forms
-
Notifications
You must be signed in to change notification settings - Fork 20
/
FormStorage.inc
executable file
·104 lines (94 loc) · 2.18 KB
/
FormStorage.inc
1
2
3
4
5
6
7
8
9
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* @file
* Defines a class that is used to persist data for Drupal Forms across
* requests.
*/
/**
* Stores data in the $form_state['storage'] for use in building/rendering the
* Form.
*/
class FormStorage {
/**
* The root element where we store all the required info.
*/
const STORAGE_ROOT = 'objective_form_storage';
/**
* A reference to $form_state['storage'][STORAGE_ROOT], this is where all
* persistant data is kept.
*
* @var array
*/
protected $storage;
/**
* Creates the FormStorage Singleton.
*
* @param array $form_state
* The Drupal Form State.
*/
public function __construct(array &$form_state) {
$this->initializeFormState($form_state);
$this->storage = &$form_state['storage'][self::STORAGE_ROOT];
}
/**
* Creates the storage slot in the Drupal form state.
*
* @param array $form_state
* The Drupal Form State.
*/
protected function initializeFormState(array &$form_state) {
if (empty($form_state['storage'])) {
$form_state['storage'] = array();
}
if (empty($form_state['storage'][self::STORAGE_ROOT])) {
$form_state['storage'][self::STORAGE_ROOT] = array();
}
}
/**
* Checks storage for the existance of a variable.
*
* @param string $name
* The stored variables name.
*
* @return bool
* TRUE if the variable exists in storage, FALSE otherwise.
*/
public function __isset($name) {
return isset($this->storage[$name]);
}
/**
* Removes a variable from storage.
*
* @param string $name
* The stored variables name.
*/
public function __unset($name) {
unset($this->storage[$name]);
}
/**
* Get a value from storage.
*
* @param mixed $name
* The stored variables name.
*
* @return mixed
* The stored variables value.
*/
public function __get($name) {
if (isset($this->storage[$name])) {
return $this->storage[$name];
}
return NULL;
}
/**
* Store a value.
*
* @param mixed $name
* The stored variables name.
* @param mixed $value
* The stored variables value.
*/
public function __set($name, $value) {
$this->storage[$name] = $value;
}
}