| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
|
| 7 |
/** |
| 8 |
* |
| 9 |
*/ |
| 10 |
class CJTServicesForm |
| 11 |
{ |
| 12 |
|
| 13 |
/** |
| 14 |
* put your comment there... |
| 15 |
* |
| 16 |
* @var mixed |
| 17 |
*/ |
| 18 |
protected $data = array(); |
| 19 |
|
| 20 |
/** |
| 21 |
* put your comment there... |
| 22 |
* |
| 23 |
* @var mixed |
| 24 |
*/ |
| 25 |
protected $definition = array(); |
| 26 |
|
| 27 |
/** |
| 28 |
* put your comment there... |
| 29 |
* |
| 30 |
* @var mixed |
| 31 |
*/ |
| 32 |
protected $name; |
| 33 |
|
| 34 |
/** |
| 35 |
* put your comment there... |
| 36 |
* |
| 37 |
* @param mixed $name |
| 38 |
* @param mixed $data |
| 39 |
* @return CJTServicesForm |
| 40 |
*/ |
| 41 |
public function __construct( $name, $data = null ) |
| 42 |
{ |
| 43 |
|
| 44 |
$this->name = $name; |
| 45 |
|
| 46 |
if ( is_array( $data ) ) |
| 47 |
{ |
| 48 |
$this->setArray( $data ); |
| 49 |
} |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* put your comment there... |
| 55 |
* |
| 56 |
* @param mixed $value |
| 57 |
*/ |
| 58 |
private function _type_array( $value ) |
| 59 |
{ |
| 60 |
if ( ! is_array( $value ) ) |
| 61 |
{ |
| 62 |
$value = array(); |
| 63 |
} |
| 64 |
|
| 65 |
return $value; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* put your comment there... |
| 70 |
* |
| 71 |
* @param mixed $value |
| 72 |
*/ |
| 73 |
private function _type_boolean( $value ) |
| 74 |
{ |
| 75 |
return ( bool ) $value; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* put your comment there... |
| 80 |
* |
| 81 |
* @param mixed $value |
| 82 |
*/ |
| 83 |
private function _type_float( $value ) |
| 84 |
{ |
| 85 |
return ( float ) $value; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* put your comment there... |
| 90 |
* |
| 91 |
* @param mixed $value |
| 92 |
*/ |
| 93 |
private function _type_integer( $value ) |
| 94 |
{ |
| 95 |
return intval( $value ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* put your comment there... |
| 100 |
* |
| 101 |
* @param mixed $value |
| 102 |
*/ |
| 103 |
private function _type_string( $value ) |
| 104 |
{ |
| 105 |
return strval( $value ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* put your comment there... |
| 110 |
* |
| 111 |
*/ |
| 112 |
protected function apply() |
| 113 |
{ |
| 114 |
|
| 115 |
# Fields to define using 2 parameters |
| 116 |
# First parameter is gruop name second parameter is |
| 117 |
# fields list, @see $this->define() |
| 118 |
$definitionsGroups = array_chunk( $this->definition, 2 ); |
| 119 |
|
| 120 |
# Process each group fields |
| 121 |
foreach ( $definitionsGroups as $groupDef ) |
| 122 |
{ |
| 123 |
|
| 124 |
$groupDefName = $groupDef[ 0 ]; |
| 125 |
|
| 126 |
foreach ( $groupDef[ 1 ] as $name => $definition ) |
| 127 |
{ |
| 128 |
|
| 129 |
# Set containers/groups if not set |
| 130 |
$groupNames = explode( '.', $groupDefName ); |
| 131 |
|
| 132 |
$pointer =& $this->data; |
| 133 |
|
| 134 |
foreach ( $groupNames as $groupName ) |
| 135 |
{ |
| 136 |
|
| 137 |
# For now lets use array for any doesnt exists container |
| 138 |
if ( ! isset( $pointer[ $groupName ] ) ) |
| 139 |
{ |
| 140 |
$pointer[ $groupName ] = array(); |
| 141 |
} |
| 142 |
|
| 143 |
$pointer =& $pointer[ $groupName ]; |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
// Set default as null if there is no default value set |
| 148 |
if (!isset($definition['default'])) |
| 149 |
{ |
| 150 |
$definition['default'] = null; |
| 151 |
} |
| 152 |
|
| 153 |
# Type cast handler name |
| 154 |
$typeHandler = "_type_{$definition[ 'type' ]}"; |
| 155 |
|
| 156 |
// Cast submitted value or set default if not value is set |
| 157 |
$pointer[$name] = isset($pointer[$name]) ? $this->$typeHandler($pointer[$name]) : $definition['default']; |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
return $this; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* put your comment there... |
| 168 |
* |
| 169 |
* @param mixed $definition |
| 170 |
*/ |
| 171 |
public function & define( $definitions ) |
| 172 |
{ |
| 173 |
|
| 174 |
$this->definition = func_get_args(); |
| 175 |
|
| 176 |
return $this; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* put your comment there... |
| 181 |
* |
| 182 |
*/ |
| 183 |
public function getArray() |
| 184 |
{ |
| 185 |
return $this->data; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* put your comment there... |
| 190 |
* |
| 191 |
* @param mixed $name |
| 192 |
*/ |
| 193 |
protected function & getElement( $path ) |
| 194 |
{ |
| 195 |
|
| 196 |
$pointer =& $this->data; |
| 197 |
|
| 198 |
$names = explode( '.', $path ); |
| 199 |
|
| 200 |
foreach ( $names as $name ) |
| 201 |
{ |
| 202 |
|
| 203 |
if ( ! isset( $pointer[ $name ] ) ) |
| 204 |
{ |
| 205 |
|
| 206 |
$nullReturn = null; |
| 207 |
|
| 208 |
return $nullReturn; |
| 209 |
} |
| 210 |
|
| 211 |
$pointer =& $pointer[ $name ]; |
| 212 |
} |
| 213 |
|
| 214 |
return $pointer; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* put your comment there... |
| 219 |
* |
| 220 |
* @param mixed $path |
| 221 |
*/ |
| 222 |
public function getElementId( $path ) |
| 223 |
{ |
| 224 |
|
| 225 |
$elementId = "{$this->name}-" . str_replace( '.', '-', $path ); |
| 226 |
|
| 227 |
return $elementId; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* put your comment there... |
| 232 |
* |
| 233 |
* @param mixed $path |
| 234 |
*/ |
| 235 |
public function getElementName( $path ) |
| 236 |
{ |
| 237 |
|
| 238 |
$elementName = "{$this->name}[" . str_replace( '.', '][', $path ) . ']'; |
| 239 |
|
| 240 |
return $elementName; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* put your comment there... |
| 245 |
* |
| 246 |
*/ |
| 247 |
public function getName() |
| 248 |
{ |
| 249 |
return $this->name; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* put your comment there... |
| 254 |
* |
| 255 |
* @param mixed $path |
| 256 |
*/ |
| 257 |
public function getValue( $path ) |
| 258 |
{ |
| 259 |
return $this->getElement( $path ); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* put your comment there... |
| 264 |
* |
| 265 |
* @param mixed $data |
| 266 |
*/ |
| 267 |
public function & setArray( $data ) |
| 268 |
{ |
| 269 |
|
| 270 |
$this->data = $data; |
| 271 |
|
| 272 |
# Apply definition ( e.g cast values if needed ) |
| 273 |
$this->apply(); |
| 274 |
|
| 275 |
return $this; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* put your comment there... |
| 280 |
* |
| 281 |
* @param mixed $path |
| 282 |
* @param mixed $data |
| 283 |
*/ |
| 284 |
public function & setValue( $path, $value ) |
| 285 |
{ |
| 286 |
|
| 287 |
$element =& $this->getElement( $path ); |
| 288 |
|
| 289 |
$element = $value; |
| 290 |
|
| 291 |
return $this; |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* put your comment there... |
| 296 |
* |
| 297 |
*/ |
| 298 |
public function & validate() |
| 299 |
{ |
| 300 |
return true; |
| 301 |
} |
| 302 |
|
| 303 |
} |
| 304 |
|