-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.builder.php
133 lines (120 loc) · 3.43 KB
/
class.builder.php
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
124
125
126
127
128
129
130
131
132
133
<?php
/**
* Tool for Toolbox
* @package Toolbox
*/
/**
* Builder class for Toolbox tools
*
* @package Toolbox
* @author Sergi Juanola
* @copyright Sergi Juanola 2012-2013
*/
abstract class Builder {
/**
* @var array $_config The array used to store configurations for each tool
*/
protected $_config = array();
/**
* Constructor for Builder classes.
* @param array $config The configuration used by this tool
*/
public function __construct($config)
{
foreach (self::getDefault(get_class($this)) as $key => $value) {
if(!array_key_exists($key, $config))
{
$config[$key] = $value;
}
}
$this->_config = $config;
}
/**
* Checks if there is a Toolbox accessible
* @return boolean TRUE if Toolbox exists, FALSE otherwise
*/
public static function hasToolbox()
{
return class_exists("Toolbox");
}
/**
* Sets a Builder's configuration variable
* @param string $key The variable name
* @param mixed $value The variable value. It can be of any type.
* @return Builder An instance of itself
*/
public function __set($key, $value)
{
$this->_config[$key] = $value;
return $this;
}
/**
* Gets a Builder's configuration variable
* @param string $key The variable name
* @return mixed The configuration value for the selected variable
*/
public function __get($key)
{
if(array_key_exists($key, $this->_config))
return $this->_config[$key];
else
return NULL;
}
/**
* Checks if a configuration variable is set
* @param string $name The variable name
* @return boolean TRUE if variable exists, FALSE otherwise
*/
public function __isset($name)
{
return isset($this->_config[$name]);
}
/**
* Unsets a variable from the configuration
* @param string $name The variable name
*/
public function __unset($name)
{
unset($this->_config[$name]);
}
/**
* If Toolbox is enabled, sets the configuration array as default
* @param string $class the class' name of this tool
* @param array $default the configuration that replaces the class' one
*/
public static function setDefault($class, $default) {
if(self::hasToolbox())
Toolbox::build()->setDefault($class, $default);
}
/**
* If Toolbox is enabled, adds the configuration array as default
* @param string $class the class' name of this tool
* @param array $default the configuration that is added to the class' one
*/
public static function addDefault($class, $default) {
if(self::hasToolbox())
Toolbox::build()->setDefault($class, array_merge($default, Toolbox::build()->getDefault($class)));
}
/**
* If Toolbox is enabled, sets the configuration array as default
* @param string $class the class' name of this tool
* @return mixed The class' default configuration, or an empty array if Toolbox is not set
*/
public static function getDefault($class) {
if(self::hasToolbox())
return Toolbox::build()->getWholeDefault($class);
else
return array();
}
/**
* Resets the tool's instance to the default configuration
* @param string $class the class' name of this tool
*/
public function resetToDefault($class) { foreach (self::getDefault($class) as $key => $value) { $this->_config[$key] = $value; } }
/**
* Building method
* @param array $config The configuration for this builder
* @return Builder An instance of itself
*/
public static function build($config = array()) { return new self($config); }
}