forked from nigelgbanks/objective_forms
-
Notifications
You must be signed in to change notification settings - Fork 20
/
FormProperty.inc
123 lines (115 loc) · 3.37 KB
/
FormProperty.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* @file
* Converts form properties into objects.
*/
/**
* A collection of static functions to help with processing for properties.
*/
class FormProperty {
/**
* Asks all the installed modules if they define any FormProperty classes.
*
* @staticvar array $cache
* For each request $cache the results.
*
* @return array
* An array of information required to instantiate FormProperty classes
* where the names of the form properties are the keys in the array.
*/
public static function GetRegisteredFormPropertiesTypes() {
static $cache;
if (empty($cache)) {
$cache = array();
foreach (module_implements('objectify_properties') as $module) {
$properties = module_invoke($module, 'objectify_properties');
if (isset($properties) && is_array($properties)) {
$cache = array_merge_recursive($cache, $properties);
}
}
}
return $cache;
}
/**
* Checks if a given form property has a registered class.
*
* @param string $name
* Name of the FormProperty we are looking for.
*
* @return bool
* TRUE if there is a registered class for the given form property.
*/
public static function IsRegisteredFormProperty($name) {
$properties = self::GetRegisteredFormPropertiesTypes();
return isset($properties[$name]);
}
/**
* Gets required information for loading and creating a given FormProperty.
*
* @param string $name
* Name of the FormProperty we are looking for.
*
* @return array
* The required information for loading and creating a given FormProperty if
* defined for the property $name.
*/
public static function GetProperty($name) {
if (self::IsRegisteredFormProperty($name)) {
$properties = self::GetRegisteredFormPropertiesTypes();
return $properties[$name];
}
return NULL;
}
/**
* Loads the file where the FormProperty class exists.
*
* If the FormProperty type is not defined it does nothing.
*
* @param string $name
* Name of the FormProperty to create.
*/
public static function LoadFile($name) {
$property = self::GetProperty($name);
if (isset($property)) {
module_load_include($property['type'], $property['module'], $property['name']);
}
}
/**
* Creates a FormProperty object and initializes it with the provided $value.
*
* @param string $name
* Name of the FormProperty to create.
* @param mixed $value
* Value to initialize the generated FormProperty object with.
*
* @return object
* The class that repersents the given form property.
*/
public static function Create($name, $value) {
$property = self::GetProperty($name);
if (isset($property)) {
$class = $property['class'];
return new $class($value);
}
return NULL;
}
/**
* Creates a FormProperty Object if one is defined by $name.
*
* @param string $name
* Name of the FormProperty to create.
* @param mixed $value
* The value of the form property.
*
* @return mixed
* If a FormProperty class is defined for $name, then a FormProperty object
* is returned, otherwise the parameter $value is returned.
*/
public static function Expand($name, $value) {
if (self::IsRegisteredFormProperty($name)) {
self::LoadFile($name);
return self::Create($name, $value);
}
return $value;
}
}