DataModel.php
3.32 KB
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
<?php
/**
* Custom model based on configuration passed into constructor
*
* @author Bogdan Savluk <savluk.bogdan@gmail.com>
*/
class DataModel extends CModel
{
/**
* Schema of page data. For details refer to PageData::__constructor()
*/
private $_schema = array();
/**
* Validator rules.
* @var array
*/
private $_rules = array();
private $_attributes = array();
private $_attributeNames = array();
private $_attributeLabels = array();
/**
* Creates new
* @param array $config
* Example:
* array(
* 'schema'=>array(
* 'key' => array(
* 'label' => 'Item Label',
* 'default' => null,
* ),
* ),
* 'rules'=>array(), // same as for CModel in rules()
* )
* @param string $scenario
*/
public function __construct($config = array(), $scenario = '')
{
$this->_schema = isset($config['schema']) ? $config['schema'] : array();
foreach ($this->_schema as $key => $itemConfig) {
$this->_attributes[$key] = isset($itemConfig['default']) ? $itemConfig['default'] : '';
$this->_attributeNames[] = $key;
$this->_attributeLabels[$key] = isset($itemConfig['label']) ? $itemConfig['label'] : '';
}
$safeAttributes = $this->_attributeNames;
if (isset($config['rules'])) {
foreach ($config['rules'] as $rule) {
$attributes = $rule[0];
if (is_string($attributes))
$attributes = preg_split('/[\s,]+/', $attributes, -1, PREG_SPLIT_NO_EMPTY);
foreach ($attributes as $attr) {
if (isset($safeAttributes[$attr])) unset($safeAttributes[$attr]);
}
$rule[0] = $attributes;
$this->_rules[] = $rule;
}
}
$this->_rules[] = array($safeAttributes, 'safe');
$this->setScenario($scenario);
$this->init();
$this->attachBehaviors($this->behaviors());
$this->afterConstruct();
}
public function init()
{
}
public function rules()
{
return $this->_rules;
}
public function attributeNames()
{
return $this->_attributeNames;
}
public function attributeLabels()
{
return $this->_attributeLabels;
}
public function __get($name)
{
if (array_key_exists($name, $this->_attributes))
return $this->_attributes[$name];
else
return parent::__get($name);
}
public function __set($name, $value)
{
if ($this->setAttribute($name, $value) === false) {
parent::__set($name, $value);
}
}
public function setAttribute($name, $value)
{
if (property_exists($this, $name))
$this->$name = $value;
else if (isset($this->_attributes[$name]))
$this->_attributes[$name] = $value;
else
return false;
return true;
}
public function __isset($name)
{
if (isset($this->_attributes[$name]))
return true;
else
return parent::__isset($name);
}
/**
* @return array Model properties as associative array
*/
public function getData()
{
return $this->_attributes;
}
}