-
Notifications
You must be signed in to change notification settings - Fork 1
/
AbstractModel.php
executable file
·142 lines (125 loc) · 3.42 KB
/
AbstractModel.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
134
135
136
137
138
139
140
141
142
<?php
/**
* Base class for a data model with getters, setters and validation.
* @abstract
*/
abstract class AbstractModel {
/**
* The list of fields stored in the model.
* @var array
*/
protected $_fields;
/**
* The fields and values stored in the model.
* @var array
*/
protected $_data;
/**
* Filters to apply to the data.
* @var array
*/
protected $_filters;
/**
* Validators to apply to the data.
* @var array
*/
protected $_validators;
/**
* Class to use to do the validation. This can be changed
* but it'll need to have the same API as the default Zend class.
* @var String
*/
protected $_validator_class = 'Zend_Filter_Input';
/**
* Instance of the $_validator_class variable.
* @var instance of $_validator_class
*/
protected $_validator;
/**
* The messages generated during validation.
* @var array
*/
protected $_messages;
/**
* Instantiate a new object.
* @param array $data An associative array of fields and data values.
* @return $this An instance of the class
*/
public function __construct (array $data = null) {
if ($data) {
foreach ($data as $name => $value) {
$this->$name = $value;
}
}
return $this;
}
/**
* Set a value for a data field.
* @param string $name Name of the data field.
* @param mixed $value The value to set.
* @return void
*/
public function __set ($name, $value) {
if (in_array($name, $this->_fields)) {
$this->_data[$name] = $value;
}
else {
throw new PropertyNotFoundException("$name not found in class");
}
}
/**
* Get a value for a data field.
* @param string $name Name of the data field.
* @return mixed
*/
public function __get ($name) {
if (in_array($name, $this->_fields)) {
return $this->_data[$name];
}
else {
throw new PropertyNotFoundException("$name not found in class");
}
}
/**
* Set a string for the name of the class to use to perform the validation.
* @param string $class Name of the class.
* @return void
*/
public function setValidatorClass ($class) {
$this->_validator_class = $class;
}
/**
* Test if the data in the model is valid according to the defined validation rules.
* @return bool
*/
public function isValid () {
$validator = $this->getValidator();
$validator->setData($this->_data);
$isValid = $validator->isValid();
if (!$isValid) {
$this->_messages = $validator->getErrors();
}
return $isValid;
}
/**
* Get an instantiation of the validator class.
* @return instance of $_validator_class variable.
*/
public function getValidator () {
if (!$this->_validator) {
$this->_validator = new $this->_validator_class($this->_filters, $this->_validators);
}
return $this->_validator;
}
/**
* Get the messages, if any, generated by the validation process.
* @return bool
*/
public function getMessages () {
return $this->_messages;
}
}
/**
* Exception for properties not found in the model
*/
class PropertyNotFoundException extends Exception { }