-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clerk.php
75 lines (64 loc) · 1.91 KB
/
Clerk.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
<?php
class Clerk {
private $fields = array();
private $values;
public function __construct($values) {
spl_autoload_register(array($this, 'autoload'));
$this->values = $values; // $values is usually $_POST
}
/**
* Validate all fields and return errors, or false if none
*/
public function getErrors() {
$errors = array();
foreach ($this->fields as $field_name => $field) {
if ($field_errors = $field->getErrors()) {
$errors[$field_name] = $field_errors;
}
}
return empty($errors) ? false : $errors;
}
/**
* Returns a new instance of a Field class
*/
public function addField($type, $name) {
$args = func_get_args();
$args[0] = $name; // Replace $type with $name
$args[1] = isset($this->values[$name]) ? $this->values[$name] : null; // Replace $name with the field's value
$reflection = new ReflectionClass("Fields\\$type");
return $this->fields[$name] = $reflection->newInstanceArgs($args);
}
/**
* Provide an alias to addField.
*
* Without alias:
* $form->addField('Bool', 'Terms');
*
* With alias:
* $form->addBool('terms');
*/
public function __call($name, $args) {
if (substr($name, 0, 3) != 'add') { // We only want to target methods such as: add, addEmail, addBool
trigger_error('Call to undefined method '.__CLASS__."::$name()", E_USER_ERROR);
}
$type = substr($name, 3) ?: 'Field';
array_unshift($args, $type); // Prepend $args with $type
return call_user_func_array(array($this, 'addField'), $args);
}
public function __get($name) {
if (!array_key_exists($name, $this->fields)) {
trigger_error('Undefined property '.__CLASS__."::$name", E_USER_NOTICE);
return null;
}
return $this->fields[$name];
}
private function autoload($name) {
if (strpos($name, 'Fields') === 0) {
$path = str_replace('\\', '/', $name).'.php';
if (file_exists(__DIR__."/$path")) {
require($path);
return true;
}
}
}
}