This repository has been archived by the owner on Dec 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Row.php
152 lines (125 loc) · 3.6 KB
/
Row.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
143
144
145
146
147
148
149
150
151
152
<?php
class EtuDev_Data_Row extends EtuDev_Data_ObservableRow implements EtuDev_Data_HighlightDataObject
{
/**
* to be extended, returns an array with the default data for a new entity
*
* @return array
*/
public function _getDefaultData()
{
return array();
}
/**
* returns the $data parameter if it is a valid row of table $table, or creates a row with that info
*
* @static
*
* @param EtuDev_Data_Table $table
* @param $data
*
* @return array|EtuDev_Interfaces_ToArrayAble|EtuDev_Interfaces_ToArrayAbleFull|null
*/
static public function checkOrCreateRowOfTable(EtuDev_Data_Table $table, $data)
{
if (!$data) {
return null;
}
if ($table->isRow($data)) {
return $data;
}
$c = null;
if (is_array($data)) {
/** @var $data array */
$c = $table->createRow();
$c->setFromArray($data);
} elseif (($data instanceof EtuDev_Interfaces_ToArrayAbleFull) || method_exists($data, 'toArrayFull')) {
/** @var $data EtuDev_Interfaces_ToArrayAbleFull */
$c = $table->createRow();
$c->setFromArray($data->toArrayFull());
} elseif (($data instanceof EtuDev_Interfaces_ToArrayAble) || method_exists($data, 'toArray')) {
/** @var $data EtuDev_Interfaces_ToArrayAble */
$c = $table->createRow();
$c->setFromArray($data->toArray());
}
return $c ? : $data;
}
/**
* @return EtuDev_Data_PseudoArray
*/
public function _setDefaultDataWhenNull()
{
$this->setValuesOnlyIfNull($this->_getDefaultData());
return $this;
}
protected function _insert()
{
$this->_setDefaultDataWhenNull();
$this->calculateBeforeModify();
return parent::_insert();
}
protected function _update()
{
$this->calculateBeforeModify();
}
protected function calculateBeforeModify()
{
foreach ($this->_getters as $k => $gt) {
if ($this->_isGetterBeforeModify($k)) {
$this->_setDirect($k, $this->$gt());
$this->addModifiedKeyIfNeeded($k);
}
}
}
/**
* to be extended: check if attribute can be used in calculateBeforeModify() or has to be ignored
*
* @param string $attribute
*
* @return bool if true it is used, if false ignored
*/
protected function _isGetterBeforeModify($attribute)
{
return true;
}
/**
* @var array con el highlight data
*/
protected $_highlightData = array();
/**
* @param array|SolrObject|Traversable $data
*
* @return EtuDev_Data_Row
*/
public function setHighlightData($data)
{
$a = array();
foreach ($data as $field => $value) {
$a[$field] = $value[0];
}
$this->_highlightData = $a;
return $this;
}
public function isHighlighted($origkey)
{
$key = $this->getDefinedAlias($origkey);
return $this->_isHighlighted($key);
}
protected function _isHighlighted($key)
{
return @isset($this->_highlightData[$key]);
}
public function getDirectHighlighted($origkey)
{
$key = $this->getDefinedAlias($origkey);
return $this->_getDirectFromHighlightedData($key);
}
protected function _getDirectFromHighlightedData($key)
{
return @$this->_highlightData[$key];
}
static public function getIdField()
{
return 'id';
}
}