forked from rkeplin/zf1-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Abstract.php
63 lines (54 loc) · 1.42 KB
/
Abstract.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
<?php
abstract class Keplin_Model_Abstract
{
public function __construct($options = null)
{
$this->setOptions($options);
}
public function setOptions($options)
{
if(null === $options)
{
return;
}
if(is_object($options) || is_array($options))
{
foreach($options as $field => $value)
{
$this->_setField($field, $value);
}
}
else
{
throw new Exception('The entity "' . get_class($this) . '" must be created by an array or object.');
}
}
protected function _setField($field, $value = null)
{
if(!$value)
{
return;
}
if(property_exists(get_class($this), $field))
{
//See if there is a setter
$method = 'set' . ucfirst($field);
if(method_exists(get_class($this), $method))
{
call_user_func(array($this, $method), $value);
return;
}
$this->$field = $value;
}
}
public function toArray()
{
$array = array();
foreach($this as $field => $value)
{
if($this->$field && !is_object($this->$field))
$array[$field] = stripslashes($value);
}
return $array;
}
}